日期:2014-05-20  浏览次数:20795 次

商店客户服务程序
老师上课演示了一个程序,让我们课下补完,过程中出现了一些问题,我java菜鸟一只,求~
Product.java:
id,name,description变量,以及对应的get和set函数

Store.java:
void addProduct(...){...}
void removeProduct(...){...}
void delProduct(...){...}
void printProductList(...){...}

Seller.java
Store store;
String sellerName;

BuyList.java:
public void addProduct(String name, int number){...}

Buyer.java:
BuyList buylist;
String buyerName;

Test.java:
测试的实现。
问题出现啦:main函数必然是static的,可是main引用的一些sellerRun(运行seller操作的函数)和buyerRun也必须设置为static,否则报错。
运行的时候选择seller之后设置完产品列表,返回到上一级菜单(选择buyer还是seller的菜单)的时候没法保存产品列表,无论再进哪个用户都会显示产品列表为空(这个问题和static有关么)
刚从c转过来,封装性和面向对象让人很头大,总感觉这程序开始构思的时候就有一些混乱……

------解决方案--------------------
在网上找一本自己喜欢的讲java基础的书看吧!
这些都是java基础,还是需要自己慢慢练,慢慢理解的!
------解决方案--------------------
Java code

/** ***************************************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;
    }
}