89 lines
2.2 KiB
Java
Raw Normal View History

2015-09-07 11:44:36 -06:00
// generics/Store.java
2015-12-15 11:47:04 -08:00
// (c)2016 MindView LLC: see Copyright.txt
2015-11-15 15:51:35 -08:00
// We make no guarantees that this code is fit for any purpose.
2016-09-23 13:23:35 -06:00
// Visit http://OnJava8.com for more book information.
2016-01-25 18:05:55 -08:00
// Building a complex model using generic collections
2015-06-15 17:47:35 -07:00
import java.util.*;
2015-11-03 12:00:44 -08:00
import java.util.function.*;
2016-01-25 18:05:55 -08:00
import onjava.*;
2015-06-15 17:47:35 -07:00
class Product {
private final int id;
private String description;
private double price;
2016-01-25 18:05:55 -08:00
public
Product(int IDnumber, String descr, double price) {
2015-06-15 17:47:35 -07:00
id = IDnumber;
description = descr;
this.price = price;
System.out.println(toString());
}
@Override
public String toString() {
return id + ": " + description + ", price: $" + price;
}
public void priceChange(double change) {
price += change;
}
2015-11-03 12:00:44 -08:00
public static Supplier<Product> generator =
new Supplier<Product>() {
2015-06-15 17:47:35 -07:00
private Random rand = new Random(47);
@Override
2015-11-03 12:00:44 -08:00
public Product get() {
2015-06-15 17:47:35 -07:00
return new Product(rand.nextInt(1000), "Test",
Math.round(rand.nextDouble() * 1000.0) + 0.99);
}
};
}
class Shelf extends ArrayList<Product> {
public Shelf(int nProducts) {
2015-11-03 12:00:44 -08:00
Suppliers.fill(this, Product.generator, nProducts);
2015-06-15 17:47:35 -07:00
}
}
class Aisle extends ArrayList<Shelf> {
public Aisle(int nShelves, int nProducts) {
for(int i = 0; i < nShelves; i++)
add(new Shelf(nProducts));
}
}
class CheckoutStand {}
class Office {}
public class Store extends ArrayList<Aisle> {
private ArrayList<CheckoutStand> checkouts =
new ArrayList<>();
private Office office = new Office();
public Store(int nAisles, int nShelves, int nProducts) {
for(int i = 0; i < nAisles; i++)
add(new Aisle(nShelves, nProducts));
}
@Override
public String toString() {
StringBuilder result = new StringBuilder();
for(Aisle a : this)
for(Shelf s : a)
for(Product p : s) {
result.append(p);
result.append("\n");
}
return result.toString();
}
public static void main(String[] args) {
System.out.println(new Store(14, 5, 10));
}
2015-09-07 11:44:36 -06:00
}
/* Output: (First 8 Lines)
2015-06-15 17:47:35 -07:00
258: Test, price: $400.99
861: Test, price: $160.99
868: Test, price: $417.99
207: Test, price: $268.99
551: Test, price: $114.99
278: Test, price: $804.99
520: Test, price: $554.99
140: Test, price: $530.99
...
2015-09-07 11:44:36 -06:00
*/