patternjavaMinor
Inventory of objects with item types and quantities
Viewed 0 times
itemquantitiesobjectswithinventorytypesand
Problem
I am thinking about how an inventory could be designed. The requirements for this inventory will be the following:
The initial design I have come up with is the following:
Item.java
and Inventory.java :
```
public class Inventory {
private ArrayList items = new ArrayList();
public Inventory() {
this.items.add(new Item("Book", "4877326352"));
this.items.add(new Item("Pen", "21366363636"));
}
public void increaseQuantity(String itemName) {
for (Item item : this.items) {
if (itemName.equals(item.getName())) {
item.add();
return;
}
}
}
public void decreaseQuantity(String itemName) {
for (Item item : this.items) {
if (itemName.equals(item.getName())) {
if (item.getQuantity()>=0) {
item.remove();
}
return;
}
}
}
- There will be predefined item types in the inventory.
- The user can define new item types and add them to the inventory.
- In the interaction with the GUI the user will be shown the item types contained in the inventory and he will click a "+" or a "-" to increase/decrease the quantity of that item.
The initial design I have come up with is the following:
Item.java
public class Item {
private String name;
private String itemId;
private int quantity;
public Item(String name, String itemId) {
this.name=name;
this.itemId=itemId;
this.quantity=0;
}
public void add() {
this.quantity++;
}
public void remove() {
this.quantity--;
}
public String getName() {
return this.name;
}
public String getItemId() {
return this.itemId;
}
public int getQuantity() {
return this.quantity;
}
public String toString() {
String s="";
s+=" name = "+this.name;
s+=" , id = "+this.itemId;
s+=" , quantity = "+this.quantity;
return s;
}
}and Inventory.java :
```
public class Inventory {
private ArrayList items = new ArrayList();
public Inventory() {
this.items.add(new Item("Book", "4877326352"));
this.items.add(new Item("Pen", "21366363636"));
}
public void increaseQuantity(String itemName) {
for (Item item : this.items) {
if (itemName.equals(item.getName())) {
item.add();
return;
}
}
}
public void decreaseQuantity(String itemName) {
for (Item item : this.items) {
if (itemName.equals(item.getName())) {
if (item.getQuantity()>=0) {
item.remove();
}
return;
}
}
}
Solution
Yes, having a quantity field in
The reason is that it is the wrong abstraction. If you pick up and hold ten pebbles in your hand, the number ten is a property of your hand (because it is how many things it is holding), not the pebble which is just a pebble.
Consider if you drop all the pebbles, now the quantity of pebbles in your hand changes to 0. But if the quantity was a property of the pebble, and you dropped all the pebbles how would you model that would each of the pebbles have a quantity of 0 now?
What if you hand the pebbles over to some one else? Then you would change the quantity in respective hand, but if the quantity was part of the pebble you couldn't model this transaction at all.
Similarly the quantity should be a property of the inventory (how many of X does it contain), not of the
Somehow this makes me think of Database Normal Forms.
Simply have a
Note that you will need to implement
Item is bad design.The reason is that it is the wrong abstraction. If you pick up and hold ten pebbles in your hand, the number ten is a property of your hand (because it is how many things it is holding), not the pebble which is just a pebble.
Consider if you drop all the pebbles, now the quantity of pebbles in your hand changes to 0. But if the quantity was a property of the pebble, and you dropped all the pebbles how would you model that would each of the pebbles have a quantity of 0 now?
What if you hand the pebbles over to some one else? Then you would change the quantity in respective hand, but if the quantity was part of the pebble you couldn't model this transaction at all.
Similarly the quantity should be a property of the inventory (how many of X does it contain), not of the
Item which is just an item.Somehow this makes me think of Database Normal Forms.
Simply have a
Map with Item as key and an Integer quantity as value. This solves your duplicate items problem as well.Map inv = new HasMap<>();
// add x of item ( works for new items too )
inv.put(item, inv.getOrDefault(item, 0) + x);Note that you will need to implement
equals and hashCode for Item.Code Snippets
Map<Item, Integer> inv = new HasMap<>();
// add x of item ( works for new items too )
inv.put(item, inv.getOrDefault(item, 0) + x);Context
StackExchange Code Review Q#148821, answer score: 2
Revisions (0)
No revisions yet.