0

I have these class on my package:

1. Class Goods with attributes (name, price, id) //Goods id is unique, but the name can be the same
2. Class Storage with attributes (Goods gArray[3])
3. Class Store with attributes (name, ArrayList<Storage>) //Store name is unique
4. Class StoreSystem with attributes (ArrayList<Store>)

I want to insert Goods into Storage which belong to certain Store. I already succeed in inserting the Store to ArrayList, but haven't found the way to insert the Goods.

Here's the code for adding the store:

public String addStore(String storeName) {
    String output = "";
    if(storeCheck(storeName)) { //storeCheck used to check whether the store name exist/not.
        output = "store already exist!";
    }
    else {
        Store s1 = new Store();
        Storage st1 = new Storage();
        s1.setStoreName(storeName);
        s1.setStorageList(null);
        st1.setGArray(null);
        listOfStore.add(s1);
        listOfStorage.add(st1);
        output = "Store added";
    }
    return output;
}
2
  • Please clarify whether stores are unique or not and if yes then are their unique or not Commented Oct 24, 2019 at 19:02
  • Also, is there any field using which you can uniquely identify a particular Storage in a Store? Something like a storageId or something? Commented Oct 24, 2019 at 19:08

3 Answers 3

1

You could create a method in the Storage class that:

  • either takes an index of the array and Goods as arguments and places the Goods in the array at that index (if you're interested in full control of where everything goes)
  • or just Goods as argument and decides where to put them internally (if you do not care which index in the array the goods go to)

Does that make sense?

Sign up to request clarification or add additional context in comments.

2 Comments

This actually make sense, I always thought I should put addGoods() method at StoreSystem class. However, if I put the addGoods() method at the Storage class, will the Storage which have those Goods be stored to the Store class as well? Or should I create another method in the StoreSystem Class to add the Storage?
I think you should keep the responsibilities of your classes clear: keep the Goods management in Storage class and the Storage management in the Store class.
1

I believe is just this, using list as intermediate:

    public void addGoods(Goods g) {

        List<Goods> storageList = Arrays.asList(this.getGoods());
        storageList.add(g);
        this.setGoods(storageList.toArray());
    }

get and set as usual, and you will need to control the size.

3 Comments

From OP's description, Storage is not a List of Goods, but an array of fixed size 3.
I find it weird as well. But those are the requirements. Actually this is my campus assignment and that particular requirement also made me confused. Sorry.
True, fixed to array
0

Make a POJO called Storage with the field Goods gArray[3]. Then have a method to add Storage in Store class (it should be as simple as using storeArrayList.add(new Storage()); and pass 3 parameters of type Goods to it). And there you go. You have a Storage and Goods in Storage.

Now properly looking up for a particular Storage in a Store without assigning an index to a particular Storage will be a bit of a bother. Ideally, every Storage object should have an id field.

The code will look something like:
The Store add method:

<return-type> addToStore(Goods[] goods) {
 storageArray.add(new Storage(goods));
}  

And then the Storage class will look something like:

class Storage {
 private final Goods gArray[3];

 public Storage(final Goods gArray) {
  this.gArray = gArray;
 }

 // getters
}  

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.