// Constructor
public Inventory(){
+ map = new HashMap();
}
public Inventory(int howmany) {
numitems = howmany;// howmany keeps track of the number of items
// in the inventory
- map = new HashMap(numitems);
+ map = new HashMap();
}
// Add item to a list of inventory
public int additem(String name, int quantity, int price){
ItemInfo newitem = new ItemInfo(quantity, price);
+ //System.printString("DEBUG -> Inside add item method ");
// Get the item from hash
if (map.containsKey(name) == false) {
- if (map.size() > numitems) {
- System.printString("Error - Items overflow");
- return -1;
- }
+ //System.printString("DEBUG -> Inside if of addmethod");
+// if (map.size() > numitems) {
+// System.printString("Error - Items overflow");
+// return -1;
+// }
map.put(name, newitem);
+ //System.printString("DEBUG -> after if ");
} else {
+ //System.printString("DEBUG -> Inside else ");
ItemInfo i = map.get(name);
i.quantity += quantity;
i.price = price;
map.put(name, i);
}
-
return 0;
}
// Buy item from a given list of inventory
- public int buyitem(String name, int quantity, int price){
+ public int buyitem(String name, int quantity){
if (map.containsKey(name) == false) {
System.printString(name);
System.printString("Error - Item does not exist");
}
i.quantity -= quantity;
map.put(name, i);
- return 0;
+ return i.price;
}
}
ServerSocket ss = new ServerSocket(9000);
System.printString("W> Creating ServerSocket\n");
Logger log = new Logger() {Initialize};
- Inventory inventorylist = new Inventory(4){TransInitialize};
+ Inventory inventorylist = new Inventory(){TransInitialize};
taskexit(s {!initialstate}); /* Turns initial state flag off, so this task won't refire */
}
//Transaction on Inventory
task Transaction(WebServerSocket web{TransPending}, Inventory inventorylist{TransInitialize}){ //Task for WebServerTransactions
System.printString("T > Inside Transaction\n");
- char[] op = new char[10];
// Parse
- web.parseTransaction();
+ int op = 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') {
+ if (op == 0 ) { /* Add */
System.printString("DEBUG > Calling add transaction\n");
+ System.printString(web.parsed[2]);
+ System.printString(web.parsed[3]);
Integer qty = new Integer(web.parsed[2]);
Integer price = new Integer(web.parsed[3]);
+ System.printString(web.parsed[1]);
int ret = inventorylist.additem(web.parsed[1], qty.intValue(), price.intValue());
if (ret == 0) {
web.httpresponse();
+ //System.printString("DEBUG -> Inside ret = 0");
StringBuffer s = new StringBuffer("Added Item ");
s.append(web.parsed[1]);
s.append(" Quantity ");
web.httpresponse();
String s = new String("Error encountered");
web.write(s.getBytes());
- }
- } else if (op[0] == 'b') {
+ }
+ } else if (op == 1) { /* Buy */
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) {
+ int ret = inventorylist.buyitem(web.parsed[1], qty.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 />");
+ s.append(" Cost ");
+ Integer cost = new Integer(ret*qty.intValue());
+ String c = cost.toString();
+ s.append(c);
String towrite = new String(s);
web.write(towrite.getBytes());
} else {
String s = new String("Error encountered");
web.write(s.getBytes());
}
- } else if (op[0] == 'i') {
+ } else if (op == 2) { /* Inventory */
System.printString("DEBUG > Calling inventory transaction\n");
- //inventorylist.inventory();
String towrite = inventorylist.inventory();
web.write(towrite.getBytes());
-
- } else {
+ } else { /* Error */
System.printString("T > Error - Unknown transaction\n");
}
//Invoke operations
-
web.close();
taskexit(web {!TransPending});
}
byte b1[] = new byte[1024];
read(b1);//Read client request from web server socket
String clientreq = new String(b1);
- System.printString(clientreq);
- System.printString("\n");
+ //System.printString(clientreq);
+ //System.printString("\n");
int index = clientreq.indexOf('/');//Parse the GET client request to find filename
int end = clientreq.indexOf('H');
filename = clientreq.subString((index+1), (end-1));
- System.printString(filename);
+ //System.printString(filename);
System.printString("\n");
return 0;
}
}
- //Process special request
- public int parseTransaction() {
- //System.printString("DEBUG -> Inside parseTransaction");
+ public int parseTransaction(){
int start = filename.indexOf('_');
String s = filename.subString(start+1);
- int n = 4;
- for (int i = 0; i < n; i++) {
- int index;
- if (i == n-1)
- index = s.indexOf('.');
- else
- index = s.indexOf('_');
- parsed[i] = s.subString(0,index);
- String tmp = s.subString(index+1);
- s = tmp;
+
+ if (s.startsWith("add")==true){
+ System.printString("DEBUG > ADD\n");
+ int i1 = s.indexOf('_');
+ parsed[0] = new String(s.subString(0,i1));
+
+ String s1 = s.subString(i1+1);
+ int i2 = s1.indexOf('_');
+ parsed[1] = new String(s1.subString(0,i2));
+
+ String s2 = s1.subString(i2+1);
+ int i3 = s2.indexOf('_');
+ parsed[2] = new String(s2.subString(0,i3));
+
+ String s3 = s2.subString(i3+1);
+ parsed[3] = s3;
+
+ return 0;
+
}
- for (int i = 0; i < 4; i++) {
- System.printString("\n Parsing : ");
- System.printString(parsed[i]);
+ if (s.startsWith("buy")==true){
+ System.printString("DEBUG > BUY\n");
+ int i1 = s.indexOf('_');
+ parsed[0] = s.subString(0,i1);
+
+ String s1 = s.subString(i1+1);
+ int i2 = s1.indexOf('_');
+ parsed[1] = s1.subString(0,i2);
+
+ String s2 = s1.subString(i2+1);
+ parsed[2] = s2;
+
+ parsed[3] = "";
+
+ return 1;
}
- System.printString(" DEBUG > INSIDE PARSE TRANSACTION");
+ if (s.startsWith("inventory")==true){
+ System.printString("DEBUG > INVENTORY\n");
+ return 2;
- return 0;
- }
+ }
+ // Error transaction
+ return -1;
+ }
}