1 public class Inventory {
5 // Transaction variables
14 public Inventory(int howmany) {
15 numitems = howmany;// howmany keeps track of the number of items
20 // Add item to a list of inventory
21 public synchronized int additem(String name, int quantity, int price){
22 ItemInfo newitem = new ItemInfo(quantity, price);
23 // Get the item from hash
24 if (map.containsKey(name) == false) {
25 map.put(name, newitem);
27 ItemInfo i = (ItemInfo) map.get(name);
28 i.quantity += quantity;
35 // Buy item from a given list of inventory
36 public synchronized int buyitem(String name, int quantity){
37 if (map.containsKey(name) == false) {
38 // System.printString("Error - Item does not exist");
41 ItemInfo i = (ItemInfo) map.get(name);
42 if (i.quantity == 0) {
43 // System.printString("Error - Item unavailable");
46 if ((i.quantity-quantity) < 0 ) {
47 // System.printString("Error - Available qty is less: Cannot Buy\n");
50 i.quantity -= quantity;
58 //Display the inventory list
59 public synchronized String inventory(){
60 HashMapIterator i = new HashMapIterator(map, 0);// Gets key from the hashmap= name of item
61 HashMapIterator j = new HashMapIterator(map, 1);//Gets the value from hashmap
62 StringBuffer sb = new StringBuffer("");
63 while (i.hasNext() == true) {
65 String name = o.toString();
66 ItemInfo oo = (ItemInfo) j.next();
69 Integer q = new Integer(oo.quantity);
70 sb.append(q.toString());
72 Integer p = new Integer(oo.price);
73 sb.append(p.toString());
76 String item = new String(sb);