1 public class Inventory {
5 // Transaction variables
15 public Inventory(int howmany) {
16 numitems = howmany;// howmany keeps track of the number of items
21 // Add item to a list of inventory
22 public int additem(String name, int quantity, int price){
23 ItemInfo newitem = new ItemInfo(quantity, price);
24 balance-=quantity*price;
26 // Get the item from hash
27 if (map.containsKey(name) == false) {
28 map.put(name, newitem);
30 ItemInfo i = (ItemInfo) map.get(name);
31 i.quantity += quantity;
38 // Buy item from a given list of inventory
39 public int buyitem(String name, int quantity){
40 if (map.containsKey(name) == false) {
41 // System.printString("Error - Item does not exist");
44 ItemInfo i = (ItemInfo) map.get(name);
45 if (i.quantity == 0) {
46 // System.printString("Error - Item unavailable");
49 if ((i.quantity-quantity) < 0 ) {
50 // System.printString("Error - Available qty is less: Cannot Buy\n");
53 i.quantity -= quantity;
55 balance+=quantity*i.price;
62 //Display the inventory list
63 public String inventory(){
64 HashMapIterator i = new HashMapIterator(map, 0);// Gets key from the hashmap= name of item
65 HashMapIterator j = new HashMapIterator(map, 1);//Gets the value from hashmap
66 StringBuffer sb = new StringBuffer("");
67 int totalvalue=balance;
68 while (i.hasNext() == true) {
70 String name = o.toString();
71 ItemInfo oo = (ItemInfo) j.next();
74 Integer q = new Integer(oo.quantity);
75 sb.append(q.toString());
77 Integer p = new Integer(oo.price);
78 sb.append(p.toString());
80 totalvalue+=oo.quantity*oo.price;
82 sb.append("Total value: ");
83 sb.append(new Integer(totalvalue));