1 4 package demo.inventory; 5 6 import java.io.BufferedReader ; 7 import java.io.IOException ; 8 import java.io.DataInputStream ; 9 import java.io.InputStreamReader ; 10 import java.io.PrintWriter ; 11 import java.io.StringWriter ; 12 import java.util.Iterator ; 13 14 public class Main 15 { 16 public Store store = new Store(); 17 private PrintWriter out = new PrintWriter (System.out,true); 18 19 private void run() 20 { 21 menu_main(); 22 } 23 24 public static void main(String [] args) 25 { 26 try 27 { 28 new Main().run(); 29 } 30 catch(Exception e) 31 { 32 e.printStackTrace(); 33 System.out.flush(); 34 } 35 } 36 37 private void printInventory() 38 { 39 out.println("+-------------------+"); 40 out.println("| Inventory Listing |"); 41 out.println("+-------------------+"); 42 out.println(); 43 printProductHeader(); 44 for(Iterator i = store.inventory.values().iterator(); i.hasNext(); ) 45 { 46 Product p = (Product)i.next(); 47 printProduct(p); 48 } 49 } 50 51 private void printDepartments() 52 { 53 out.println("+----------------------------------+"); 54 out.println("| Inventory Listing by Departments |"); 55 out.println("+----------------------------------+"); 56 out.println(); 57 for(Iterator i = store.departments.iterator(); i.hasNext(); ) 58 { 59 Department d = (Department)i.next(); 60 out.println("Department: "+d.getName()); 61 Product[] products = d.getProducts(); 62 for(int p=0; p < products.length; p++) 63 { 64 printProduct(products[p]); 65 } 66 out.println(); 67 } 68 } 69 70 private void printProductHeader() 71 { 72 out.println("SKU Product Name Price"); 73 out.println("------ ------------------ --------"); 74 } 75 76 private void printProduct(Product p) 77 { 78 out.print(padString(p.sku,8)); 79 out.print(padString(p.name,20)); 80 out.print(padString(p.price+"",8)); 81 out.println(); 82 } 83 84 private void menu_main() 85 { 86 out.println(); 87 out.println("DSO Inventory Manager"); 88 out.println(); 89 out.println("This sample application shows how to use Terracotta DSO to share and"); 90 out.println("propagate changes to data structures."); 91 out.println(); 92 out.println("To perform an action, press the key encased in the square-brackets"); 93 out.println("from the list of options presented."); 94 out.println(); 95 out.println("Press the [H] key for detailed information on each action."); 96 out.println(); 97 while(true) 98 { 99 out.println(); 100 out.println("+------------------------------------------------------------------+"); 101 out.println("| [I]nventory [D]epartments [U]pdate [H]elp [Q]uit |"); 102 out.println("+------------------------------------------------------------------+"); 103 out.print("> "); 104 out.flush(); 105 String input = getInput().trim().toUpperCase(); 106 107 if (input.length() == 0) 108 continue; 109 110 switch(input.charAt(0)) 111 { 112 case 'I': 113 printInventory(); 114 continue; 115 case 'Q': 116 return; 117 case 'D': 118 printDepartments(); 119 continue; 120 case 'U': 121 updatePrice(); 122 continue; 123 case 'H': 124 printHelp(); 125 continue; 126 } 127 } 128 } 129 130 private void updatePrice() 131 { 132 Product p = null; 133 { 134 printInventory(); 135 out.println("\nEnter SKU of product to update:"); 136 out.print("> "); 137 out.flush(); 138 String s = getInput().toUpperCase(); 139 p = (Product)store.inventory.get(s); 140 if (p == null) 141 { 142 out.print("[ERR] No such product with SKU '"+s+"'\n"); 143 return; 144 } 145 } 146 double d = -1; 147 out.println(); 148 do 149 { 150 out.println("Enter new price for '"+p.name+"': "); 151 out.print("> "); 152 out.flush(); 153 String s = getInput().toUpperCase(); 154 try 155 { 156 d = Double.valueOf(s).doubleValue(); 157 } 158 catch(NumberFormatException nfe) { continue; } 159 synchronized(p) 160 { 161 p.setPrice(d); 162 }; 163 } while(d < 0); 164 out.println("\nPrice updated:"); 165 printProduct(p); 166 } 167 168 private String getInput() 169 { 170 BufferedReader stdin = new BufferedReader (new InputStreamReader (System.in)); 171 try 172 { 173 return stdin.readLine(); 174 } 175 catch(IOException ioe) 176 { 177 ioe.printStackTrace(); 178 return ""; 179 } 180 } 181 182 private String padString(String in, int length) 183 { 184 StringWriter out = new StringWriter (); 185 out.write(in); 186 length -= in.length(); 187 for (int i=0; i<length; i++) 188 out.write(' '); 189 return out.toString(); 190 } 191 192 private void printHelp() 193 { 194 out.println("+------+"); 195 out.println("| Help |"); 196 out.println("+------+"); 197 out.println(); 198 out.println("Press the key that correspond the action that you wish to perform"); 199 out.println("Here is what each of the actions will do:"); 200 out.println(); 201 out.println("[I]nventory:"); 202 out.println("This will list the contents of the inventory."); 203 out.println(); 204 out.println("[D]epartments:"); 205 out.println("This will list the contents of the inventory, grouped by the"); 206 out.println("department that owns the inventory item."); 207 out.println(); 208 out.println("[U]pdate:"); 209 out.println("Takes you into 'edit' mode to change the 'price' field value"); 210 out.println("of an inventory item."); 211 out.println(); 212 out.println("[H]elp:"); 213 out.println("Print this information."); 214 out.println(); 215 out.println("[Q]uit:"); 216 out.println("Exit this application."); 217 out.println(); 218 } 219 } 220 | Popular Tags |