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