Coding the bean implementation

This class implements the business methods defined in the bean remote interface. It also defines some standard methods that are declared abstract on SessionBean and so these methods should be implemented for our bean implementation to be complete. Finally, because we overloaded the create method on the home interface, we must provide matching ejbCreate methods in the bean implementation that accept the same sets of parameters. This is because the bean implementation class is the only place that you put your bean code. The implementation of the home interface that we defined in Coding the home interface is generated by the tooling, so if we need to implement an overloaded create method, we have to do it here:

package casino;

     import java.util.Random;
     import javax.ejb.*;

     public class RouletteWheelBean implements SessionBean {

       // Necessary code to fulfill SessionBean interface definition.

       private SessionContext ctx = null;

       public void ejbActivate() throws javax.ejb.EJBException {}
       public void ejbPassivate() throws javax.ejb.EJBException {}
       public void ejbRemove() throws javax.ejb.EJBException {}
       public SessionContext getSessionContext() { return ctx; }
       public void setSessionContext(SessionContext ctx) throws 
         javax.ejb.EJBException { this.ctx = ctx; 
       }

       /////////////////////////////
       // The bean state information
       private int wheelValue;

       private int currentCredit;

       /////////////////////
       // Our create methods

       public void ejbCreate() throws javax.ejb.EJBException, CreateException {
         currentCredit = 100;
         wheelValue = ((int)System.currentTimeMillis())%37;
       }

       public void ejbCreate(int credit) throws javax.ejb.EJBException,
         CreateException { currentCredit = credit;
         wheelValue = ((int)System.currentTimeMillis())%37;
       }

       ///////////////////////////////////////////////////////////////////////////
       // Implementations of the remote methods the client may call on an instance
       
       //
       // Place a bet, either "red" or "black" for the specified amount.
       // Then simulate the wheel spinning and construct a response string 
       // indicating the outcome to the caller.
       //
       public String bet(String color,int amount) throws javax.ejb.EJBException {

         if (!color.equalsIgnoreCase("red") && !color.equalsIgnoreCase("black"))
           return new String("You can only bet on red or black");
           
         if (amount > currentCredit) 
           return new String("You only have $"+currentCredit+" !");

         // Use the current wheel value as the random number seed
         Random randomizer = new Random((long)wheelValue);

         // Spin the wheel
         wheelValue = Math.abs(randomizer.nextInt()) % 37;

         // Construct a reply
         StringBuffer result = 
           new StringBuffer("Number: "+wheelValue+" Color: "+color(wheelValue)+"\n");

         // Did the caller win?
         if (color(wheelValue).equalsIgnoreCase(color)) {
           currentCredit+=(amount*2);
           result.append("Well Done! You won $");
           result.append((amount*2));
         } else {
           currentCredit -= amount;
           result.append("Bad Luck! You lost $");
           result.append(amount);
         }
         result.append(", you now have $");
         result.append(currentCredit);
         return result.toString();
       }


       //
       // Return the current status of this roulette wheel instance.
       // The number and color
       // it is currently on and the amount of credit the client still has to gamble.
       //
       public String getCurrentStatus() throws javax.ejb.EJBException {
         return new String("Number:"+wheelValue+" Color:"+color(wheelValue)+"
         You have $"+currentCredit);
       }


       //
       // Allow the client to collect his winnings, then zero the credit so
       // they cannot collect twice!
       //
       public int collectWinnings()throws javax.ejb.EJBException {
         int winnings = currentCredit;
         currentCredit = 0; 
         return winnings;
       }


       //
       // Convert a number on the wheel into a color
       //
       private String color(int value) {
         if (value == 0) return "Green";
         if (value % 2 == 0) return "Black";
         return "Red";
       }

     }