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