adding scripts for automating results
[IRC.git] / Robust / src / Tests / Inventory.java
1 public class Inventory {
2         // Inventory flags
3         flag TransInitialize;
4
5         // Transaction variables
6         int numitems;
7         HashMap map;
8         
9         // Constructor
10         public Inventory(){
11                 map = new HashMap();
12         }
13
14         public Inventory(int howmany) {
15                 numitems = howmany;// howmany keeps track of the number of items 
16                                    // in the inventory
17                 map = new HashMap();
18         }
19
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                 // Get the item from hash
24                 if (map.containsKey(name) == false) {
25                         map.put(name, newitem);
26                 } else {
27                         ItemInfo i = (ItemInfo) map.get(name);
28                         i.quantity += quantity;
29                         i.price = price;
30                         map.put(name, i);
31                 }
32                 return 0;
33         }       
34
35         // Buy item from a given list of inventory      
36         public int buyitem(String name, int quantity){
37                 if (map.containsKey(name) == false) {
38                         System.printString("Error - Item does not exist");
39                         return -1;
40                 } else {
41                         ItemInfo i = (ItemInfo) map.get(name);
42                         if (i.quantity == 0) {
43                                 System.printString("Error - Item unavailable");
44                                 return -1;
45                         }
46                         if ((i.quantity-quantity) < 0 ) {
47                                 System.printString("Error - Available qty is less: Cannot Buy\n");
48                                 return -1;
49                         } else {
50                                 i.quantity -= quantity;
51                                 map.put(name, i);
52                                 return i.price;
53                         }
54                 }
55                 return 0;
56         }
57
58         //Display the inventory list
59         public String inventory(){
60                 HashMapIterator i = new HashMapIterator(map, 0);
61                 HashMapIterator j = new HashMapIterator(map, 1);
62                 StringBuffer sb = new StringBuffer("");
63                 while (i.hasNext() == true) {
64                         Object o = i.next();
65                         String name = o.toString();
66                         ItemInfo oo = (ItemInfo) j.next();
67                         sb.append(name);
68                         sb.append(" ");
69                         Integer q = new Integer(oo.quantity);
70                         sb.append(q.toString());
71                         sb.append(" ");
72                         Integer p = new Integer(oo.price);
73                         sb.append(p.toString());
74                         sb.append("\n");
75                 }
76                 String item = new String(sb);   
77                 return item;    
78         }       
79 }