1 public class Inventory {
2 // Transaction variables
13 public Inventory(int howmany) {
14 numitems = howmany;// howmany keeps track of the number of items
19 // Add item to a list of inventory
20 public int additem(String name, int quantity, int price){
21 ItemInfo newitem = new ItemInfo(quantity, price);
22 balance-=quantity*price;
24 // Get the item from hash
25 if (map.containsKey(name) == false) {
26 map.put(name, newitem);
28 ItemInfo i = (ItemInfo) map.get(name);
29 i.quantity += quantity;
36 // Buy item from a given list of inventory
37 public int buyitem(String name, int quantity){
38 if (map.containsKey(name) == false) {
39 // System.printString("Error - Item does not exist");
42 ItemInfo i = (ItemInfo) map.get(name);
43 if (i.quantity == 0) {
44 // System.printString("Error - Item unavailable");
47 if ((i.quantity-quantity) < 0 ) {
48 // System.printString("Error - Available qty is less: Cannot Buy\n");
51 i.quantity -= quantity;
53 balance+=quantity*i.price;
60 //Display the inventory list
61 //Display the inventory list
62 public synchronized void inventory(Socket s){
63 HashMapIterator i = new HashMapIterator(map, 0);// Gets key from the hashmap= name of item
64 HashMapIterator j = new HashMapIterator(map, 1);//Gets the value from hashmap
65 int totalvalue=balance;
66 while (i.hasNext() == true) {
67 StringBuffer sb = new StringBuffer("");
69 String name = o.toString();
70 ItemInfo oo = (ItemInfo) j.next();
73 Integer q = new Integer(oo.quantity);
74 sb.append(q.toString());
76 Integer p = new Integer(oo.price);
77 sb.append(p.toString());
79 totalvalue+=oo.quantity*oo.price;
80 s.write(sb.toString().getBytes());
82 StringBuffer sb=new StringBuffer("");
83 sb.append("Total value: ");
84 sb.append((new Integer(totalvalue)).toString());
86 s.write(sb.toString().getBytes());