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