0

I have a Class named Item.

public class Item {

private int code;
private String name;
private double price;
private int quantity;

public Item(int code, String name, double price, int quantity) {
    setCode(code);
    setName(name);
    setPrice(price);
    setQuantity(quantity);
}
//Item getters and setters

And I have a Class named Stock that creates an ArrayList of Items.

public class Stock {
private ArrayList<Item> stock;

public Stock() {
    stock = new ArrayList<Item>();
}

public ArrayList<Item> getStock() {
    return stock;
}

I also have a Class named ItemRegister that adds an Item to the Stock.

public class ItemRegister extends Stock {

public void registerItem(String name, double price) {
    getStock().add(new Item(setItemCode(), name, price, 0));
}

private int setItemCode() {
    return getStock().size() + 1;
}

I'm using unit tests to see if I did add an Item to the Stock.

public class ItemRegisterTest {

@Test
public void testIfHasRegisteredItemInStock() {
    Stock s = new Stock();
    assertTrue(s.getStock().size() == 0);
    ItemRegister i = new ItemRegister();        
    i.registerItem("Oleo", 20.0);
    assertTrue(s.getStock().size() == 1);
}

}

When I run these tests it is returning an error. On the second assertTrue, if I test with the object i it'll return true but what I want is to add to Stock and not ItemRegister because if later on I wanna consult Stock I'll call Stock and not ItemRegister.

9
  • 3
    Do you think your s object is in any way related to your i object? Why or why not? Commented Jan 29, 2019 at 0:39
  • Just check i instead of s: assertTrue(i.getStock().size() == 1); Commented Jan 29, 2019 at 0:45
  • @SotiriosDelimanolis not really. Because i'm instantianting a new object Commented Jan 29, 2019 at 0:49
  • Change the s by i like this assertTrue(i.getStock().size() == 1); s has zero elements. Commented Jan 29, 2019 at 0:50
  • Hi @SergeiSirik Later on I wanna try and make a selling class and I wanna consult from Stock but it is not adding how I wanted it Commented Jan 29, 2019 at 0:51

2 Answers 2

1

From the comments, what you can do is maybe changing the parent-child relationship and having the ItemRegister holding an instance of a Stock object. So, you can modify the ItemRegister class like this:

public class ItemRegister  {
    Stock s;
    public ItemRegister(Stock s) {
        this.s = s;
    }

    public void registerItem(String name, double price) {
        s.getStock().add(new Item(setItemCode(), name, price, 0));
    }

    private int setItemCode() {
        return s.getStock().size() + 1;
    }
}

Then, the test code you originally wrote will be true with a slight modification ItemRegister i = new ItemRegister(s);, and you can work on one instance of Stock object.

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

Comments

0

I want is to add to Stock and not ItemRegister because if later on I wanna consult Stock I'll call Stock and not ItemRegister.

Then you need to wrap Stock into ItemRegister.

public class ItemRegister  {
    Stock stock;
    public ItemRegister(Stock stock) {
        this.stock = stock;
    }

    public void registerItem(String name, double price) {
        stock.getStock().add(new Item(setItemCode(), name, price, 0));
    }

    private int setItemCode() {
        return stock.getStock().size() + 1;
    }
}

Use it in your unit tests like this:

public class ItemRegisterTest {
    @Test
    public void testIfHasRegisteredItemInStock() {
        Stock s = new Stock();
        assertTrue(s.getStock().size() == 0);
        ItemRegister i = new ItemRegister(s);        
        i.registerItem("Oleo", 20.0);
        assertTrue(s.getStock().size() == 1);
    }
}

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.