2

I have a product with following format:

public Item(String barcode, double price, int inStock) {
    if (barcode == null) {
        barcode = "";
    }
    if (price < 0) {
        price = 0;
    }
    if (inStock < 0) {
        inStock = 0;
    }
    this.barcode = barcode;
    this.price = price;
    this.inStock = inStock;
}

And an ArrayList to store the items in another class:

public class Inventory {
    private ArrayList<Item> currentInventory;

    public Inventory() {
        this.currentInventory = new ArrayList<Inventory>();
    }

    /**
     * Adds default products to the inventory
     */
    public void inventoryDefault() {    
        this.currentInventory("JMS343", 100.00, 5);
        this.currentInventory("RQ090", 50.00, 20);  
    }

I cannot figure out how to add 2 items as defaults, or always in the currentInventory, until removed by the user. I have also tried:

public void inventoryDefault() {    
    this.currentInventory.add(JMS343, 100.00, 5);
    this.currentInventory.add(RQ090, 50.00, 20);    
}

This, however, shows an error that JMS343 and RQ090 cannot be resolved to variables. I thought I was creating them at this point, since it is just a String. Any help would be great. Thanks!

Working solution as shown below:

        public void inventoryDefault() {    
            this.currentInventory.add(new Item("JMS343", 100.00, 5));
            this.currentInventory.add(new Item("RQ090", 50.00, 20));    
        }
4
  • 1
    You forgot the surrounding double quotes. " JMS343 " Commented Dec 5, 2015 at 18:36
  • You also forgot the Item constructor. Commented Dec 5, 2015 at 18:38
  • I did the double quotes in my first try. Tried it with and without and showed that in my examples. Commented Dec 5, 2015 at 18:45
  • Forgive me if I'm wrong, but isn't the constructor for Items the "public Items(x, x, x)" at the beginning of the example? Commented Dec 5, 2015 at 18:46

2 Answers 2

2

do this to the constructor of the Inventory class

public Inventory() {
        this.currentInventory = new ArrayList<Inventory>();
        this.currentInventory.add(new Item("JMS343", 100.00, 5));
        this.currentInventory.add(new Item("RQ090", 50.00, 20);
    }
Sign up to request clarification or add additional context in comments.

1 Comment

This worked exactly as asked for! I added it to my code above. Will accept as answer when it allows me to.
2

You need to construct Item objects with the constructor to add them to the ArrayList. E.g.

public void inventoryDefault() {    
    currentInventory.add(new Item("JMS343", 100.00, 5);
    currentInventory.add(new Item("RQ090", 50.00, 20);
}

Or add an appropriate function to Inventory that may also be useful otherwise :

public void addItem(String barcode, double price, int inStock) {
    Item item = new Item(barcode, price, inStock);
    currentInventory.add(item);
}

public void inventoryDefault() {    
    addItem("JMS343", 100.00, 5);
    addItem("RQ090", 50.00, 20);
}

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.