Create a class called Storage that a hardware store might use to the actual storage of the * store. * A storage should include the following information as instance variables: * a map of all the items that the storage has (type HashMap). The key * is item’s description * In addition, provide a method named getItemQuantity(String description) that returns the * item that is being searched if there is any in storage. If the item is found in storage * returns the ItemQuantity otherwise returns null * Provide a method addItemQuantity(Item, Number) that saves input items to the storage. This * method should take as inputs the Item and the quantity of item. It should be a void method and should add the item to storage map
import java.util.HashMap;
public class Storage {
private HashMap<String, Item> storage;
public Storage() {
storage = new HashMap<>();
}
public Integer getItemQuantity(String description) {
Item item = storage.get(description);
if (item == null) {
return null;
}
return item.getQuantity();
}
public void addItemQuantity(Item item, Integer quantity) {
item.setQuantity(quantity);
storage.put(item.getDescription(), item);
}
}
Comments
Leave a comment