FileDocCategorySizeDatePackage
Client_2.javaAPI DocExample3009Sat Mar 06 09:32:24 GMT 1999com.titan.cabin

Client_2.java

package com.titan.cabin;

import com.titan.cabin.CabinHome;
import com.titan.cabin.Cabin;
import com.titan.cabin.CabinPK;

import javax.naming.InitialContext;
import javax.naming.Context;
import javax.naming.NamingException;
import javax.ejb.CreateException;
import java.rmi.RemoteException;
import java.util.Properties;

public class Client_2 {

    public static void main(String [] args){
        try{
            Context jndiContext = getInitialContext();

            CabinHome home = (CabinHome)jndiContext.lookup("CabinHome");

            // add 9 cabins to deck 1 of ship 1
            makeCabins(home, 2, 10, 1, 1);
            // add 10 cabins to deck 2 of ship 1
            makeCabins(home, 11, 20, 2, 1);
            // add 10 cabins to deck 3 of ship 1
            makeCabins(home, 21, 30, 3, 1);

            // add 10 cabins to deck 1 of ship 2
            makeCabins(home, 31, 40, 1, 2);
            // add 10 cabins to deck 2 of ship 2
            makeCabins(home, 41, 50, 2, 2);
            // add 10 cabins to deck 3 of ship 2
            makeCabins(home, 51, 60, 3, 2);

            // add 10 cabins to deck 1 of ship 3
            makeCabins(home, 61, 70, 1, 3);
            // add 10 cabins to deck 2 of ship 3
            makeCabins(home, 71, 80, 2, 3);
            // add 10 cabins to deck 3 of ship 3
            makeCabins(home, 81, 90, 3, 3);
             // add 10 cabins to deck 4 of ship 3
            makeCabins(home, 91, 100, 4, 3);

            for(int i = 1; i <= 100; i++){
                CabinPK pk = new CabinPK();
                pk.id = i;
                Cabin cabin = home.findByPrimaryKey(pk);
                System.out.println("PK = "+i+", Ship = "+cabin.getShip()
                  + ", Deck = "+cabin.getDeckLevel()
                  + ", BedCount = "+cabin.getBedCount()
                  + ", Name = "+cabin.getName());
            }

        }catch(java.rmi.RemoteException re){re.printStackTrace();}
         catch(javax.naming.NamingException ne)
              {ne.printStackTrace();}
         catch(javax.ejb.CreateException ce){ce.printStackTrace();}
         catch(javax.ejb.FinderException fe){fe.printStackTrace();}
    }

  static public javax.naming.Context getInitialContext()
                throws javax.naming.NamingException{
    Properties p = new Properties();
    // ... specify the JNDI properties specific to the vendor
    return new javax.naming.InitialContext(p);
  }
  public static void makeCabins(CabinHome home, int fromId, int toId,
    int deckLevel, int shipNumber)
    throws RemoteException, CreateException{

    int bc = 3;
    for(int i = fromId; i <= toId; i++){
        Cabin cabin = home.create(i);
        int suiteNumber = deckLevel*100+(i-fromId);
        cabin.setName("Suite "+suiteNumber);
        cabin.setDeckLevel(deckLevel);
        bc = (bc==3)?2:3;
        cabin.setBedCount(bc);
        cabin.setShip(shipNumber);
    }
  }
}