start of new file
[IRC.git] / Robust / src / Benchmarks / Conglomerator / Tag / Inventory.java
1 public class Inventory {
2     flag TransInitialize;
3     // Transaction variables
4     int numitems;
5     HashMap map;
6     int balance;
7     
8     // Constructor
9     public Inventory(){
10         map = new HashMap();
11         balance=100000;
12     }
13     
14     public Inventory(int howmany) {
15         numitems = howmany;// howmany keeps track of the number of items 
16         // in the inventory
17         map = new HashMap();
18     }
19     
20     // Add item to a list of inventory
21     public int additem(String name, int quantity, int price){
22         ItemInfo newitem = new ItemInfo(quantity, price);
23         balance-=quantity*price;
24         
25         // Get the item from hash
26         if (map.containsKey(name) == false) {
27             map.put(name, newitem);
28         } else {
29             ItemInfo i = (ItemInfo) map.get(name);
30             i.quantity += quantity;
31             i.price = price;
32             map.put(name, i);
33         }
34         return 0;
35     }   
36     
37     // Buy item from a given list of inventory  
38     public int buyitem(String name, int quantity){
39         if (map.containsKey(name) == false) {
40             //          System.printString("Error - Item does not exist");
41             return -1;
42         } else {
43             ItemInfo i = (ItemInfo) map.get(name);
44             if (i.quantity == 0) {
45                 //                      System.printString("Error - Item unavailable");
46                 return -1;
47             }
48             if ((i.quantity-quantity) < 0 ) {
49                 //                      System.printString("Error - Available qty is less: Cannot Buy\n");
50                 return -1;
51             } else {
52                 i.quantity -= quantity;
53                 map.put(name, i);
54                 balance+=quantity*i.price;
55                 return i.price;
56             }
57         }
58         return 0;
59     }
60
61     //Display the inventory list
62     //Display the inventory list
63     public synchronized void inventory(Socket s){
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         int totalvalue=balance;
67         while (i.hasNext() == true) {
68             StringBuffer sb = new StringBuffer("");
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             s.write(sb.toString().getBytes());
82         }
83         StringBuffer sb=new StringBuffer("");
84         sb.append("Total value: ");
85         sb.append((new Integer(totalvalue)).toString());
86         sb.append("\n");
87         s.write(sb.toString().getBytes());
88     }   
89 }