1 27 28 package orderclient; 29 30 31 import java.util.*; 32 import javax.naming.Context ; 33 import javax.naming.InitialContext ; 34 import javax.rmi.PortableRemoteObject ; 35 import order.LineItem; 36 import order.OrderRemote; 37 import order.OrderRemoteHome; 38 39 40 public class OrderClient { 41 public static void main(String [] args) { 42 try { 43 ArrayList lineItems = new ArrayList(); 44 45 lineItems.add(new LineItem("p23", 13, 12.00, 1, "123")); 46 lineItems.add(new LineItem("p67", 47, 89.00, 2, "123")); 47 lineItems.add(new LineItem("p11", 28, 41.00, 3, "123")); 48 49 Context initial = new InitialContext (); 50 Object objref = initial.lookup("ejb/SimpleOrder"); 51 52 OrderRemoteHome home = 53 (OrderRemoteHome) PortableRemoteObject.narrow(objref, OrderRemoteHome.class); 54 55 OrderRemote duke = 56 home.create("123", "c44", "open", totalItems(lineItems), 57 lineItems); 58 59 displayItems(duke.getLineItems()); 60 System.out.println(); 61 62 Collection c = home.findByProductId("p67"); 63 Iterator i = c.iterator(); 64 65 while (i.hasNext()) { 66 OrderRemote order = (OrderRemote) i.next(); 67 String id = (String ) order.getPrimaryKey(); 68 69 System.out.println(id); 70 } 71 72 System.exit(0); 73 } catch (Exception ex) { 74 System.err.println("Caught an exception."); 75 ex.printStackTrace(); 76 } 77 } 78 79 static double totalItems(ArrayList lineItems) { 80 double total = 0.00; 81 ListIterator iterator = lineItems.listIterator(0); 82 83 while (iterator.hasNext()) { 84 LineItem item = (LineItem) iterator.next(); 85 86 total += item.getUnitPrice(); 87 } 88 89 return total; 90 } 91 92 static void displayItems(ArrayList lineItems) { 93 ListIterator iterator = lineItems.listIterator(0); 94 95 while (iterator.hasNext()) { 96 LineItem item = (LineItem) iterator.next(); 97 98 System.out.println(item.getOrderId() + " " + item.getItemNo() + 99 " " + item.getProductId() + " " + item.getUnitPrice()); 100 } 101 } 102 } 103 | Popular Tags |