如何给每个存储对象它自己的股票的ArrayList? [英] How to give each store object it's own arrayList of stock?

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

问题描述

查找这一个艰难的,基本上我有三类:存储类,股票类,然后在类的图形用户界面。当创建一个商店,我希望它有它自己的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).

我试着只包含基本的code,它是必要的,(删除了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.)

下面是一些类的code(即很可能是错误的)

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?

感谢。

推荐答案

商店的构造,你有

ArrayList <Stock> stockItems = ...

这实际上是创建一个局部变量 stockItems ,而不是改变的领域。为了让工作中使用刚刚

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

stockItems = ...

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

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