Main.java
001 /*
002  *
003  * All content copyright Terracotta, Inc., unless otherwise indicated. All rights reserved.
004  *
005  */
006 package demo.inventory;
007 
008 import java.io.BufferedReader;
009 import java.io.IOException;
010 import java.io.InputStreamReader;
011 import java.io.PrintWriter;
012 import java.io.StringWriter;
013 import java.util.Iterator;
014 
015 public class Main {
016    private Store store = new Store();
017    private PrintWriter out = new PrintWriter(System.out, true);
018 
019    private void run() {
020       menu_main();
021    }
022 
023    public static void main(String[] args) {
024       try {
025          new Main().run();
026       catch (Exception e) {
027          e.printStackTrace();
028          System.out.flush();
029       }
030    }
031 
032    private void printInventory() {
033       out.println("+-------------------+");
034       out.println("| Inventory Listing |");
035       out.println("+-------------------+");
036       out.println();
037       printProductHeader();
038       for (Iterator i = store.getInventory().values().iterator(); i.hasNext();) {
039          Product p = (Producti.next();
040          printProduct(p);
041       }
042    }
043 
044    private void printDepartments() {
045       out.println("+----------------------------------+");
046       out.println("| Inventory Listing by Departments |");
047       out.println("+----------------------------------+");
048       out.println();
049       for (Iterator i = store.getDepartments().iterator(); i.hasNext();) {
050          Department d = (Departmenti.next();
051          out.println("Department: " + d.getName());
052          Product[] products = d.getProducts();
053          for (int p = 0; p < products.length; p++) {
054             printProduct(products[p]);
055          }
056          out.println();
057       }
058    }
059 
060    private void printProductHeader() {
061       out.println("SKU     Product Name        Price");
062       out.println("------  ------------------  --------");
063    }
064 
065    private void printProduct(Product p) {
066       out.print(padString(p.getSKU()8));
067       out.print(padString(p.getName()20));
068       out.print(padString(p.getPrice() ""8));
069       out.println();
070    }
071 
072    private void menu_main() {
073       out.println();
074       out.println("DSO Inventory Manager");
075       out.println();
076       out
077             .println("This sample application shows how to use Terracotta DSO to share and");
078       out.println("propagate changes to data structures.");
079       out.println();
080       out
081             .println("To perform an action, press the key encased in the square-brackets");
082       out.println("from the list of options presented.");
083       out.println();
084       out
085             .println("Press the [H] key for detailed information on each action.");
086       out.println();
087       while (true) {
088          out.println();
089          out
090                .println("+------------------------------------------------------------------+");
091          out
092                .println("| [I]nventory  [D]epartments  [U]pdate              [H]elp  [Q]uit |");
093          out
094                .println("+------------------------------------------------------------------+");
095          out.print("> ");
096          out.flush();
097          String input = getInput().trim().toUpperCase();
098 
099          if (input.length() == 0)
100             continue;
101 
102          switch (input.charAt(0)) {
103          case 'I':
104             printInventory();
105             continue;
106          case 'Q':
107             return;
108          case 'D':
109             printDepartments();
110             continue;
111          case 'U':
112             updatePrice();
113             continue;
114          case 'H':
115             printHelp();
116             continue;
117          }
118       }
119    }
120 
121    private void updatePrice() {
122       Product p = null;
123       {
124          printInventory();
125          out.println("\nEnter SKU of product to update:");
126          out.print("> ");
127          out.flush();
128          String s = getInput().toUpperCase();
129          p = (Productstore.getInventory().get(s);
130          if (p == null) {
131             out.print("[ERR] No such product with SKU '" + s + "'\n");
132             return;
133          }
134       }
135       double d = -1;
136       out.println();
137       do {
138          out.println("Enter new price for '" + p.getName() "': ");
139          out.print("> ");
140          out.flush();
141          String s = getInput().toUpperCase();
142          try {
143             d = Double.valueOf(s).doubleValue();
144          catch (NumberFormatException nfe) {
145             continue;
146          }
147          synchronized (p) {
148             p.setPrice(d);
149          }
150          ;
151       while (d < 0);
152       out.println("\nPrice updated:");
153       printProduct(p);
154    }
155 
156    private String getInput() {
157       BufferedReader stdin = new BufferedReader(new InputStreamReader(
158             System.in));
159       try {
160          return stdin.readLine();
161       catch (IOException ioe) {
162          ioe.printStackTrace();
163          return "";
164       }
165    }
166 
167    private String padString(String in, int length) {
168       StringWriter out = new StringWriter();
169       out.write(in);
170       length -= in.length();
171       for (int i = 0; i < length; i++)
172          out.write(' ');
173       return out.toString();
174    }
175 
176    private void printHelp() {
177       out.println("+------+");
178       out.println("| Help |");
179       out.println("+------+");
180       out.println();
181       out
182             .println("Press the key that correspond the action that you wish to perform");
183       out.println("Here is what each of the actions will do:");
184       out.println();
185       out.println("[I]nventory:");
186       out.println("This will list the contents of the inventory.");
187       out.println();
188       out.println("[D]epartments:");
189       out
190             .println("This will list the contents of the inventory, grouped by the");
191       out.println("department that owns the inventory item.");
192       out.println();
193       out.println("[U]pdate:");
194       out
195             .println("Takes you into 'edit' mode to change the 'price' field value");
196       out.println("of an inventory item.");
197       out.println();
198       out.println("[H]elp:");
199       out.println("Print this information.");
200       out.println();
201       out.println("[Q]uit:");
202       out.println("Exit this application.");
203       out.println();
204    }
205 }