start of new file
[IRC.git] / Robust / src / Benchmarks / WebServerJ / Inventory.java
1 import java.util.*;
2 import java.net.*;
3 import java.io.*;
4
5 public class Inventory {
6     // Transaction variables
7     int numitems;
8     HashMap map;
9     int balance;
10     
11     // Constructor
12     public Inventory(){
13         map = new HashMap();
14         balance=100000;
15     }
16     
17     public Inventory(int howmany) {
18         numitems = howmany;// howmany keeps track of the number of items 
19         // in the inventory
20         map = new HashMap();
21     }
22     
23     // Add item to a list of inventory
24     public synchronized void additem(String name, int quantity, int price){
25         ItemInfo newitem = new ItemInfo(quantity, price);
26         balance-=quantity*price;
27         
28         // Get the item from hash
29         if (map.containsKey(name) == false) {
30             map.put(name, newitem);
31         } else {
32             ItemInfo i = (ItemInfo) map.get(name);
33             i.quantity += quantity;
34             i.price = price;
35             map.put(name, i);
36         }
37     }   
38     
39     // Buy item from a given list of inventory  
40     public synchronized 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     }
61
62     //Display the inventory list
63     public synchronized void inventory(Socket s){
64         try {
65             OutputStream sockout=s.getOutputStream();
66         Iterator i = map.keySet().iterator();// Gets key from the hashmap= name of item
67         Iterator j = map.values().iterator();//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             sockout.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         sockout.write(sb.toString().getBytes());
90         } catch (Exception e) {
91             e.printStackTrace();
92         }       
93     }
94 }