基本上我有三个类:Store 类、Stock 类和 GUI 类。创建商店时,我希望它拥有自己的 arraryList,以便我可以向其中添加多个库存对象。(通过 GUI 完成)。
我试图只包含所需的基本代码,(已删除 getter 方法、setter 方法、默认构造函数 compareTo 等)
下面是一些类的代码:
店铺
public class Store {
private int id;
private String name;
private String location;
private ArrayList <Stock> stockItems = new ArrayList<Stock> ();
public Store(int idIn, String nameIn, String locationIn) {
id = idIn;
name = nameIn;
location = locationIn;
stockItems = new ArrayList<Stock> ();
}
//to add stock items to a store?
public void addStockItem(Stock s) {
stockItems.add(s);
}
}
库存
public class Stock {
private int id;
private String name;
private double price;
private int units;
public Stock(int idIn, String nameIn, double priceIn, int unitsIn) {
id = idIn;
name = nameIn;
price = priceIn;
units = unitsIn;
}
}
GUI:(只是与我的问题相关的代码)
int indexStore = lst_Store.getSelectedIndex(); //store that the user selects from list
int indexStock = lst_Stock.getSelectedIndex(); //stock item selected by user
//get the specific stock details
Stock s = stocks.get(indexStock);
Store z = new Store(); //so i can call method below
z.addStockItem(s);
真正让我感到困惑的是 GUI 代码,基本上我想将所选的库存添加到所选的商店。完成后,您将如何访问特定商店的 arrayList 中的信息?
非常感谢。