1 public class Inventory {
5 // Transaction variables
16 public Inventory(int howmany) {
17 numitems = howmany;// howmany keeps track of the number of items
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;
27 // Get the item from hash
28 if (map.containsKey(name) == false) {
29 map.put(name, newitem);
31 ItemInfo i = (ItemInfo) map.get(name);
32 i.quantity += quantity;
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");
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;
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("");
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 s.write(sb.toString().getBytes());
85 StringBuffer sb=new StringBuffer("");
86 sb.append("Total value: ");
87 sb.append((new Integer(totalvalue)).toString());
89 s.write(sb.toString().getBytes());