001: /*
002: @COPYRIGHT@
003: */
004: package demo.inventory;
005:
006: import java.io.BufferedReader;
007: import java.io.IOException;
008: import java.io.InputStreamReader;
009: import java.io.PrintWriter;
010: import java.io.StringWriter;
011: import java.util.Iterator;
012:
013: public class Main {
014: private Store store = new Store();
015: private PrintWriter out = new PrintWriter(System.out, true);
016:
017: private void run() {
018: menu_main();
019: }
020:
021: public static void main(String[] args) {
022: try {
023: new Main().run();
024: } catch (Exception e) {
025: e.printStackTrace();
026: System.out.flush();
027: }
028: }
029:
030: private void printInventory() {
031: out.println("+-------------------+");
032: out.println("| Inventory Listing |");
033: out.println("+-------------------+");
034: out.println();
035: printProductHeader();
036: for (Iterator i = store.getInventory().values().iterator(); i
037: .hasNext();) {
038: Product p = (Product) i.next();
039: printProduct(p);
040: }
041: }
042:
043: private void printDepartments() {
044: out.println("+----------------------------------+");
045: out.println("| Inventory Listing by Departments |");
046: out.println("+----------------------------------+");
047: out.println();
048: for (Iterator i = store.getDepartments().iterator(); i
049: .hasNext();) {
050: Department d = (Department) i.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 = (Product) store.getInventory().get(s);
130: if (p == null) {
131: out.print("[ERR] No such product with SKU '" + s
132: + "'\n");
133: return;
134: }
135: }
136: double d = -1;
137: out.println();
138: do {
139: out.println("Enter new price for '" + p.getName() + "': ");
140: out.print("> ");
141: out.flush();
142: String s = getInput().toUpperCase();
143: try {
144: d = Double.valueOf(s).doubleValue();
145: } catch (NumberFormatException nfe) {
146: continue;
147: }
148: synchronized (p) {
149: p.setPrice(d);
150: }
151: ;
152: } while (d < 0);
153: out.println("\nPrice updated:");
154: printProduct(p);
155: }
156:
157: private String getInput() {
158: BufferedReader stdin = new BufferedReader(
159: new InputStreamReader(System.in));
160: try {
161: return stdin.readLine();
162: } catch (IOException ioe) {
163: ioe.printStackTrace();
164: return "";
165: }
166: }
167:
168: private String padString(String in, int length) {
169: StringWriter out = new StringWriter();
170: out.write(in);
171: length -= in.length();
172: for (int i = 0; i < length; i++)
173: out.write(' ');
174: return out.toString();
175: }
176:
177: private void printHelp() {
178: out.println("+------+");
179: out.println("| Help |");
180: out.println("+------+");
181: out.println();
182: out
183: .println("Press the key that correspond the action that you wish to perform");
184: out.println("Here is what each of the actions will do:");
185: out.println();
186: out.println("[I]nventory:");
187: out.println("This will list the contents of the inventory.");
188: out.println();
189: out.println("[D]epartments:");
190: out
191: .println("This will list the contents of the inventory, grouped by the");
192: out.println("department that owns the inventory item.");
193: out.println();
194: out.println("[U]pdate:");
195: out
196: .println("Takes you into 'edit' mode to change the 'price' field value");
197: out.println("of an inventory item.");
198: out.println();
199: out.println("[H]elp:");
200: out.println("Print this information.");
201: out.println();
202: out.println("[Q]uit:");
203: out.println("Exit this application.");
204: out.println();
205: }
206: }
|