// generics/Store.java// (c)2017 MindView LLC: see Copyright.txt// We make no guarantees that this code is fit for any purpose.// Visit http://OnJava8.com for more book information.// Building a complex model using generic collectionsimport java.util.*;import java.util.function.*;import onjava.*;class Product {private final int id;private String description;private double price;Product(int idNumber, String descr, double price) {id = idNumber;description = descr;this.price = price;System.out.println(toString());}@Overridepublic String toString() {return id + ": " + description +", price: $" + price;}public void priceChange(double change) {price += change;}public static Supplier<Product> generator =new Supplier<Product>() {private Random rand = new Random(47);@Overridepublic Product get() {return new Product(rand.nextInt(1000), "Test",Math.round(rand.nextDouble() * 1000.0) + 0.99);}};}class Shelf extends ArrayList<Product> {Shelf(int nProducts) {Suppliers.fill(this, Product.generator, nProducts);}}class Aisle extends ArrayList<Shelf> {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));}@Overridepublic 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(5, 4, 3));}}/* Output: (First 8 Lines)258: Test, price: 400ドル.99861: Test, price: 160ドル.99868: Test, price: 417ドル.99207: Test, price: 268ドル.99551: Test, price: 114ドル.99278: Test, price: 804ドル.99520: Test, price: 554ドル.99140: Test, price: 530ドル.99...*/
此处可能存在不合适展示的内容,页面不予展示。您可通过相关编辑功能自查并修改。
如您确认内容无涉及 不当用语 / 纯广告导流 / 暴力 / 低俗色情 / 侵权 / 盗版 / 虚假 / 无价值内容或违法国家有关法律法规的内容,可点击提交进行申诉,我们将尽快为您处理。