⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 carttestclient1.java~7~

📁 EJB 实例
💻 JAVA~7~
字号:
package ejb;

import java.lang.Object;
import javax.naming.*;
import javax.rmi.PortableRemoteObject;
import java.lang.String;
import java.util.Date;
import java.util.Calendar;
import java.util.GregorianCalendar;
import javax.ejb.Handle;
import java.rmi.RemoteException;
import javax.ejb.EJBMetaData;
import javax.ejb.HomeHandle;
import java.util.List;

public class CartTestClient1 extends Object {
    private static final String ERROR_NULL_REMOTE = "Remote interface reference is null.  It must be created by calling one of the Home interface methods first.";
    private Cart cart = null;
    private CartHome cartHome = null;

    //Construct the EJB test client
    public CartTestClient1() {
        initialize();
    }

    public void initialize() {


        try {

            //get naming context
            Context context = new InitialContext();
            //look up jndi name
            Object ref = context.lookup("Cart");
            //look up jndi name and cast to Home interface
            cartHome = (CartHome) PortableRemoteObject.narrow(ref, CartHome.class);

        } catch (Exception e) {
            e.printStackTrace();
        }

    }



    //----------------------------------------------------------------------------
    // Methods that use Home interface methods to generate a Remote interface reference
    //----------------------------------------------------------------------------

    public Cart create(String cardHolderName, String creditCardNumber,
                       Date expirationDate) {
        try {
            cart = cartHome.create(cardHolderName, creditCardNumber,
                                   expirationDate);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return cart;
    }

    public EJBMetaData getEJBMetaData() {
        EJBMetaData returnValue = null;
        try {
            returnValue = cartHome.getEJBMetaData();
        } catch (Exception e) {
            e.printStackTrace();
        }
        return returnValue;
    }

    public HomeHandle getHomeHandle() {
        HomeHandle returnValue = null;
        try {
            returnValue = cartHome.getHomeHandle();
        } catch (Exception e) {
            e.printStackTrace();
        }
        return returnValue;
    }

    public void remove(Object object) {
        try {
            cartHome.remove(object);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    public void remove(Handle handle) {
        try {
            cartHome.remove(handle);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    //----------------------------------------------------------------------------
    // Methods that use Remote interface methods to access data through the bean
    //----------------------------------------------------------------------------

    public void addItem() throws RemoteException {
        /**@todo Update these variables with appropriate values for calling addItem().*/
        Item item = null;
        if (cart == null) {
            System.out.println("Error in addItem(): " + ERROR_NULL_REMOTE);
            return;
        }

        cart.addItem(item);
    }

    public List getContents() throws RemoteException {
        List returnValue = null;
        if (cart == null) {
            System.out.println("Error in getContents(): " + ERROR_NULL_REMOTE);
            return returnValue;
        }

        returnValue = cart.getContents();
        return returnValue;
    }

    public float getTotalPrice() throws RemoteException {
        float returnValue = 0f;
        if (cart == null) {
            System.out.println("Error in getTotalPrice(): " + ERROR_NULL_REMOTE);
            return returnValue;
        }

        returnValue = cart.getTotalPrice();
        return returnValue;
    }

    public void purchase() throws RemoteException {
        if (cart == null) {
            System.out.println("Error in purchase(): " + ERROR_NULL_REMOTE);
            return;
        }

        cart.purchase();
    }

    public void removeItem() throws RemoteException {
        /**@todo Update these variables with appropriate values for calling removeItem().*/
        Item item = null;
        if (cart == null) {
            System.out.println("Error in removeItem(): " + ERROR_NULL_REMOTE);
            return;
        }

        cart.removeItem(item);
    }

    //----------------------------------------------------------------------------
    // Utility Methods
    //----------------------------------------------------------------------------

    public CartHome getHome() {
        return cartHome;
    }

    //Main method
    public static void main(String[] args) {
        CartTestClient1 client = new CartTestClient1();
        CartHome home = client.getHome();

        String cardHolderName = "Suzy Programmer";
        String creditCardNumber = "1234-5678-9012-3456";
        Date expirationDate = new GregorianCalendar(2004, Calendar.JULY, 30).getTime();

        Cart cart = null;

        try {
            cart = home.create(cardHolderName, creditCardNumber, expirationDate);
        }   catch (Exception e) {
            System.out.println("Could not create Cart session bean\n" + e);
        }

        Item kassemBook = new Item("J2EE Blueprints", 39.99f, "Book");

        try {
          cart.addItem(kassemBook);
        } catch (Exception e) {
          System.out.println("Could not add the book to the cart\n" + e);
        }

        Item milesAlbum = new Item("Kind of Blue", 11.97f, "CD");

        try {
          cart.addItem(milesAlbum);
        } catch (Exception e) {
          System.out.println("Could not add the CD to the cart\n" + e);
        }

        try {
          summarize(cart);
        } catch (Exception e) {
          System.out.println("Could not summarize the items in the cart\n" + e);
        }

        try {
          cart.removeItem(kassemBook);
        } catch (Exception e) {
          System.out.println("Could not remove the book from the cart\n" + e);
        }

        Item calvertBook = new Item("Charles Calvert's Learn JBuilder 7", 49.95f, "Book");

        try {
          cart.addItem(calvertBook);
        } catch (Exception e) {
          System.out.println("Could not add book to the cart\n" + e);
        }

        try {
          summarize(cart);
        } catch (Exception e) {
          System.out.println("Could not summarize the items in the cart\n" + e);
        }

        try {
          cart.purchase();
        } catch(Exception e) {
          System.out.println("Could not purchase the items:\n" + e);
        }

        try {
          cart.remove();
        } catch(Exception e) {
          System.out.println("Could not remove the Cart bean\n" + e);
        }
    }

    private static void summarize(Cart cart) throws Exception {
        System.out.println("======= Cart Summary ========");
        List items = cart.getContents();
        for (int i = 0, n = items.size(); i < n; i++) {
          Item current = (Item) items.get(i);
          System.out.println("Price: $" + current.getPrice() + "\t" +
          current.getType() + "\t" + current.getTitle());
        }

        System.out.println("Total: $" + cart.getTotalPrice());
        System.out.println("=============================");
    }
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -