目录结构

  • Coffee
  • CoffeeBrewer
  • Product
  • Order
  • OrderItem
  • Sales
  • SalesFormatter
  • Catalog
  • CatalogLoader
  • FileCatalogLoader
  • DataFormatException
  • PlainTextSalesFormatter
  • HTMLSalesFormatter
  • XMLSalesFormatter

  • GourmetCoffeeGUI (仅需提交这个类)

系统使用的catalog.dat文件在压缩包内

Coffee

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
import java.awt.BorderLayout;
import java.awt.GridLayout;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JTextField;

public class Coffee extends Product {
private String origin;
private String roast;
private String flavor;
private String aroma;
private String acidity;
private String body;

public Coffee(String var1, String var2, double var3, String var5, String var6, String var7, String var8, String var9, String var10) {
super(var1, var2, var3);
this.origin = var5;
this.roast = var6;
this.flavor = var7;
this.aroma = var8;
this.acidity = var9;
this.body = var10;
}

public String getOrigin() {
return this.origin;
}

public String getRoast() {
return this.roast;
}

public String getFlavor() {
return this.flavor;
}

public String getAroma() {
return this.aroma;
}

public String getAcidity() {
return this.acidity;
}

public String getBody() {
return this.body;
}

public String toString() {
return super.toString() + "_" + this.getOrigin() + "_" + this.getRoast() + "_" + this.getFlavor() + "_" + this.getAroma() + "_" + this.getAcidity() + "_" + this.getBody();
}

public JPanel getPanel() {
JPanel var1 = new JPanel(new BorderLayout());
JTextField var2 = new JTextField(this.getCode());
JTextField var3 = new JTextField(this.getDescription());
JTextField var4 = new JTextField(Double.toString(this.getPrice()));
JTextField var5 = new JTextField(this.getOrigin());
JTextField var6 = new JTextField(this.getRoast());
JTextField var7 = new JTextField(this.getFlavor());
JTextField var8 = new JTextField(this.getAroma());
JTextField var9 = new JTextField(this.getAcidity());
JTextField var10 = new JTextField(this.getBody());
var2.setEditable(false);
var3.setEditable(false);
var4.setEditable(false);
var5.setEditable(false);
var6.setEditable(false);
var7.setEditable(false);
var8.setEditable(false);
var9.setEditable(false);
var10.setEditable(false);
JPanel var11 = new JPanel(new GridLayout(9, 1));
var11.add(new JLabel("Code:"));
var11.add(new JLabel("Description: "));
var11.add(new JLabel("Price:"));
var11.add(new JLabel("Origin:"));
var11.add(new JLabel("Roast:"));
var11.add(new JLabel("Flavor:"));
var11.add(new JLabel("Aroma:"));
var11.add(new JLabel("Acidity:"));
var11.add(new JLabel("Body:"));
JPanel var12 = new JPanel(new GridLayout(9, 1));
var12.add(var2);
var12.add(var3);
var12.add(var4);
var12.add(var5);
var12.add(var6);
var12.add(var7);
var12.add(var8);
var12.add(var9);
var12.add(var10);
var1.add(var11, "West");
var1.add(var12, "East");
return var1;
}
}

CoffeeBrewer

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
import java.awt.BorderLayout;
import java.awt.GridLayout;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JTextField;

public class CoffeeBrewer extends Product {
private String model;
private String waterSupply;
private int numberOfCups;

public CoffeeBrewer(String var1, String var2, double var3, String var5, String var6, int var7) {
super(var1, var2, var3);
this.model = var5;
this.waterSupply = var6;
this.numberOfCups = var7;
}

public String getModel() {
return this.model;
}

public String getWaterSupply() {
return this.waterSupply;
}

public int getNumberOfCups() {
return this.numberOfCups;
}

public String toString() {
return super.toString() + "_" + this.getModel() + "_" + this.getWaterSupply() + "_" + this.getNumberOfCups();
}

public JPanel getPanel() {
JPanel var1 = new JPanel(new BorderLayout());
JTextField var2 = new JTextField(this.getCode());
JTextField var3 = new JTextField(this.getDescription());
JTextField var4 = new JTextField(Double.toString(this.getPrice()));
JTextField var5 = new JTextField(this.getModel());
JTextField var6 = new JTextField(this.getWaterSupply());
JTextField var7 = new JTextField(Integer.toString(this.getNumberOfCups()));
var2.setEditable(false);
var3.setEditable(false);
var4.setEditable(false);
var5.setEditable(false);
var6.setEditable(false);
var7.setEditable(false);
JPanel var8 = new JPanel(new GridLayout(6, 1));
var8.add(new JLabel("Code:"));
var8.add(new JLabel("Description: "));
var8.add(new JLabel("Price:"));
var8.add(new JLabel("Model:"));
var8.add(new JLabel("Water supply:"));
var8.add(new JLabel("Number of cups:"));
JPanel var9 = new JPanel(new GridLayout(6, 1));
var9.add(var2);
var9.add(var3);
var9.add(var4);
var9.add(var5);
var9.add(var6);
var9.add(var7);
var1.add(var8, "West");
var1.add(var9, "Center");
return var1;
}
}

Product

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
import java.awt.BorderLayout;
import java.awt.GridLayout;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JTextField;

public class Product {
private String code;
private String description;
private double price;

public Product(String var1, String var2, double var3) {
this.code = var1;
this.description = var2;
this.price = var3;
}

public String getCode() {
return this.code;
}

public String getDescription() {
return this.description;
}

public double getPrice() {
return this.price;
}

public boolean equals(Object var1) {
return var1 instanceof Product && this.getCode().equals(((Product)var1).getCode());
}

public String toString() {
return this.getCode() + "_" + this.getDescription() + "_" + this.getPrice();
}

public JPanel getPanel() {
JPanel var1 = new JPanel(new BorderLayout());
JTextField var2 = new JTextField(this.getCode());
JTextField var3 = new JTextField(this.getDescription());
JTextField var4 = new JTextField(Double.toString(this.getPrice()));
var2.setEditable(false);
var3.setEditable(false);
var4.setEditable(false);
JPanel var5 = new JPanel(new GridLayout(3, 1));
var5.add(new JLabel("Code:"));
var5.add(new JLabel("Description: "));
var5.add(new JLabel("Price:"));
JPanel var6 = new JPanel(new GridLayout(3, 1));
var6.add(var2);
var6.add(var3);
var6.add(var4);
var1.add(var5, "West");
var1.add(var6, "Center");
return var1;
}
}

Order

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
import java.util.Iterator;
import java.util.Vector;

public class Order {
private Vector items = new Vector();

public Order() {
}

public void addItem(OrderItem var1) {
this.items.add(var1);
}

public void removeItem(OrderItem var1) {
this.items.removeElement(var1);
}

public Iterator getItemsIterator() {
return this.items.iterator();
}

public OrderItem getItem(Product var1) {
Iterator var2 = this.getItemsIterator();

OrderItem var3;
do {
if (!var2.hasNext()) {
return null;
}

var3 = (OrderItem)var2.next();
} while(!var3.getProduct().equals(var1));

return var3;
}

public int getNumberOfItems() {
return this.items.size();
}

public double getTotalCost() {
double var1 = 0.0D;

for(Iterator var3 = this.getItemsIterator(); var3.hasNext(); var1 += ((OrderItem)var3.next()).getValue()) {
}

return var1;
}

public OrderItem[] getItems() {
OrderItem[] var1 = new OrderItem[this.getNumberOfItems()];
int var2 = 0;

for(Iterator var3 = this.getItemsIterator(); var3.hasNext(); var1[var2++] = (OrderItem)var3.next()) {
}

return var1;
}
}

OrderItem

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
public class OrderItem {
private Product product;
private int quantity;

public OrderItem(Product var1, int var2) {
this.product = var1;
this.quantity = var2;
}

public Product getProduct() {
return this.product;
}

public int getQuantity() {
return this.quantity;
}

public void setQuantity(int var1) {
this.quantity = var1;
}

public double getValue() {
return this.getProduct().getPrice() * (double)this.getQuantity();
}

public String toString() {
return this.getQuantity() + " " + this.getProduct().getCode() + " " + this.getProduct().getPrice();
}
}

Sales

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
import java.util.Iterator;
import java.util.Vector;

public class Sales {
private Vector orders = new Vector();

public Sales() {
}

public void addOrder(Order var1) {
this.orders.add(var1);
}

public Iterator getOrdersIterator() {
return this.orders.iterator();
}

public int getNumberOfOrders() {
return this.orders.size();
}
}

SalesFormatter

1
2
3
public interface SalesFormatter {
String formatSales(Sales var1);
}

Catalog

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
import java.util.Iterator;
import java.util.Vector;

public class Catalog {
private Vector products = new Vector();

public Catalog() {
}

public void addProduct(Product var1) {
this.products.add(var1);
}

public Iterator getProductsIterator() {
return this.products.iterator();
}

public Product getProduct(String var1) {
Iterator var2 = this.getProductsIterator();

Product var3;
do {
if (!var2.hasNext()) {
return null;
}

var3 = (Product)var2.next();
} while(!var3.getCode().equals(var1));

return var3;
}

public int getNumberOfProducts() {
return this.products.size();
}

public String[] getCodes() {
String[] var1 = new String[this.getNumberOfProducts()];
int var2 = 0;

for(Iterator var3 = this.getProductsIterator(); var3.hasNext(); var1[var2++] = ((Product)var3.next()).getCode()) {
}

return var1;
}
}

CatalogLoader

1
2
3
4
5
6
7
import java.io.FileNotFoundException;
import java.io.IOException;

public interface CatalogLoader {
Catalog loadCatalog(String var1) throws FileNotFoundException, IOException, DataFormatException;
}

FileCatalogLoader

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
import java.io.BufferedReader;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;
import java.util.StringTokenizer;

public class FileCatalogLoader implements CatalogLoader {
private static final String COFFEE_PREFIX = "Coffee";
private static final String COFFEE_BREWER_PREFIX = "Brewer";
private static final String PRODUCT_PREFIX = "Product";
private static final String DELIM = "_";

public FileCatalogLoader() {
}

public Catalog loadCatalog(String var1) throws FileNotFoundException, IOException, DataFormatException {
Catalog var2 = new Catalog();
BufferedReader var3 = new BufferedReader(new FileReader(var1));

for(String var4 = var3.readLine(); var4 != null; var4 = var3.readLine()) {
Object var5 = null;
if (var4.startsWith("Brewer")) {
var5 = this.readCoffeeBrewer(var4);
} else if (var4.startsWith("Coffee")) {
var5 = this.readCoffee(var4);
} else {
if (!var4.startsWith("Product")) {
throw new DataFormatException(var4);
}

var5 = this.readProduct(var4);
}

var2.addProduct((Product)var5);
}

return var2;
}

private Product readProduct(String var1) throws DataFormatException {
StringTokenizer var2 = new StringTokenizer(var1, "_");
if (var2.countTokens() != 4) {
throw new DataFormatException(var1);
} else {
try {
String var3 = var2.nextToken();
return new Product(var2.nextToken(), var2.nextToken(), Double.parseDouble(var2.nextToken()));
} catch (NumberFormatException var4) {
throw new DataFormatException(var1);
}
}
}

private Coffee readCoffee(String var1) throws DataFormatException {
StringTokenizer var2 = new StringTokenizer(var1, "_");
if (var2.countTokens() != 10) {
throw new DataFormatException(var1);
} else {
try {
String var3 = var2.nextToken();
return new Coffee(var2.nextToken(), var2.nextToken(), Double.parseDouble(var2.nextToken()), var2.nextToken(), var2.nextToken(), var2.nextToken(), var2.nextToken(), var2.nextToken(), var2.nextToken());
} catch (NumberFormatException var4) {
throw new DataFormatException(var1);
}
}
}

private CoffeeBrewer readCoffeeBrewer(String var1) throws DataFormatException {
StringTokenizer var2 = new StringTokenizer(var1, "_");
if (var2.countTokens() != 7) {
throw new DataFormatException(var1);
} else {
try {
String var3 = var2.nextToken();
return new CoffeeBrewer(var2.nextToken(), var2.nextToken(), Double.parseDouble(var2.nextToken()), var2.nextToken(), var2.nextToken(), Integer.parseInt(var2.nextToken()));
} catch (NumberFormatException var4) {
throw new DataFormatException(var1);
}
}
}
}

DataFormatException

1
2
3
4
5
6
7
8
9
public class DataFormatException extends Exception {
public DataFormatException() {
}

public DataFormatException(String var1) {
super(var1);
}
}

PlainTextSalesFormatter

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
import java.util.Iterator;

public class PlainTextSalesFormatter implements SalesFormatter {
private static final String NEW_LINE = System.getProperty("line.separator");
private static PlainTextSalesFormatter singletonInstance = null;

public static PlainTextSalesFormatter getSingletonInstance() {
if (singletonInstance == null) {
singletonInstance = new PlainTextSalesFormatter();
}

return singletonInstance;
}

private PlainTextSalesFormatter() {
}

public String formatSales(Sales var1) {
String var2 = "";
int var3 = 0;

Order var5;
for(Iterator var4 = var1.getOrdersIterator(); var4.hasNext(); var2 = var2 + NEW_LINE + "Total = " + var5.getTotalCost() + NEW_LINE) {
var5 = (Order)var4.next();
StringBuffer var10000 = (new StringBuffer()).append(var2).append("------------------------").append(NEW_LINE).append("Order ");
++var3;
var2 = var10000.append(var3).append(NEW_LINE).append(NEW_LINE).toString();

OrderItem var7;
for(Iterator var6 = var5.getItemsIterator(); var6.hasNext(); var2 = var2 + var7.getQuantity() + " " + var7.getProduct().getCode() + " " + var7.getProduct().getPrice() + NEW_LINE) {
var7 = (OrderItem)var6.next();
}
}

return var2;
}
}

HTMLSalesFormatter

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
import java.util.Iterator;

public class HTMLSalesFormatter implements SalesFormatter {
private static final String NEW_LINE = System.getProperty("line.separator");
private static HTMLSalesFormatter singletonInstance = null;

public static HTMLSalesFormatter getSingletonInstance() {
if (singletonInstance == null) {
singletonInstance = new HTMLSalesFormatter();
}

return singletonInstance;
}

private HTMLSalesFormatter() {
}

public String formatSales(Sales var1) {
String var2 = "<html>" + NEW_LINE + " <body>" + NEW_LINE + " <center><h2>Orders</h2></center>" + NEW_LINE;
Iterator var3 = var1.getOrdersIterator();

while(var3.hasNext()) {
Order var4 = (Order)var3.next();
var2 = var2 + " <hr>" + NEW_LINE + " <h4>Total = " + var4.getTotalCost() + "</h4>" + NEW_LINE;

OrderItem var6;
for(Iterator var5 = var4.getItemsIterator(); var5.hasNext(); var2 = var2 + " <p>" + NEW_LINE + " <b>code:</b> " + var6.getProduct().getCode() + "<br>" + NEW_LINE + " <b>quantity:</b> " + var6.getQuantity() + "<br>" + NEW_LINE + " <b>price:</b> " + var6.getProduct().getPrice() + NEW_LINE + " </p>" + NEW_LINE) {
var6 = (OrderItem)var5.next();
}
}

var2 = var2 + " </body>" + NEW_LINE + "</html>";
return var2;
}
}

XMLSalesFormatter

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
import java.util.Iterator;

public class XMLSalesFormatter implements SalesFormatter {
private static final String NEW_LINE = System.getProperty("line.separator");
private static XMLSalesFormatter singletonInstance = null;

public static XMLSalesFormatter getSingletonInstance() {
if (singletonInstance == null) {
singletonInstance = new XMLSalesFormatter();
}

return singletonInstance;
}

private XMLSalesFormatter() {
}

public String formatSales(Sales var1) {
String var2 = "<Sales>" + NEW_LINE;

for(Iterator var3 = var1.getOrdersIterator(); var3.hasNext(); var2 = var2 + " </Order>" + NEW_LINE) {
Order var4 = (Order)var3.next();
var2 = var2 + " <Order total=\"" + var4.getTotalCost() + "\">" + NEW_LINE;

OrderItem var6;
for(Iterator var5 = var4.getItemsIterator(); var5.hasNext(); var2 = var2 + " <OrderItem quantity=\"" + var6.getQuantity() + "\" price=\"" + var6.getProduct().getPrice() + "\">" + var6.getProduct().getCode() + "</OrderItem>" + NEW_LINE) {
var6 = (OrderItem)var5.next();
}
}

var2 = var2 + "</Sales>";
return var2;
}
}

GourmetCoffeeGUI

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import javax.swing.event.*;
import java.io.*;
import java.text.*;

/**
* Gourmet Coffee System.
*
* @author author name
* @version 1.1.0
* @see Product
* @see Coffee
* @see CoffeeBrewer
* @see Catalog
* @see OrderItem
* @see Order
* @see Sales
* @see CatalogLoader
* @see FileCatalogLoader
* @see DataFormatException
* @see SalesFormatter
* @see PlainTextSalesFormatter
* @see HTMLSalesFormatter
* @see XMLSalesFormatter
*/
@SuppressWarnings("unchecked")
public class GourmetCoffeeGUI extends JPanel {

/* Standar error stream */
static private PrintWriter stdErr = new PrintWriter(System.err, true);

/* Window width in pixels */
static private int WIDTH = 600;

/* Window height in pixels */
static private int HEIGHT = 530;

/* Size of the catalog list cell in pixels */
static private int CATALOG_CELL_SIZE = 50;

/* Visible rows in catalog list */
static private int CATALOG_LIST_ROWS = 14;

/* Size of the order list cell in pixels */
static private int ORDER_CELL_SIZE = 100;

/* Visible rows in order list */
static private int ORDER_LIST_ROWS = 6;

/* Size quantity text field */
static private int QUANTITY__TEXTFIELD_SIZE = 5;

/* Size total text field */
static private int TOTAL__TEXTFIELD_SIZE = 8;

/* Rows in status text area rows */
static private int STATUS_ROWS = 10;

/* Rows in status text area cols */
static private int STATUS_COLS = 40;

private JList catalogList;
private JList orderList;
private JButton addModifyButton;
private JButton removeButton;
private JButton registerSaleButton;
private JButton displaySalesButton;
private JButton saveSalesButton;
private JPanel productPanel;
private JLabel quantityLabel;
private JLabel totalLabel;
private JTextField quantityTextField;
private JTextField totalTextField;
private JTextArea statusTextArea;
private JRadioButton plainRadioButton;
private JRadioButton HTMLRadioButton;
private JRadioButton XMLRadioButton;

private JFileChooser fileChooser;

private Catalog catalog;
private Order currentOrder;
private Sales sales;
private SalesFormatter salesFormatter;
private NumberFormat dollarFormatter;

/**
* Loads a product catalog and starts the application.
*
* @param args String arguments. Not used.
* @throws IOException if there are errors in the loading the catalog.
*/
public static void main(String[] args) throws IOException {

String filename = "";

if (args.length != 1) {
filename = "catalog.dat";
} else {
filename = args[0];
}
try {
Catalog catalog = (new FileCatalogLoader()).loadCatalog(filename);

JFrame frame = new JFrame("Gourmet Coffee");

frame.setContentPane(new GourmetCoffeeGUI(catalog));
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setSize(WIDTH, HEIGHT);
frame.setResizable(true);
frame.setVisible(true);

} catch (FileNotFoundException fnfe) {
stdErr.println("The file does not exist");

System.exit(1);

} catch (DataFormatException dfe) {
stdErr.println("The file contains malformed data: "
+ dfe.getMessage());

System.exit(1);
}
}

/**
* Instantiates the components and arranges them in a window.
*
* @param initialCatalog a product catalog.
*/
public GourmetCoffeeGUI(Catalog initialCatalog) {

// create the components
catalogList = new JList();
orderList = new JList();
catalogList.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
catalogList.setVisibleRowCount(CATALOG_LIST_ROWS);
catalogList.setFixedCellWidth(CATALOG_CELL_SIZE);
orderList.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
orderList.setVisibleRowCount(ORDER_LIST_ROWS);
orderList.setFixedCellWidth(ORDER_CELL_SIZE);
addModifyButton = new JButton("Add|Modify Order Item");
removeButton = new JButton("Remove Order Item");
registerSaleButton = new JButton("Register Sale of Current Order");
displaySalesButton = new JButton("Display Sales");
saveSalesButton = new JButton("Save Sales");
quantityLabel = new JLabel("Quantity:");
totalLabel = new JLabel("Total:");
quantityTextField = new JTextField("", QUANTITY__TEXTFIELD_SIZE);
totalTextField = new JTextField("$0.00", TOTAL__TEXTFIELD_SIZE);
totalTextField.setEditable(false);
statusTextArea = new JTextArea(STATUS_ROWS, STATUS_COLS);
statusTextArea.setEditable(false);
plainRadioButton = new JRadioButton("Plain", true);
HTMLRadioButton = new JRadioButton("HTML");
XMLRadioButton = new JRadioButton("XML");
ButtonGroup group = new ButtonGroup();
group.add(plainRadioButton);
group.add(HTMLRadioButton);
group.add(XMLRadioButton);

// Product Information panel
productPanel = new JPanel();
productPanel.setBorder(
BorderFactory.createTitledBorder("Product Information"));

// Catalog panel
JPanel catalogPanel = new JPanel();
catalogPanel.setBorder(BorderFactory.createTitledBorder("Catalog"));
catalogPanel.add(
new JScrollPane(catalogList,
JScrollPane.VERTICAL_SCROLLBAR_ALWAYS,
JScrollPane.HORIZONTAL_SCROLLBAR_NEVER));

// "Add|Modify Product" panel
JPanel centralPanel = new JPanel(new BorderLayout());
JPanel addModifyPanel = new JPanel(new GridLayout(2, 1));
JPanel quantityPanel = new JPanel();
quantityPanel.add(quantityLabel);
quantityPanel.add(quantityTextField);
addModifyPanel.add(quantityPanel);
addModifyPanel.add(addModifyButton);
centralPanel.add(productPanel, BorderLayout.CENTER);
centralPanel.add(addModifyPanel, BorderLayout.SOUTH);

// Order panel
JPanel orderPanel = new JPanel(new BorderLayout());
orderPanel.setBorder(BorderFactory.createTitledBorder("Order"));
JPanel totalPanel = new JPanel();
totalPanel.add(totalLabel);
totalPanel.add(totalTextField);
JPanel buttonsPanel = new JPanel(new GridLayout(2, 1));
buttonsPanel.add(removeButton);
buttonsPanel.add(registerSaleButton);
orderPanel.add(totalPanel, BorderLayout.NORTH);
orderPanel.add(new JScrollPane(orderList,
JScrollPane.VERTICAL_SCROLLBAR_ALWAYS,
JScrollPane.HORIZONTAL_SCROLLBAR_NEVER), BorderLayout.CENTER);
orderPanel.add(buttonsPanel, BorderLayout.SOUTH);

// Status panel
JPanel bottomPanel = new JPanel(new BorderLayout());
bottomPanel.setBorder(BorderFactory.createTitledBorder("Status"));
JPanel salesButtonsPanel = new JPanel(new GridLayout(1, 5));
salesButtonsPanel.add(plainRadioButton);
salesButtonsPanel.add(HTMLRadioButton);
salesButtonsPanel.add(XMLRadioButton);
salesButtonsPanel.add(displaySalesButton);
salesButtonsPanel.add(saveSalesButton);
bottomPanel.add(new JScrollPane(statusTextArea,
JScrollPane.VERTICAL_SCROLLBAR_AS_NEEDED,
JScrollPane.HORIZONTAL_SCROLLBAR_AS_NEEDED), BorderLayout.CENTER);
bottomPanel.add(salesButtonsPanel, BorderLayout.SOUTH);

// arrange panels in window
setLayout(new BorderLayout());
add(catalogPanel, BorderLayout.WEST);
add(centralPanel, BorderLayout.CENTER);
add(orderPanel, BorderLayout.EAST);
add(bottomPanel, BorderLayout.SOUTH);

// start listening for list and buttons events
catalogList.addListSelectionListener(new DisplayProductListener());
addModifyButton.addActionListener(new AddModifyListener());
removeButton.addActionListener(new RemoveListener());
registerSaleButton.addActionListener(new RegisterSaleListener());
displaySalesButton.addActionListener(new DisplaySalesListener());
saveSalesButton.addActionListener(new SaveSalesListener());
plainRadioButton.addActionListener(new PlainListener());
HTMLRadioButton.addActionListener(new HTMLListener());
XMLRadioButton.addActionListener(new XMLListener());

// populate the product catalog
catalog = initialCatalog;
catalogList.setListData(catalog.getCodes());

currentOrder = new Order();
sales = new Sales();
salesFormatter = PlainTextSalesFormatter.getSingletonInstance();
fileChooser = new JFileChooser();
dollarFormatter = NumberFormat.getCurrencyInstance();
}

/**
* This inner class handles list-selection events.
*/
class DisplayProductListener implements ListSelectionListener {

/**
* Displays the information of the selected product.
*
* @param event the event object.
*/
public void valueChanged(ListSelectionEvent event) {

if (!catalogList.getValueIsAdjusting()) {
String code = (String) catalogList.getSelectedValue();

Product product = catalog.getProduct(code);

productPanel.removeAll();
productPanel.setVisible(false); // Use this
productPanel.add(product.getPanel()); // to update the
productPanel.setVisible(true); // panel correctly

statusTextArea.setText("Product " + code
+ " has been displayed");
}
}
}

/**
* This inner class processes <code>addModifyButton</code> events.
*/
class AddModifyListener implements ActionListener {

/**
* Adds an order item to the current order.
*
* @param event the event object.
*/
public void actionPerformed(ActionEvent event) {

String var3 = (String) GourmetCoffeeGUI.this.catalogList.getSelectedValue();

int var2;
try {
var2 = Integer.parseInt(GourmetCoffeeGUI.this.quantityTextField.getText());
} catch (NumberFormatException var6) {
GourmetCoffeeGUI.this.statusTextArea.setText("Please enter an integer.");
return;
}

if (var2 < 1) {
GourmetCoffeeGUI.this.statusTextArea.setText("Please enter a right integer.");
} else if (var3 == null) {
GourmetCoffeeGUI.this.statusTextArea.setText("Please select a product code from the catalog list.");
} else {
Product var4 = GourmetCoffeeGUI.this.catalog.getProduct(var3);
OrderItem var5 = GourmetCoffeeGUI.this.currentOrder.getItem(var4);
if (var5 == null) {
var5 = new OrderItem(var4, var2);
GourmetCoffeeGUI.this.currentOrder.addItem(var5);
GourmetCoffeeGUI.this.orderList.setListData(GourmetCoffeeGUI.this.currentOrder.getItems());
GourmetCoffeeGUI.this.statusTextArea.setText("The product has been added.");
} else {
var5.setQuantity(var2);
GourmetCoffeeGUI.this.orderList.setListData(GourmetCoffeeGUI.this.currentOrder.getItems());
GourmetCoffeeGUI.this.statusTextArea.setText("The product has been modified.");
}

GourmetCoffeeGUI.this.totalTextField.setText(GourmetCoffeeGUI.this.dollarFormatter.format(GourmetCoffeeGUI.this.currentOrder.getTotalCost()));
GourmetCoffeeGUI.this.quantityTextField.setText("");
}
}

}

/**
* This inner class processes <code>removeButton</code> events.
*/
class RemoveListener implements ActionListener {

/**
* Removes an order item from the current order.
*
* @param event the event object.
*/
public void actionPerformed(ActionEvent event) {

if (GourmetCoffeeGUI.this.currentOrder.getNumberOfItems() == 0) {
GourmetCoffeeGUI.this.statusTextArea.setText("The order is empty.");
} else {
OrderItem var2 = (OrderItem) GourmetCoffeeGUI.this.orderList.getSelectedValue();
if (var2 == null) {
GourmetCoffeeGUI.this.statusTextArea.setText("Please select an item from the order list.");
} else {
GourmetCoffeeGUI.this.currentOrder.removeItem(var2);
GourmetCoffeeGUI.this.orderList.setListData(GourmetCoffeeGUI.this.currentOrder.getItems());
GourmetCoffeeGUI.this.totalTextField.setText(GourmetCoffeeGUI.this.dollarFormatter.format(GourmetCoffeeGUI.this.currentOrder.getTotalCost()));
GourmetCoffeeGUI.this.statusTextArea.setText("The product has been removed.");
}
}
}
}

/**
* This inner class processes <code>registerSaleButton</code> button events.
*/
class RegisterSaleListener implements ActionListener {

/**
* Registers the sale of the current order.
*
* @param event the event object.
*/
public void actionPerformed(ActionEvent event) {

if (currentOrder.getNumberOfItems() == 0) {
statusTextArea.setText("The order is empty.");
} else {
sales.addOrder(currentOrder);
currentOrder = new Order();
orderList.setListData(currentOrder.getItems());
totalTextField.setText(dollarFormatter.format(0));
statusTextArea.setText("The sale has been registered.");
}
}
}

/**
* This inner class processes <code>displaySalesButton</code>events.
*/
class DisplaySalesListener implements ActionListener {

/**
* Displays the sales information.
*
* @param event the event object.
*/
public void actionPerformed(ActionEvent event) {

if (sales.getNumberOfOrders() == 0) {
statusTextArea.setText("No orders have been sold.");
} else {
statusTextArea.setText(salesFormatter.formatSales(sales));
}
}
}

/*
* This inner class processes <code>saveSalesButton</code> events.
*/
class SaveSalesListener implements ActionListener {

/**
* Saves the sales informations in a file.
*
* @param event the event object.
*/
public void actionPerformed(ActionEvent event) {

if (GourmetCoffeeGUI.this.sales.getNumberOfOrders() == 0) {
GourmetCoffeeGUI.this.statusTextArea.setText("No orders have been sold.");
} else {
GourmetCoffeeGUI.this.fileChooser.setFileSelectionMode(0);
int var2 = GourmetCoffeeGUI.this.fileChooser.showSaveDialog((Component) null);
if (var2 != 0) {
GourmetCoffeeGUI.this.statusTextArea.setText("The sales information has not been saved.");
} else {
File var3 = GourmetCoffeeGUI.this.fileChooser.getSelectedFile();

try {
PrintWriter var4 = new PrintWriter(new FileWriter(var3));
var4.print(GourmetCoffeeGUI.this.salesFormatter.formatSales(GourmetCoffeeGUI.this.sales));
var4.close();
GourmetCoffeeGUI.this.statusTextArea.setText("The sales information has been saved.");
} catch (IOException var5) {
GourmetCoffeeGUI.this.statusTextArea.setText("Error: Problem with I/O");
}
}
}
/* PLACE YOUR CODE HERE */

}
}

/*
* This inner class processes <code>plainRadioButton</code> events.
*/
class PlainListener implements ActionListener {

/**
* Sets the sales formatter to plain text.
*
* @param event the event object.
*/
public void actionPerformed(ActionEvent event) {

salesFormatter = PlainTextSalesFormatter.getSingletonInstance();
}
}

/*
* This inner class processes <code>HTMLRadioButton</code> events.
*/
class HTMLListener implements ActionListener {

/**
* Sets the sales formatter to HTML.
*
* @param event the event object.
*/
public void actionPerformed(ActionEvent event) {
salesFormatter = HTMLSalesFormatter.getSingletonInstance();
}
}

/*
* This inner class processes <code>XMLRadioButton</code> events.
*/
class XMLListener implements ActionListener {

/**
* Sets the sales formatter to XML.
*
* @param event the event object.
*/
public void actionPerformed(ActionEvent event) {

salesFormatter = XMLSalesFormatter.getSingletonInstance();
}
}
}