check in changes
[IRC.git] / Robust / src / Benchmarks / WebServer / Inventory.java
1 public class Inventory {
2     // Inventory flags
3     flag TransInitialize;
4     
5     // Transaction variables
6     int numitems;
7     HashMap map;
8     int balance=100000;
9     
10     // Constructor
11     public Inventory(){
12         map = new HashMap();
13     }
14     
15     public Inventory(int howmany) {
16         numitems = howmany;// howmany keeps track of the number of items 
17         // in the inventory
18         map = new HashMap();
19     }
20     
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;
25         
26         // Get the item from hash
27         if (map.containsKey(name) == false) {
28             map.put(name, newitem);
29         } else {
30             ItemInfo i = (ItemInfo) map.get(name);
31             i.quantity += quantity;
32             i.price = price;
33             map.put(name, i);
34         }
35         return 0;
36     }   
37     
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");
42             return -1;
43         } else {
44             ItemInfo i = (ItemInfo) map.get(name);
45             if (i.quantity == 0) {
46                 //                      System.printString("Error - Item unavailable");
47                 return -1;
48             }
49             if ((i.quantity-quantity) < 0 ) {
50                 //                      System.printString("Error - Available qty is less: Cannot Buy\n");
51                 return -1;
52             } else {
53                 i.quantity -= quantity;
54                 map.put(name, i);
55                 balance+=quantity*i.price;
56                 return i.price;
57             }
58         }
59         return 0;
60     }
61
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) {
69             Object o = i.next();
70             String name = o.toString();
71             ItemInfo oo = (ItemInfo) j.next();
72             sb.append(name);
73             sb.append(" ");
74             Integer q = new Integer(oo.quantity);
75             sb.append(q.toString());
76             sb.append(" ");
77             Integer p = new Integer(oo.price);
78             sb.append(p.toString());
79             sb.append("\n");
80             totalvalue+=oo.quantity*oo.price;
81         }
82         sb.append("Total value: ");
83         sb.append(new Integer(totalvalue));
84         sb.append("\n");
85         return sb.toString();   
86     }   
87 }