|
| 1 | +package generics; |
| 2 | + |
1 | 3 | //: generics/Store.java |
2 | 4 | // Building up a complex model using generic containers. |
3 | 5 | import java.util.*; |
4 | 6 | import net.mindview.util.*; |
5 | 7 |
|
6 | 8 | class Product { |
7 | | - private final int id; |
8 | | - private String description; |
9 | | - private double price; |
10 | | - public Product(int IDnumber, String descr, double price){ |
11 | | - id = IDnumber; |
12 | | - description = descr; |
13 | | - this.price = price; |
14 | | - System.out.println(toString()); |
15 | | - } |
16 | | - public String toString() { |
17 | | - return id + ": " + description + ", price: $" + price; |
18 | | - } |
19 | | - public void priceChange(double change) { |
20 | | - price += change; |
21 | | - } |
22 | | - public static Generator<Product> generator = |
23 | | - new Generator<Product>() { |
24 | | - private Random rand = new Random(47); |
25 | | - public Product next() { |
26 | | - return new Product(rand.nextInt(1000), "Test", |
27 | | - Math.round(rand.nextDouble() * 1000.0) + 0.99); |
28 | | - } |
29 | | - }; |
| 9 | + private final int id; |
| 10 | + private String description; |
| 11 | + private double price; |
| 12 | + |
| 13 | + public Product(int IDnumber, String descr, double price) { |
| 14 | + id = IDnumber; |
| 15 | + description = descr; |
| 16 | + this.price = price; |
| 17 | + System.out.println(toString()); |
| 18 | + } |
| 19 | + |
| 20 | + public String toString() { |
| 21 | + return id + ": " + description + ", price: $" + price; |
| 22 | + } |
| 23 | + |
| 24 | + public void priceChange(double change) { |
| 25 | + price += change; |
| 26 | + } |
| 27 | + |
| 28 | + public static Generator<Product> generator = new Generator<Product>() { |
| 29 | + private Random rand = new Random(47); |
| 30 | + |
| 31 | + public Product next() { |
| 32 | + return new Product(rand.nextInt(1000), "Test", Math.round(rand |
| 33 | + .nextDouble() * 1000.0) + 0.99); |
| 34 | + } |
| 35 | + }; |
30 | 36 | } |
31 | 37 |
|
32 | 38 | class Shelf extends ArrayList<Product> { |
33 | | - public Shelf(int nProducts) { |
34 | | - Generators.fill(this, Product.generator, nProducts); |
35 | | - } |
36 | | -} |
| 39 | + public Shelf(int nProducts) { |
| 40 | + Generators.fill(this, Product.generator, nProducts); |
| 41 | + } |
| 42 | +} |
37 | 43 |
|
38 | 44 | class Aisle extends ArrayList<Shelf> { |
39 | | - public Aisle(int nShelves, int nProducts) { |
40 | | - for(int i = 0; i < nShelves; i++) |
41 | | - add(new Shelf(nProducts)); |
42 | | - } |
| 45 | + public Aisle(int nShelves, int nProducts) { |
| 46 | + for (int i = 0; i < nShelves; i++) |
| 47 | + add(new Shelf(nProducts)); |
| 48 | + } |
43 | 49 | } |
44 | 50 |
|
45 | | -class CheckoutStand {} |
46 | | -class Office {} |
| 51 | +class CheckoutStand { |
| 52 | +} |
| 53 | + |
| 54 | +class Office { |
| 55 | +} |
47 | 56 |
|
48 | 57 | public class Store extends ArrayList<Aisle> { |
49 | | - private ArrayList<CheckoutStand> checkouts = |
50 | | - new ArrayList<CheckoutStand>(); |
51 | | - private Office office = new Office(); |
52 | | - public Store(int nAisles, int nShelves, int nProducts) { |
53 | | - for(int i = 0; i < nAisles; i++) |
54 | | - add(new Aisle(nShelves, nProducts)); |
55 | | - } |
56 | | - public String toString() { |
57 | | - StringBuilder result = new StringBuilder(); |
58 | | - for(Aisle a : this) |
59 | | - for(Shelf s : a) |
60 | | - for(Product p : s) { |
61 | | - result.append(p); |
62 | | - result.append("\n"); |
63 | | - } |
64 | | - return result.toString(); |
65 | | - } |
66 | | - public static void main(String[] args) { |
67 | | - System.out.println(new Store(14, 5, 10)); |
68 | | - } |
69 | | -} /* Output: |
70 | | -258: Test, price: $400.99 |
71 | | -861: Test, price: $160.99 |
72 | | -868: Test, price: $417.99 |
73 | | -207: Test, price: $268.99 |
74 | | -551: Test, price: $114.99 |
75 | | -278: Test, price: $804.99 |
76 | | -520: Test, price: $554.99 |
77 | | -140: Test, price: $530.99 |
78 | | -... |
79 | | -*///:~ |
| 58 | + private ArrayList<CheckoutStand> checkouts = new ArrayList<CheckoutStand>(); |
| 59 | + private Office office = new Office(); |
| 60 | + |
| 61 | + public Store(int nAisles, int nShelves, int nProducts) { |
| 62 | + for (int i = 0; i < nAisles; i++) |
| 63 | + add(new Aisle(nShelves, nProducts)); |
| 64 | + } |
| 65 | + |
| 66 | + public String toString() { |
| 67 | + StringBuilder result = new StringBuilder(); |
| 68 | + for (Aisle a : this) |
| 69 | + for (Shelf s : a) |
| 70 | + for (Product p : s) { |
| 71 | + result.append(p); |
| 72 | + result.append("\n"); |
| 73 | + } |
| 74 | + return result.toString(); |
| 75 | + } |
| 76 | + |
| 77 | + public static void main(String[] args) { |
| 78 | + System.out.println(new Store(14, 5, 10)); |
| 79 | + } |
| 80 | +} /* |
| 81 | + * Output: 258: Test, price: $400.99 861: Test, price: $160.99 868: Test, price: |
| 82 | + * $417.99 207: Test, price: $268.99 551: Test, price: $114.99 278: Test, price: |
| 83 | + * $804.99 520: Test, price: $554.99 140: Test, price: $530.99 ... |
| 84 | + */// :~ |
0 commit comments