1 public class Inventory {
3 // Transaction variables
14 public Inventory(int howmany) {
15 numitems = howmany;// howmany keeps track of the number of items
20 // Add item to a list of inventory
21 public int additem(String name, int quantity, int price){
22 ItemInfo newitem = new ItemInfo(quantity, price);
23 balance-=quantity*price;
25 // Get the item from hash
26 if (map.containsKey(name) == false) {
27 map.put(name, newitem);
29 ItemInfo i = (ItemInfo) map.get(name);
30 i.quantity += quantity;
37 // Buy item from a given list of inventory
38 public int buyitem(String name, int quantity){
39 if (map.containsKey(name) == false) {
40 // System.printString("Error - Item does not exist");
43 ItemInfo i = (ItemInfo) map.get(name);
44 if (i.quantity == 0) {
45 // System.printString("Error - Item unavailable");
48 if ((i.quantity-quantity) < 0 ) {
49 // System.printString("Error - Available qty is less: Cannot Buy\n");
52 i.quantity -= quantity;
54 balance+=quantity*i.price;
61 //Display the inventory list
62 //Display the inventory list
63 public synchronized void inventory(Socket s){
64 HashMapIterator i = new HashMapIterator(map, 0);// Gets key from the hashmap= name of item
65 HashMapIterator j = new HashMapIterator(map, 1);//Gets the value from hashmap
66 int totalvalue=balance;
67 while (i.hasNext() == true) {
68 StringBuffer sb = new StringBuffer("");
70 String name = o.toString();
71 ItemInfo oo = (ItemInfo) j.next();
74 Integer q = new Integer(oo.quantity);
75 sb.append(q.toString());
77 Integer p = new Integer(oo.price);
78 sb.append(p.toString());
80 totalvalue+=oo.quantity*oo.price;
81 s.write(sb.toString().getBytes());
83 StringBuffer sb=new StringBuffer("");
84 sb.append("Total value: ");
85 sb.append((new Integer(totalvalue)).toString());
87 s.write(sb.toString().getBytes());