5 public class Inventory {
6 // Transaction variables
17 public Inventory(int howmany) {
18 numitems = howmany;// howmany keeps track of the number of items
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;
28 // Get the item from hash
29 if (map.containsKey(name) == false) {
30 map.put(name, newitem);
32 ItemInfo i = (ItemInfo) map.get(name);
33 i.quantity += quantity;
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");
45 ItemInfo i = (ItemInfo) map.get(name);
46 if (i.quantity == 0) {
47 // System.printString("Error - Item unavailable");
50 if ((i.quantity-quantity) < 0 ) {
51 // System.printString("Error - Available qty is less: Cannot Buy\n");
54 i.quantity -= quantity;
56 balance+=quantity*i.price;
62 //Display the inventory list
63 public synchronized void inventory(Socket s){
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("");
72 String name = o.toString();
73 ItemInfo oo = (ItemInfo) j.next();
76 Integer q = new Integer(oo.quantity);
77 sb.append(q.toString());
79 Integer p = new Integer(oo.price);
80 sb.append(p.toString());
82 totalvalue+=oo.quantity*oo.price;
83 sockout.write(sb.toString().getBytes());
85 StringBuffer sb=new StringBuffer("");
86 sb.append("Total value: ");
87 sb.append((new Integer(totalvalue)).toString());
89 sockout.write(sb.toString().getBytes());
90 } catch (Exception e) {