日期:2014-05-20 浏览次数:20795 次
/** ***************************************Seller*************************************************** */ class Seller implements Comparable<Seller> { private int id; private ArrayList<Product> products; private Intermediary intermediary;// private Seller(int id) { this.id = id; init(); } private Seller(int id, ArrayList<Product> products, Intermediary intermediary) { this.id = id; this.products = products; this.intermediary = intermediary; init(); } private void init() { if (products == null) products = new ArrayList<Product>(); if (intermediary == null) intermediary = Intermediary.createIntermediary(0); } public static Seller createSeller(int id) { return new Seller(id); } public static Seller createSeller(int id, ArrayList<Product> products, Intermediary intermediary) { return new Seller(id, products, intermediary); } boolean add(Product product) {// new arrivals return products.add(product); } boolean remove(Product product) {// take out of return products.remove(product); } public Product getProduct(int id) {// display product for (int i = 0; i < products.size(); i++) { if (products.get(i).getId() == id) return products.get(i); } return null; } public void displayProductInStore() {// display product for (int i = 0; i < products.size(); i++) { System.out.println(products.get(i)); } } public int compareTo(Seller seller) { int temp = id > seller.id ? 1 : (id == seller.id ? 0 : -1); return temp; } public Intermediary getIntermediary() { return intermediary; } public void setIntermediary(Intermediary intermediary) { this.intermediary = intermediary; } public int getId() { return id; } public void setId(int id) { this.id = id; } public ArrayList<Product> getProducts() { return products; } public void setProducts(ArrayList<Product> products) { this.products = products; } public String toString() { return getClass().getSimpleName() + id; } } /** ***************************************Buyer*************************************************** */ class Buyer implements Comparable<Buyer> { private int id; private ArrayList<Product> products; private Seller seller;// private Intermediary intermediary;// private Buyer(int id) { this.id = id; init(); } private Buyer(int id, ArrayList<Product> products, Seller seller, Intermediary intermediary) { super(); this.id = id; this.products = products; this.seller = seller; this.intermediary = intermediary; init(); } private void init() { if (products == null) products = new ArrayList<Product>(); if (seller == null) seller = Seller.createSeller(0); if (intermediary == null) intermediary = Intermediary.createIntermediary(0); } public static Buyer createBuyer(int id) { return new Buyer(id); } public static Buyer createBuyer(int id, ArrayList<Product> products, Seller seller, Intermediary intermediary) { return new Buyer(id, products, seller, intermediary); } public void add(int id, String name, String descreption) {// add products.add(Product.createProduct(id, name, descreption)); } public void add(Product product) {// add seller's product to shoppincar if (product != null) products.add(product); else throw new UnsupportedOperationException(); } public boolean remove(int id) {// remove product from shopping car for (int i = 0; i < products.size(); i++) { if (products.get(i).getId() == id) { products.remove(id); return true; } } return false; } boolean remove(Product product) {// remove product from shopping car return products.remove(product); } public Product getProduct(int id) { for (Product product : products) { if (product.getId() == id) return product; } return null; } public void displayProductInShoppingCar() {// display products in`shopping // car for (Product product : products) { System.out.println(product); } } public void displayProductFromSeller() { seller.displayProductInStore(); } public void displaySellerFromIntermediary() { intermediary.displaySeller(); } public void buy(Product product) {// buy thing intermediary.trade(seller, this, product); } public int compareTo(Buyer buyer) { int temp = id > buyer.id ? 1 : (id == buyer.id ? 0 : -1); return temp; } public int getId() { return id; } public void setId(int id) { this.id = id; } public Seller getSeller() { return seller; } public void setSeller(Seller seller) { this.seller = seller; } public Intermediary getIntermediary() { return intermediary; } public void setIntermediary(Intermediary intermediary) { this.intermediary = intermediary; } public ArrayList<Product> getProducts() { return products; } public void setProducts(ArrayList<Product> products) { this.products = products; } public String toString() { return getClass().getSimpleName() + id; } } /** ***************************************Product*************************************************** */ class Product { private final int id;// 每件商品ID是唯一的 private String name; private String description; private Product(int id) { super(); this.id = id; init(); } private Product(int id, String name, String description) { super(); this.id = id; this.name = name; this.description = description; init(); } private void init() { if (name == null) name = "name" + id; if (description == null) description = "description" + id; } public static Product createProduct(int id) { return new Product(id); } public static Product createProduct(int id, String name, String description) { return new Product(id, name, description); } public int getId() { return id; } public String getName() { return name; } public void setName(String name) { this.name = name; } public String getDescription() { return description; } public void setDescription(String description) { this.description = description; } public String toString() { return getClass().getSimpleName() + id; } }