KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > orderclient > OrderClient


1 /*
2  * Copyright (c) 2005 Sun Microsystems, Inc. All rights reserved. U.S.
3  * Government Rights - Commercial software. Government users are subject
4  * to the Sun Microsystems, Inc. standard license agreement and
5  * applicable provisions of the FAR and its supplements. Use is subject
6  * to license terms.
7  *
8  * This distribution may include materials developed by third parties.
9  * Sun, Sun Microsystems, the Sun logo, Java and J2EE are trademarks
10  * or registered trademarks of Sun Microsystems, Inc. in the U.S. and
11  * other countries.
12  *
13  * Copyright (c) 2005 Sun Microsystems, Inc. Tous droits reserves.
14  *
15  * Droits du gouvernement americain, utilisateurs gouvernementaux - logiciel
16  * commercial. Les utilisateurs gouvernementaux sont soumis au contrat de
17  * licence standard de Sun Microsystems, Inc., ainsi qu'aux dispositions
18  * en vigueur de la FAR (Federal Acquisition Regulations) et des
19  * supplements a celles-ci. Distribue par des licences qui en
20  * restreignent l'utilisation.
21  *
22  * Cette distribution peut comprendre des composants developpes par des
23  * tierces parties. Sun, Sun Microsystems, le logo Sun, Java et J2EE
24  * sont des marques de fabrique ou des marques deposees de Sun
25  * Microsystems, Inc. aux Etats-Unis et dans d'autres pays.
26  */

27
28 package orderclient;
29
30
31 import java.util.*;
32 import javax.naming.Context JavaDoc;
33 import javax.naming.InitialContext JavaDoc;
34 import javax.rmi.PortableRemoteObject JavaDoc;
35 import order.LineItem;
36 import order.OrderRemote;
37 import order.OrderRemoteHome;
38
39
40 public class OrderClient {
41     public static void main(String JavaDoc[] 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 JavaDoc initial = new InitialContext JavaDoc();
50             Object JavaDoc 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 JavaDoc id = (String JavaDoc) order.getPrimaryKey();
68
69                 System.out.println(id);
70             }
71
72             System.exit(0);
73         } catch (Exception JavaDoc 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