// Transaction variables
int numitems;
- int current;//keeps track of current position
- int totalvalue;
+ HashMap map;
- // Item properties
- String item_name[];
- int item_quantity[];
- int item_price[];
-
// Constructor
public Inventory(){
- current = 0;
}
public Inventory(int howmany) {
numitems = howmany;// howmany keeps track of the number of items
// in the inventory
- current = 0;
- item_name = new String[howmany];
- item_quantity = new int [howmany];
- item_price = new int [howmany];
- for (int i = 0; i < howmany; i++) {
- item_name[i] = "";
- item_quantity[i] = 0;
- item_price[i] = 0;
- }
+ map = new HashMap(numitems);
}
// Add item to a list of inventory
public int additem(String name, int quantity, int price){
- //check current value
- if(current>=numitems){
- System.printString("Cannot add any further items");
- return -1;
- }
- // Search thru existing items
- for(int i=0; i<numitems;i++){
- if(item_name[i]== name){
- item_quantity[i]+= quantity;
- return 0;
+ ItemInfo newitem = new ItemInfo(quantity, price);
+
+ // Get the item from hash
+ if (map.containsKey(name) == false) {
+ if (map.size() > numitems) {
+ System.printString("Error - Items overflow");
+ return -1;
}
+ map.put(name, newitem);
+ } else {
+ ItemInfo i = map.get(name);
+ i.quantity += quantity;
+ i.price = price;
+ map.put(name, i);
}
- // Add new item if not found
- item_name[current]= name;
- item_quantity[current]= quantity;
- item_price[current]= price;
- current++;
+
return 0;
}
// Buy item from a given list of inventory
public int buyitem(String name, int quantity, int price){
- //Search through existing items
- for(int i=0; i<numitems;i++){
- if(item_name[i]== name){
- item_quantity[i]-= quantity;
- if (item_quantity[i]<=0){// if the quantity falls
- // to zero
- current--;
- }
- totalvalue = quantity*price;
- return 0;
+ if (map.containsKey(name) == false) {
+ System.printString(name);
+ System.printString("Error - Item does not exist");
+ return -1;
+ } else {
+ ItemInfo i = map.get(name);
+ if (i.quantity == 0) {
+ System.printString("Error - Item unavailable");
+ return -1;
}
+ i.quantity -= quantity;
+ map.put(name, i);
+ return 0;
}
- System.printString("Cannot find the item in the inventory");
- return -1;
-
}
//Display the inventory list
- public int inventory(){
- Integer tmp;
- for(int i=0; i<current; i++){
- System.printString(" The items are ");
- System.printString(item_name[i]);
- // System.printString(" The sale is ");
- System.printString(" The quantity of item is ");
- tmp = new Integer(item_quantity[i]);
- System.printString(tmp.toString());
- System.printString(" The price of item is ");
- tmp = new Integer(item_price[i]);
- System.printString(tmp.toString());
+ public String inventory(){
+ HashMapIterator i = new HashMapIterator(map, 0);
+ HashMapIterator j = new HashMapIterator(map, 1);
+ StringBuffer sb = new StringBuffer("");
+ while (i.hasNext() == true) {
+ Object o = i.next();
+ String name = o.toString();
+ System.printString(name);
+ ItemInfo oo = j.next();
+ sb.append(name);
+ sb.append(" ");
+ Integer q = new Integer(oo.quantity);
+ sb.append(q.toString());
+ sb.append(" ");
+ Integer p = new Integer(oo.price);
+ sb.append(p.toString());
+ sb.append("\n");
}
- return 0;
+ String item = new String(sb);
+ return item;
}
}
--- /dev/null
+class ItemInfo {
+ int quantity;
+ int price;
+ ItemInfo(int x, int y) {
+ quantity = x;
+ price = y;
+ }
+}
ServerSocket ss = new ServerSocket(9000);
System.printString("W> Creating ServerSocket\n");
Logger log = new Logger() {Initialize};
- Inventory inventorylist = new Inventory(3){TransInitialize};
+ Inventory inventorylist = new Inventory(4){TransInitialize};
taskexit(s {!initialstate}); /* Turns initial state flag off, so this task won't refire */
}
// Parse
web.parseTransaction();
// Check for the kind of operation
+ System.printString("DEBUG > After Web parse transaction call\n");
op = web.parsed[0].toCharArray();
if (op[0] == 'a') {
- Integer qty = new Integer(web.parsed[1]);
- Integer price = new Integer(web.parsed[2]);
- inventorylist.additem(web.parsed[0], qty.intValue(), price.intValue());
+ System.printString("DEBUG > Calling add transaction\n");
+ Integer qty = new Integer(web.parsed[2]);
+ Integer price = new Integer(web.parsed[3]);
+ int ret = inventorylist.additem(web.parsed[1], qty.intValue(), price.intValue());
+ if (ret == 0) {
+ web.httpresponse();
+ StringBuffer s = new StringBuffer("Added Item ");
+ s.append(web.parsed[1]);
+ s.append(" Quantity ");
+ s.append(web.parsed[2]);
+ s.append(" Price ");
+ s.append(web.parsed[3]);
+ s.append("\n");
+ String towrite = new String(s);
+ web.write(towrite.getBytes());
+ } else {
+ web.httpresponse();
+ String s = new String("Error encountered");
+ web.write(s.getBytes());
+ }
} else if (op[0] == 'b') {
- Integer qty = new Integer(web.parsed[1]);
- Integer price = new Integer(web.parsed[2]);
- inventorylist.buyitem(web.parsed[0], qty.intValue(), price.intValue());
+ System.printString("DEBUG > Calling buy transaction\n");
+ Integer qty = new Integer(web.parsed[2]);
+ Integer price = new Integer(web.parsed[3]);
+ int ret = inventorylist.buyitem(web.parsed[1], qty.intValue(), price.intValue());
+ if (ret == 0) {
+ web.httpresponse();
+ StringBuffer s = new StringBuffer("Bought item ");
+ s.append(web.parsed[1]);
+ s.append(" Quantity ");
+ s.append(web.parsed[2]);
+ s.append(" Price ");
+ s.append(web.parsed[3]);
+ s.append("<br />");
+ String towrite = new String(s);
+ web.write(towrite.getBytes());
+ } else {
+ web.httpresponse();
+ String s = new String("Error encountered");
+ web.write(s.getBytes());
+ }
} else if (op[0] == 'i') {
- inventorylist.inventory();
+ System.printString("DEBUG > Calling inventory transaction\n");
+ //inventorylist.inventory();
+ String towrite = inventorylist.inventory();
+ web.write(towrite.getBytes());
+
} else {
System.printString("T > Error - Unknown transaction\n");
}
-
//Invoke operations
web.close();
while ((ret = def_file.read(buf)) > 0) {// Read from file and write
// one byte at a time into the socket
-
byte tosend[] = new byte[ret];
for (int i = 0; i < ret; i++) {
tosend[i] = buf[i];
return false;
}
}
+
//Process special request
- public void parseTransaction() {
+ public int parseTransaction() {
//System.printString("DEBUG -> Inside parseTransaction");
int start = filename.indexOf('_');
String s = filename.subString(start+1);
int n = 4;
-/*
- if (s.startsWith("add")== true) {
- n = 4;
- }
- if (s.startsWith("buy")== true) {
- n = 3;
- }
- if (s.startsWith("inventory")== true) {
- n = 1;
- }
-*/
for (int i = 0; i < n; i++) {
int index;
if (i == n-1)
System.printString("\n Parsing : ");
System.printString(parsed[i]);
}
- return;
+ System.printString(" DEBUG > INSIDE PARSE TRANSACTION");
+
+ return 0;
}
}