KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > Javazon


1 /* ----------------------------------------------------------------------------
2  * EAOP 1.0, 2002-12-19
3  * (c) 2002 Remi Douence, Mario Sudholt; OBASCO group; EMN/INRIA; France
4  * THIS SOFTWARE IS PROVIDED AS IS AND WITHOUT ANY WARRANTY
5   -------------------------------------------------------------------------- */

6
7 import java.util.*;
8
9 class Javazon {
10
11     public void trade() {
12     System.out.println("Welcome to www.javazon.com");
13
14     // scenario
15
Customer c1 = new Customer("c1");
16     Customer c2 = new Customer("c2");
17      
18     Shop.javazon.add("p1", 10);
19     Shop.javazon.add("p2", 20);
20     
21     for (int i = 0; i < 6; i++) {
22         c1.searchAll();
23         c1.searchByPrice(10);
24         c1.buy();
25         c2.searchAll();
26         c2.searchByName("p");
27         c2.buy();
28         c2.checkout();
29         c1.searchAll();
30         c1.searchByPrice(10);
31         c1.buy();
32         c1.checkout();
33         
34         Shop.javazon.processOrders();
35     }
36     }
37 }
38
39 class Product implements Cloneable JavaDoc {
40
41     String JavaDoc name;
42     float price;
43
44     Product(String JavaDoc name, float price) {
45     this.name = name;
46     this.price = price;
47     }
48     public String JavaDoc toString() {
49     return this.name + " costs " + this.price + " euros";
50     }
51 }
52
53 public class Customer {
54
55     String JavaDoc name;
56     Set search;
57     ShoppingCart shoppingCart;
58
59     public Customer(String JavaDoc name) {
60     this.name = name;
61     this.shoppingCart = new ShoppingCart();
62     }
63     void searchAll() {
64     this.search = Shop.javazon.searchAll();
65     }
66     void searchByName(String JavaDoc subName) {
67     this.search = Shop.javazon.searchByName(this.search, subName);
68     }
69     void searchByPrice(float maxPrice) {
70     this.search = Shop.javazon.searchByPrice(this.search, maxPrice);
71     }
72     void buy() {
73     this.shoppingCart.add(this.search);
74     }
75     public void checkout() {
76     Shop.javazon.bill(this);
77     }
78     public String JavaDoc toString() {
79     return this.name;
80     }
81 }
82
83 class Shop {
84
85     static Shop javazon = new Shop();
86
87     Set products;
88     Vector orders;
89
90     Shop() {
91     this.products = new HashSet();
92     this.orders = new Vector();
93     }
94     Shop add(String JavaDoc name, float price) {
95     this.products.add(new Product(name, price));
96     return this;
97     }
98     Set searchAll() {
99     return new HashSet(this.products);
100     }
101     Set searchByName(Set previousSearch, String JavaDoc subName) {
102     Set result = new HashSet();
103     for (Iterator i = previousSearch.iterator(); i.hasNext(); ) {
104         Product p = (Product)(i.next());
105         if (p.name.indexOf(subName) != -1) {
106         result.add(p);
107         }
108     }
109     return result;
110     }
111     Set searchByPrice(Set previousSearch, float maxPrice) {
112     Set result = new HashSet();
113     for (Iterator i = previousSearch.iterator(); i.hasNext(); ) {
114         Product p = (Product)(i.next());
115         if (p.price <= maxPrice) {
116         result.add(p);
117         }
118     }
119     return result;
120     }
121     void bill(Customer customer) {
122     this.orders.add(new Order(customer, customer.shoppingCart.products));
123     customer.shoppingCart.products = new Vector();
124     }
125     public void processOrders() {
126     for (Iterator i = this.orders.iterator(); i.hasNext(); ) {
127         ((Order)(i.next())).ship();
128         i.remove();
129     }
130     }
131 }
132
133 class ShoppingCart {
134
135     Vector products;
136
137     ShoppingCart() {
138     this.products = new Vector();
139     }
140     void add(Set products) {
141     for (Iterator i = products.iterator(); i.hasNext(); ) {
142         this.products.add(i.next());
143     }
144     }
145     float total() {
146     float total = 0;
147         for (Iterator i = products.iterator(); i.hasNext();) {
148         Product p = (Product)(i.next());
149         total += p.price;
150         }
151     return total;
152     }
153 }
154
155 class Order {
156
157     Customer customer;
158     Vector products;
159
160     Order(Customer customer, Vector products) {
161     this.customer = customer;
162     this.products = products;
163     }
164     float total() {
165     float total = 0;
166         for (Iterator i = this.products.iterator(); i.hasNext();) {
167         Product p = (Product)(i.next());
168             total += p.price;
169         }
170     return total;
171     }
172     public void ship() {
173     String JavaDoc message = "shipping ";
174     for (Iterator i = this.products.iterator(); i.hasNext(); ) {
175         message += i.next() + " ";
176     }
177     message += " to " + this.customer;
178     System.out.println(message);
179     }
180 }
181
Popular Tags