/* ---------------------------------------------------------------------------- * EAOP 1.0, 2002-12-19 * (c) 2002 Remi Douence, Mario Sudholt; OBASCO group; EMN/INRIA; France * THIS SOFTWARE IS PROVIDED AS IS AND WITHOUT ANY WARRANTY -------------------------------------------------------------------------- */ import java.util.*; class Javazon { public void trade() { System.out.println("Welcome to www.javazon.com"); // scenario Customer c1 = new Customer("c1"); Customer c2 = new Customer("c2"); Shop.javazon.add("p1", 10); Shop.javazon.add("p2", 20); for (int i = 0; i < 6; i++) { c1.searchAll(); c1.searchByPrice(10); c1.buy(); c2.searchAll(); c2.searchByName("p"); c2.buy(); c2.checkout(); c1.searchAll(); c1.searchByPrice(10); c1.buy(); c1.checkout(); Shop.javazon.processOrders(); } } } class Product implements Cloneable { String name; float price; Product(String name, float price) { this.name = name; this.price = price; } public String toString() { return this.name + " costs " + this.price + " euros"; } } public class Customer { String name; Set search; ShoppingCart shoppingCart; public Customer(String name) { this.name = name; this.shoppingCart = new ShoppingCart(); } void searchAll() { this.search = Shop.javazon.searchAll(); } void searchByName(String subName) { this.search = Shop.javazon.searchByName(this.search, subName); } void searchByPrice(float maxPrice) { this.search = Shop.javazon.searchByPrice(this.search, maxPrice); } void buy() { this.shoppingCart.add(this.search); } public void checkout() { Shop.javazon.bill(this); } public String toString() { return this.name; } } class Shop { static Shop javazon = new Shop(); Set products; Vector orders; Shop() { this.products = new HashSet(); this.orders = new Vector(); } Shop add(String name, float price) { this.products.add(new Product(name, price)); return this; } Set searchAll() { return new HashSet(this.products); } Set searchByName(Set previousSearch, String subName) { Set result = new HashSet(); for (Iterator i = previousSearch.iterator(); i.hasNext(); ) { Product p = (Product)(i.next()); if (p.name.indexOf(subName) != -1) { result.add(p); } } return result; } Set searchByPrice(Set previousSearch, float maxPrice) { Set result = new HashSet(); for (Iterator i = previousSearch.iterator(); i.hasNext(); ) { Product p = (Product)(i.next()); if (p.price <= maxPrice) { result.add(p); } } return result; } void bill(Customer customer) { this.orders.add(new Order(customer, customer.shoppingCart.products)); customer.shoppingCart.products = new Vector(); } public void processOrders() { for (Iterator i = this.orders.iterator(); i.hasNext(); ) { ((Order)(i.next())).ship(); i.remove(); } } } class ShoppingCart { Vector products; ShoppingCart() { this.products = new Vector(); } void add(Set products) { for (Iterator i = products.iterator(); i.hasNext(); ) { this.products.add(i.next()); } } float total() { float total = 0; for (Iterator i = products.iterator(); i.hasNext();) { Product p = (Product)(i.next()); total += p.price; } return total; } } class Order { Customer customer; Vector products; Order(Customer customer, Vector products) { this.customer = customer; this.products = products; } float total() { float total = 0; for (Iterator i = this.products.iterator(); i.hasNext();) { Product p = (Product)(i.next()); total += p.price; } return total; } public void ship() { String message = "shipping "; for (Iterator i = this.products.iterator(); i.hasNext(); ) { message += i.next() + " "; } message += " to " + this.customer; System.out.println(message); } }