public class ProductDAO { private ArrayList productList;

public ProductDAO() {
    productList = new ArrayList<Product>();
    init();
}

// 初始化7个商品对象
public void init() {
    productList.add(new Product('10001', 'iPhone 12', 7999.00, 50));
    productList.add(new Product('10002', '华为Mate 40', 6999.00, 60));
    productList.add(new Product('10003', '小米10', 3499.00, 100));
    productList.add(new Product('10004', 'OPPO Reno4', 2999.00, 80));
    productList.add(new Product('10005', 'vivo X50', 3398.00, 70));
    productList.add(new Product('10006', '三星Galaxy S21', 7699.00, 40));
    productList.add(new Product('10007', '魅族17', 3699.00, 90));
}

// 将当前所有的商品信息列表显示
public void proInfo() {
    System.out.println("商品编号\t商品名称\t\t商品价格\t商品库存");
    for(Product p : productList) {
        System.out.println(p.getId() + "\t" + p.getName() + "\t" + p.getPrice() + "\t" + p.getStock());
    }
}

// 添加一个商品,并能保存到集合中,更新显示当前所有商品信息
public void proAdd(Product p) {
    productList.add(p);
    proInfo();
}

// 修改商品,并能保存到集合中,更新显示当前所有商品信息
public void proModify(String id, Product p) {
    for(int i = 0; i < productList.size(); i++) {
        if(productList.get(i).getId().equals(id)) {
            productList.set(i, p);
            break;
        }
    }
    proInfo();
}

// 删除1条商品信息,并能保存到集合中,更新显示当前所有商品信息
public void proDel(String id) {
    for(int i = 0; i < productList.size(); i++) {
        if(productList.get(i).getId().equals(id)) {
            productList.remove(i);
            break;
        }
    }
    proInfo();
}

}


原文地址: https://www.cveoy.top/t/topic/oTQ4 著作权归作者所有。请勿转载和采集!

免费AI点我,无需注册和登录