如何给每个商店对象它自己的库存数组? [英] How to give each store object it's own arrayList of stock?

查看:27
本文介绍了如何给每个商店对象它自己的库存数组?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

发现这个很难,基本上我有三个类:Store 类、Stock 类,然后是 GUI 类.创建商店时,我希望它拥有自己的 arraryList,以便我可以向其中添加多个库存对象.(通过 GUI 完成).

Finding this one tough, basically I have three classes: Store class, Stock class and then the class for the GUI. When a store is created, I want it to have it's very own arraryList so that I can add multiple stock objects to it. (done through the GUI).

我尝试只包含所需的基本代码,(已删除 getter 方法、setter 方法、默认构造函数 compareTo 等)

I've tried to only include the basic code that is needed, (have deleted the getter methods, setter methods, default constructors compareTo etc.)

这是一些类的代码(很可能是错误的)

Here's some of the code of the classes (that could very well be wrong)

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;
    ArrayList <Stock> 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 中,要从 GUI 将库存商品添加到特定商店,我应该调用什么?

Can anyone tell me if I'm on the right track? In the GUI, what would I call to add a stock item in to a specific store from the GUI?

谢谢.

推荐答案

Store的构造函数中,你有

ArrayList <Stock> stockItems = ...

这实际上是在创建一个局部变量 stockItems,而不是更改字段.为了使它工作,只需

That is actually creating a local variable stockItems, instead of changing the field. To make it work use just

stockItems = ...

这篇关于如何给每个商店对象它自己的库存数组?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆