@Test public void testAddItem() { final Item[] item = new Item[inventory.getCapacity()]; for (int i = 0; i< inventory.getCapacity(); i++) { item[i] = MockFactory.mockItem(i + 1, 1); assertEquals(0, inventory.addItem(item[i])); } final Item newItem = mock(Item.class); when(newItem.getAmount()).thenReturn(1); assertEquals(1, inventory.addItem(newItem)); when(newItem.getId()).thenReturn(2); assertEquals(0, inventory.addItem(newItem)); assertEquals(2, inventory.getItemAmount(newItem)); inventory.removeItem(0); assertEquals(0, inventory.addItem(newItem)); assertEquals(3, inventory.getItemAmount(newItem)); inventory.removeItem(0); inventory.addItem(item[0]); when(newItem.getAmount()).thenReturn(9998); assertThat(inventory.addItem(newItem), greaterThan(0)); verify(newItem).addAmount(-9997); }
@Override public int addItem(final Item item) { int i; if ((i = hasItem(item)) != -1) { int amount = item.getAmount(); int newAmount, oldAmount; final int id = item.getId(); for (; i < inventory.length; i++) { if (inventory[i]!= null && inventory[i].getId() == id) { oldAmount = inventory[i].getAmount(); newAmount = inventory[i].addAmount(amount); amount += oldAmount - newAmount; if (amount == 0) { return 0; } } } item.addAmount(amount - item.getAmount()); } for (i = 0; i < inventory.length; i++) { if (inventory[i] == null) { inventory[i] = item; return 0; } } return item.getAmount(); }
InventoryImpl implements Inventory { @Override public int addItem(final Item item) { int i; if ((i = hasItem(item)) != -1) { int amount = item.getAmount(); int newAmount, oldAmount; final int id = item.getId(); for (; i < inventory.length; i++) { if (inventory[i]!= null && inventory[i].getId() == id) { oldAmount = inventory[i].getAmount(); newAmount = inventory[i].addAmount(amount); amount += oldAmount - newAmount; if (amount == 0) { return 0; } } } item.addAmount(amount - item.getAmount()); } for (i = 0; i < inventory.length; i++) { if (inventory[i] == null) { inventory[i] = item; return 0; } } return item.getAmount(); } }
InventoryImpl implements Inventory { @Override public int addItem(final Item item) { int i; if ((i = hasItem(item)) != -1) { int amount = item.getAmount(); int newAmount, oldAmount; final int id = item.getId(); for (; i < inventory.length; i++) { if (inventory[i]!= null && inventory[i].getId() == id) { oldAmount = inventory[i].getAmount(); newAmount = inventory[i].addAmount(amount); amount += oldAmount - newAmount; if (amount == 0) { return 0; } } } item.addAmount(amount - item.getAmount()); } for (i = 0; i < inventory.length; i++) { if (inventory[i] == null) { inventory[i] = item; return 0; } } return item.getAmount(); } InventoryImpl();  InventoryImpl(int slots);  InventoryImpl(Item[] inventory); }
InventoryImpl implements Inventory { @Override public int addItem(final Item item) { int i; if ((i = hasItem(item)) != -1) { int amount = item.getAmount(); int newAmount, oldAmount; final int id = item.getId(); for (; i < inventory.length; i++) { if (inventory[i]!= null && inventory[i].getId() == id) { oldAmount = inventory[i].getAmount(); newAmount = inventory[i].addAmount(amount); amount += oldAmount - newAmount; if (amount == 0) { return 0; } } } item.addAmount(amount - item.getAmount()); } for (i = 0; i < inventory.length; i++) { if (inventory[i] == null) { inventory[i] = item; return 0; } } return item.getAmount(); } InventoryImpl();  InventoryImpl(int slots);  InventoryImpl(Item[] inventory); @Override int addItem(final Item item); @Override Item getItem(int slot); @Override boolean hasFreeSlots(); @Override int hasItem(Item item); @Override Item removeItem(int slot); @Override Item removeItem(Item item); @Override Item removeItem(int slot, int amount); @Override int getCapacity(); @Override int getItemAmount(Item item); @Override void cleanup(); @Override void setCapacity(int capacity); @Override Iterator<Item> iterator(); }
InventoryImpl implements Inventory { @Override public int addItem(final Item item) { int i; if ((i = hasItem(item)) != -1) { int amount = item.getAmount(); int newAmount, oldAmount; final int id = item.getId(); for (; i < inventory.length; i++) { if (inventory[i]!= null && inventory[i].getId() == id) { oldAmount = inventory[i].getAmount(); newAmount = inventory[i].addAmount(amount); amount += oldAmount - newAmount; if (amount == 0) { return 0; } } } item.addAmount(amount - item.getAmount()); } for (i = 0; i < inventory.length; i++) { if (inventory[i] == null) { inventory[i] = item; return 0; } } return item.getAmount(); } InventoryImpl();  InventoryImpl(int slots);  InventoryImpl(Item[] inventory); @Override int addItem(final Item item); @Override Item getItem(int slot); @Override boolean hasFreeSlots(); @Override int hasItem(Item item); @Override Item removeItem(int slot); @Override Item removeItem(Item item); @Override Item removeItem(int slot, int amount); @Override int getCapacity(); @Override int getItemAmount(Item item); @Override void cleanup(); @Override void setCapacity(int capacity); @Override Iterator<Item> iterator();  }