-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathStore.java
More file actions
475 lines (407 loc) · 19.2 KB
/
Store.java
File metadata and controls
475 lines (407 loc) · 19.2 KB
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
import javax.sound.sampled.AudioInputStream;
import javax.sound.sampled.AudioSystem;
import javax.sound.sampled.Clip;
import javax.swing.*;
import javax.swing.table.DefaultTableModel;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.print.PageFormat;
import java.awt.print.Printable;
import java.awt.print.PrinterException;
import java.awt.print.PrinterJob;
import java.io.File;
import java.math.BigDecimal;
import java.math.MathContext;
import java.text.NumberFormat;
import java.util.*;
public class Store extends JFrame {
// create a jtable, put it in scrollpane
DefaultTableModel data=new DefaultTableModel(){
@Override
public boolean isCellEditable(int row, int column) {
return false;
}
};
JTable table=new JTable(data);
JScrollPane jScrollPane=new JScrollPane(table);
// use a list to contain all of the web ads
ArrayList<WebAd> cart=new ArrayList<>();
// create buttons
JButton addWebAd=new JButton("Add WebAd");
JButton removeWebAd=new JButton("Remove Selected Ad");
JButton showStats=new JButton("Show Stats");
JButton viewAd=new JButton("View Selected Ad");
JButton printAd=new JButton("Print Selected Ad");
JButton removeAll=new JButton("Remove All");
// store information about webAd price stats
BigDecimal low,high,avg,sum=new BigDecimal("0");
// jlabel to store the total price
JLabel total=new JLabel("$0.00",SwingConstants.CENTER);
// we put buttons panel in south panel, so when you resize the window, the bottom widgets will always be centered
JPanel southPanel=new JPanel(new FlowLayout(FlowLayout.CENTER));
// jpanel to put six buttons and one label
JPanel buttonsPanel=new JPanel();
// we use group layout to precisely control the location and gap of each button
GroupLayout layout=new GroupLayout(buttonsPanel);
// use numberformat to format bigdecimal
static NumberFormat currencyFormat=NumberFormat.getCurrencyInstance(Locale.US);
// use jFrame to show the window of "view selected ad"
JFrame jFrame;
// sound effects for buttons
File file;
AudioInputStream audioInputStream;
Clip clip;
// when web ad changes, we update the jtable
void updateTable(){
// clear previous jtable data
data.setRowCount(0);
sum=new BigDecimal("0");
// get low price, high price and avg price
for(int i=0;i<cart.size();i++){
WebAd webAd=cart.get(i);
if(i==0){
low=new BigDecimal(webAd.getAdjustedPrice().toPlainString());
high=new BigDecimal(webAd.getAdjustedPrice().toPlainString());
sum=new BigDecimal(webAd.getAdjustedPrice().toPlainString());
}else {
if(low.compareTo(webAd.getAdjustedPrice())>0) low=new BigDecimal(webAd.getAdjustedPrice().toPlainString());
if(high.compareTo(webAd.getAdjustedPrice())<0) high=new BigDecimal(webAd.getAdjustedPrice().toPlainString());
sum=sum.add(webAd.getAdjustedPrice());
}
data.addRow(webAd.getTableRow());
}
if(cart.size()>0) avg=sum.divide(new BigDecimal(cart.size()+""), MathContext.DECIMAL32);
// update new price to jlabel
total.setText(currencyFormat.format(sum));
}
Store(){
// basic information about top level jframe
super("My WebAd Store");
setSize(800, 600);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
// set titel for jtable
data.setColumnIdentifiers(new String[]{"WebAd Price","Discount","Tax","Total"});
// put scrollpane in the center
add(jScrollPane,BorderLayout.CENTER);
buttonsPanel.setLayout(layout);
// create style for the total price label
total.setPreferredSize(new Dimension(200, 100));
total.setBackground(Color.black);
total.setForeground(Color.green);
total.setOpaque(true);
total.setFont(new Font("Courier", Font.BOLD, 30));
// we use group layout to bind the size of all buttons, so we just need to set size for a random button here
addWebAd.setPreferredSize(new Dimension(150, 50 ));
// create gaps for the group layout
layout.setAutoCreateGaps(true);
layout.setAutoCreateContainerGaps(true);
// listener for "add webAd"
addWebAd.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e){
cart.add(new WebAd());
updateTable();
// use exception handling, so the program can run successfully even if you lose those attached sound files
try {
file=new File("cling_1.wav");
audioInputStream= AudioSystem.getAudioInputStream(file);
clip=AudioSystem.getClip();
clip.open(audioInputStream);
clip.setMicrosecondPosition(0);
clip.start();
}catch (Exception ex){
System.out.println(ex);
}
}
});
// listener for "show stats"
showStats.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
// use exception handling, so the program can run successfully even if you lose those attached sound files
try {
file=new File("camera-focus-1.wav");
audioInputStream= AudioSystem.getAudioInputStream(file);
clip=AudioSystem.getClip();
clip.open(audioInputStream);
clip.setMicrosecondPosition(0);
clip.start();
}catch (Exception ex){
System.out.println(ex);
}
if(cart.size()==0){
JOptionPane.showMessageDialog(null, "No web ads.","Error",JOptionPane.ERROR_MESSAGE);
}else {
String message="Low Price = "+currencyFormat.format(low)+"\nHigh Price = "+currencyFormat.format(high)+"\nAvg Price = "+currencyFormat.format(avg);
JOptionPane.showMessageDialog(null, message,"Summary Statistics",JOptionPane.PLAIN_MESSAGE);
}
}
});
// listener for "remove selected ad"
removeWebAd.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
// use exception handling, so the program can run successfully even if you lose those attached sound files
try {
file=new File("glass_breaking_1.wav");
audioInputStream= AudioSystem.getAudioInputStream(file);
clip=AudioSystem.getClip();
clip.open(audioInputStream);
clip.setMicrosecondPosition(0);
clip.start();
}catch (Exception ex){
System.out.println(ex);
}
int idx=table.getSelectedRow();
if(idx>-1){
cart.remove(idx);
updateTable();
}else {
JOptionPane.showMessageDialog(null, "No selected web ad.","Error",JOptionPane.ERROR_MESSAGE);
}
}
});
// listener for "print selected ad"
printAd.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e){
// use exception handling, so the program can run successfully even if you lose those attached sound files
try {
file=new File("coin_1.wav");
audioInputStream= AudioSystem.getAudioInputStream(file);
clip=AudioSystem.getClip();
clip.open(audioInputStream);
clip.setMicrosecondPosition(0);
clip.start();
}catch (Exception ex){
System.out.println(ex);
}
int idx=table.getSelectedRow();
if(idx>-1){
PrinterJob printerJob=PrinterJob.getPrinterJob();
printerJob.setPrintable(cart.get(idx));
if(printerJob.printDialog()){
try {
printerJob.print();
}catch (PrinterException exc){
System.out.println("Printer dead"+exc);
}
}
}else {
JOptionPane.showMessageDialog(null, "No selected web ad.","Error",JOptionPane.ERROR_MESSAGE);
}
}
});
// listener for "view selected ad"
viewAd.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
// use exception handling, so the program can run successfully even if you lose those attached sound files
try {
file=new File("cling_2.wav");
audioInputStream= AudioSystem.getAudioInputStream(file);
clip=AudioSystem.getClip();
clip.open(audioInputStream);
clip.setMicrosecondPosition(0);
clip.start();
}catch (Exception ex){
System.out.println(ex);
}
int idx=table.getSelectedRow();
if(idx>-1){
jFrame=new JFrame();
jFrame.setTitle("WebAd "+(idx+1));
jFrame.add(cart.get(idx),BorderLayout.CENTER);
jFrame.pack();
jFrame.setVisible(true);
jFrame.setDefaultCloseOperation(DISPOSE_ON_CLOSE);
}else {
JOptionPane.showMessageDialog(null, "No selected web ad.","Error",JOptionPane.ERROR_MESSAGE);
}
}
});
// listener for "remove all"
removeAll.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
// use exception handling, so the program can run successfully even if you lose those attached sound files
try {
file=new File("zipper_bag_2.wav");
audioInputStream= AudioSystem.getAudioInputStream(file);
clip=AudioSystem.getClip();
clip.open(audioInputStream);
clip.setMicrosecondPosition(0);
clip.start();
}catch (Exception ex){
System.out.println(ex);
}
// clear the data in web ad list
cart.clear();
updateTable();
}
});
// set group layout for buttons and label
layout.setHorizontalGroup(
layout.createSequentialGroup()
.addGroup(layout.createParallelGroup()
.addComponent(addWebAd)
.addComponent(viewAd)
)
.addGroup(layout.createParallelGroup()
.addComponent(removeWebAd)
.addComponent(printAd)
)
.addGroup(layout.createParallelGroup()
.addComponent(showStats)
.addComponent(removeAll)
)
.addComponent(total,0, GroupLayout.PREFERRED_SIZE,
GroupLayout.PREFERRED_SIZE)
);
// we make the size of all of the buttons exactly the same
layout.linkSize(addWebAd,removeWebAd,showStats,viewAd,printAd,removeAll);
layout.setVerticalGroup(
layout.createSequentialGroup()
.addGroup(
layout.createParallelGroup(GroupLayout.Alignment.CENTER)
.addGroup(layout.createSequentialGroup()
.addGroup(layout.createParallelGroup()
.addComponent(addWebAd)
.addComponent(removeWebAd)
.addComponent(showStats)
)
.addGroup(layout.createParallelGroup()
.addComponent(viewAd)
.addComponent(printAd)
.addComponent(removeAll)
)
)
.addComponent(total,0, GroupLayout.PREFERRED_SIZE,
GroupLayout.PREFERRED_SIZE)
)
);
southPanel.add(buttonsPanel);
add(southPanel,BorderLayout.SOUTH);
setVisible(true);
}
public static void main(String[] args) {
Store app=new Store();
}
}
class WebAd extends JPanel implements Printable {
Random random=new Random();
static NumberFormat percentageFormat=NumberFormat.getPercentInstance();
static NumberFormat percentageFormatWithFraction=NumberFormat.getPercentInstance();
static NumberFormat currencyFormat=NumberFormat.getCurrencyInstance(Locale.US);
private static BigDecimal taxRate=new BigDecimal("0.0725");
//set the initial position of the phone structure, so we can move it easily
int xPosOfPhonePic=200;
int yPosOfPhonePic=50;
String taglineText="Better Protection!";//initial tagline information
int currentID;//product number
Color phoneFrontPane=new Color( 0, 0, 0, 255);//set phone front pane color to black
Color bg=new Color( 255, 204, 204, 255);//set Ad panel background color to pink
public static BigDecimal getTaxRate() {
return taxRate;
}
public static void setTaxRate(BigDecimal taxRate) {
WebAd.taxRate = taxRate;
}
private BigDecimal unitPrice=new BigDecimal(random.nextInt(100)+"."+random.nextInt(100));
private BigDecimal discountRate=new BigDecimal("0."+random.nextInt(5)+random.nextInt(10));
// public BigDecimal getUnitPrice() {
// return unitPrice;
// }
public void setUnitPrice(BigDecimal unitPrice) {
this.unitPrice = unitPrice;
}
public BigDecimal getDiscountRate() {
return discountRate;
}
public void setDiscountRate(BigDecimal discountRate) {
this.discountRate = discountRate;
}
public BigDecimal getAdjustedPrice() {
return adjustedPrice;
}
public void setAdjustedPrice(BigDecimal adjustedPrice) {
this.adjustedPrice = adjustedPrice;
}
private BigDecimal adjustedPrice=unitPrice.multiply(new BigDecimal("1").subtract(discountRate)).multiply(new BigDecimal("1").add(taxRate));
WebAd(){
percentageFormatWithFraction.setMinimumFractionDigits(2);
currentID=random.nextInt(1000)+1000;
// set the size, so we can view or print it easily
setSize(320, 320);
}
@Override
public Dimension getPreferredSize() {
return new Dimension(320, 320);
}
// use this method to populate jtable
String[] getTableRow(){
return new String[]{currencyFormat.format(unitPrice),percentageFormat.format(discountRate),percentageFormatWithFraction.format(taxRate.doubleValue()),currencyFormat.format(adjustedPrice)};
}
@Override
public void paintComponent(Graphics g1d) {
super.paintComponent(g1d);
Graphics2D g = (Graphics2D) g1d;
//set background color to pink
g.setColor(bg);
g.fillRect(0, 0, 320, 320);
//draw title
g.setColor( new Color( 0, 0, 0, 255) );
g.setFont( new Font("Monospaced", 1, 20) );
g.drawString("Smart Phone Case Company", 0, 20);
//draw the phone
g.setColor( new Color(139, 139, 139, 255) );
g.fillRoundRect(xPosOfPhonePic-7, yPosOfPhonePic-7, 100+14, 150+14, 15, 15);
//draw phone silent, volume up and down, and close screen buttons
g.fillRoundRect(xPosOfPhonePic-8, yPosOfPhonePic+10, 1, 10, 1, 1);
g.fillRoundRect(xPosOfPhonePic-8, yPosOfPhonePic+30, 1, 20, 1, 1);
g.fillRoundRect(xPosOfPhonePic-8, yPosOfPhonePic+55, 1, 20, 1, 1);
g.fillRoundRect(xPosOfPhonePic+100+7, yPosOfPhonePic+30, 2, 20, 0, 0);
//draw the phone front pane
g.setColor( new Color(211, 211, 211, 255) );
g.fillRoundRect(xPosOfPhonePic-5, yPosOfPhonePic-5, 100+10, 150+10, 15, 15);
g.setColor(phoneFrontPane);
g.fillRoundRect(xPosOfPhonePic, yPosOfPhonePic, 100, 150, 15, 15);
g.setColor( new Color(211, 211, 211, 255) );
g.fillRoundRect(xPosOfPhonePic+20, yPosOfPhonePic, 60, 12, 15, 15);
g.fillRect(xPosOfPhonePic+20, yPosOfPhonePic, 60, 6);
//draw the phone voice and light listener
g.setColor( new Color( 0, 0, 0, 255) );
g.fillRoundRect(xPosOfPhonePic+40-4, yPosOfPhonePic+3, 20, 3, 0, 0);
g.fillOval(xPosOfPhonePic+40-4+20+4, yPosOfPhonePic+2, 5, 5);
g.setColor( new Color( 255, 255, 255, 255) );
g.drawLine(xPosOfPhonePic+5, yPosOfPhonePic+145, xPosOfPhonePic+95, yPosOfPhonePic+5);
g.setColor( new Color( 0, 0, 0, 255) );
g.fillRect(xPosOfPhonePic+20, yPosOfPhonePic+60, 60, 30);
g.setColor( new Color( 255, 255, 255, 255) );
g.setFont( new Font("Monospaced", 0, 15) );
g.drawString("6.1-inch", xPosOfPhonePic+20, yPosOfPhonePic+80);
//draw other texts
g.setColor( new Color( 0, 0, 0, 255) );
g.setFont( new Font("Monospaced", 2, 15) );
g.drawString(taglineText, xPosOfPhonePic-80, yPosOfPhonePic+180);
g.drawString("Product No."+currentID, 5, 100);
g.drawString("Only "+currencyFormat.format(adjustedPrice), 10, 150);
g.drawString("(Adjusted Price)", 10, 170);
g.setFont( new Font("Monospaced", 0, 15) );
g.drawString("The fictitious phone case company", 10, 290);
g.drawString("www.smartphonecase.com", 10, 310);
}
@Override
public int print(Graphics graphics, PageFormat pageFormat, int pageIndex) throws PrinterException {
System.out.println("printing");
if(pageIndex>0){
return NO_SUCH_PAGE;
}
Graphics2D graphics2D=(Graphics2D)graphics;
// change the printing margin
graphics2D.translate(pageFormat.getImageableX()+50, pageFormat.getImageableY()+50);
paint(graphics2D);
return PAGE_EXISTS;
}
}