// final code, two bugs (inconsistent expected values) found import java.util.*; // ----------------------------------------------------------------------------- // the entry point for the ordering process class Ordering { Vector /* Order */ open = new Vector(); Vector /* Customer */ customers = new Vector(); // -------------------------------------------- Customer lookup(String name) { for(int i = 0; i < customers.size(); i++) { Customer c = (Customer)customers.elementAt(i); if (name.equals(c.name)) return c; } return null; } Order openOrder(Customer c) { Order o = new Order(c); open.add(o); return o; } // add an order line to o void addLine(Order o, Product p, int q) { o.addLine(p,q); } // compute the value of the order, write to file int closeOrder(Order o) { return o.value(); } // -------------------------------------------- // Examples and Tests // no examples needed: only one exists (singleton pattern) public static void main(String argv[]) { Ordering o = new Ordering(); o.customers.add(Customer.c1); o.customers.add(Customer.c2); o.customers.add(Customer.c3); Order or1 = o.openOrder(o.lookup("Matthias")); Order or2 = o.openOrder(o.lookup("Jonathan")); Tester.check("testing ordering: order creation", or2.c, Customer.c1); Tester.check("testing ordering: order creation", or1.c, Customer.c2); o.addLine(or1, Product.p1, 10); o.addLine(or2, Product.p2, 2); o.addLine(or1, Product.p3, 1); int v1 = o.closeOrder(or1); int v2 = o.closeOrder(or2); Tester.check("testing ordering: order closure", v1, 20); Tester.check("testing ordering: order closure", v2, 10); } } // ----------------------------------------------------------------------------- // representing a customer class Customer { String name; String address; Customer(String name, String address) { this.name = name; this.address = address; } // -------------------------------------------- // Examples static Customer c1 = new Customer("Jonathan","Northeastern"); static Customer c2 = new Customer("Matthias","Rice"); static Customer c3 = new Customer("Robby","Chicago"); } // ----------------------------------------------------------------------------- // representing an order class Order { Customer c; Vector /* OrderLine */ lines = new Vector(); // -------------------------------------------- Order(Customer c) { this.c = c; } // add a line for p and q to the order void addLine(Product p, int q) { lines.add(new OrderLine(p,q)); } // compute the value of the order int value() { int sum = 0; for(int i = 0; i < lines.size(); i++) sum += ((OrderLine)lines.elementAt(i)).value; return sum; } // ---------------------------------------------- // Examples and Tests static Order o1 = new Order(Customer.c1); public static void main(String argv[]) { // populate order o1.addLine(Product.p1, 10); o1.addLine(Product.p2, 2); o1.addLine(Product.p3, 1); // <===== bug (1) // System.out.println("value = " + o1.value()); Tester.check("testing order, addline + value", o1.value(), 26); } } // ----------------------------------------------------------------------------- // representing an orderline class OrderLine { Product p; int q; int value; // -------------------------------------------- OrderLine(Product p, int q) { this.p = p; this.q = q; value = p.price * q; } // -------------------------------------------- // Examples & Tests static OrderLine ol1 = new OrderLine(Product.p1, 10); static OrderLine ol2 = new OrderLine(Product.p2, 5); static OrderLine ol3 = new OrderLine(Product.p3, 1); public static void main(String argv[]) { Tester.check("testing order line", ol1.value, 10); } } // ----------------------------------------------------------------------------- // representing a product class Product { String name; String description; int price; // -------------------------------------------- Product(String name, String description, int price) { this.name = name; this.description = description; this.price = price; } // -------------------------------------------- // Examples static Product p1 = new Product("salt", "oh well", 1); static Product p2 = new Product("pepper", "so so", 3); // <=== bug (2) static Product p3 = new Product("jalapeno", "yeap!", 10); } // ----------------------------------------------------------------------------- // a minimal test infrastructure class Tester { static int faults = 0; static void check(String s, int i1, int i2) { check(s, new Integer(i1), new Integer(i2), i1==i2); } static void check(String s, Object o1, Object o2) { check(s, o1, o2, o1==o2); } static void check(String s, Object o1, Object o2, boolean t) { if (!t) { faults += 1; System.out.println("*** test failed: " + s + " got " + o1 + " expected " + o2 + " *** "); } } public static void main(String argv[]) { // calling all test methods in all classes OrderLine.main(argv); Order.main(argv); Ordering.main(argv); System.out.println(faults + " test cases failed"); } }