changes to build script to increase java heap memory
[IRC.git] / Robust / src / Benchmarks / WebServerJava / 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 synchronized 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 synchronized 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     public synchronized void inventory(Socket s){
62         HashMapIterator i = new HashMapIterator(map, 0);// Gets key from the hashmap= name of item
63         HashMapIterator j = new HashMapIterator(map, 1);//Gets the value from hashmap 
64         int totalvalue=balance;
65         while (i.hasNext() == true) {
66             StringBuffer sb = new StringBuffer("");
67             Object o = i.next();
68             String name = o.toString();
69             ItemInfo oo = (ItemInfo) j.next();
70             sb.append(name);
71             sb.append(" ");
72             Integer q = new Integer(oo.quantity);
73             sb.append(q.toString());
74             sb.append(" ");
75             Integer p = new Integer(oo.price);
76             sb.append(p.toString());
77             sb.append("\n");
78             totalvalue+=oo.quantity*oo.price;
79             s.write(sb.toString().getBytes());
80         }
81         StringBuffer sb=new StringBuffer("");
82         sb.append("Total value: ");
83         sb.append((new Integer(totalvalue)).toString());
84         sb.append("\n");
85         s.write(sb.toString().getBytes());
86     }   
87 }