Bugs fixed. Works for no constrains of naming convention while calling
authoradash <adash>
Mon, 6 Nov 2006 05:06:17 +0000 (05:06 +0000)
committeradash <adash>
Mon, 6 Nov 2006 05:06:17 +0000 (05:06 +0000)
add, buy and inventory methods
no restriction on size of the inventory

Robust/src/Tests/Inventory.java
Robust/src/Tests/WebServerExample.java
Robust/src/Tests/WebServerSocket.java

index 3072c52dcd923c5e4a285ba1a4d20b84996b11f6..8c3103e8022bf6b9f4aa3f939b50b944ed2e80d5 100644 (file)
@@ -8,37 +8,41 @@ public class Inventory {
        
        // 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");
@@ -51,7 +55,7 @@ public class Inventory {
                        }
                        i.quantity -= quantity;
                        map.put(name, i);
-                       return 0;
+                       return i.price;
                }
        }
 
index 4bc3b4e588330ef0b8b2748d29e9789232e9e54a..8ea363854530005b5b37561786b4372a4765b5fa 100644 (file)
@@ -7,7 +7,7 @@ task Startup(StartupObject s {initialstate}) {
        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 */
 }
 
@@ -51,19 +51,20 @@ task LogRequest(WebServerSocket web{LogPending}, Logger log{Initialize}) {
 //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 ");
@@ -77,21 +78,21 @@ task Transaction(WebServerSocket web{TransPending}, Inventory inventorylist{Tran
                        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 {
@@ -99,17 +100,14 @@ task Transaction(WebServerSocket web{TransPending}, Inventory inventorylist{Tran
                        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});
 }
index 69fd1c689e0d68ec29cd066890a19ab6227f8f45..b8abe511f70bb74699dd48b9b5bd23ac8efe4b97 100644 (file)
@@ -61,12 +61,12 @@ public class WebServerSocket extends Socket {
                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;
        }
@@ -82,28 +82,51 @@ public class WebServerSocket extends Socket {
        }
 
        
-       //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;
+       }
 }