-
Notifications
You must be signed in to change notification settings - Fork 0
/
cd_shelf_gui.java
488 lines (446 loc) · 18.5 KB
/
cd_shelf_gui.java
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
478
479
480
481
482
483
484
485
486
487
488
import java.awt.Color;
import java.io.IOException;
import javax.swing.*;
import javax.swing.AbstractButton;
import javax.swing.ButtonModel;
import javax.swing.JFrame;
import javax.swing.JToggleButton;
import javax.swing.event.ChangeEvent;
import javax.swing.event.ChangeListener;
import javax.swing.event.TableModelEvent;
import javax.swing.event.TableModelListener;
import javax.swing.table.TableModel;
public class cd_shelf_gui {
// the cd_shelf has a gui that allows the user to add, remove, search and print
// cds. The cds are diplayed in a table and there are
// Buttons to add, remove, search and print cds. The user can also sort the cds
// by price or artist.
public static void main(String[] args) {
// create a new cd_shelf
cd_shelf shelf = new cd_shelf(100);
// create a new JFrame
JFrame frame = new JFrame("CD Shelf");
// set the size of the frame
// try {
// UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
// } catch (Exception e) {
// }
frame.setSize(500, 650);
// create a new JPanel
JPanel panel = new JPanel();
// create a new JLabel
JLabel label = new JLabel("CD Shelf");
// add the label to the panel
panel.add(label);
Color color = new Color(255, 239, 213);
frame.getContentPane().setBackground(color);
// JToggleBUtton to toggle dark mode
JToggleButton darkMode = new JToggleButton("Dark Mode");
panel.add(darkMode);
// set the default state of the toggle button to be not selected
darkMode.setSelected(false);
// Change the panel background color when the toggle button is selected
darkMode.addChangeListener(
new ChangeListener() {
@Override
public void stateChanged(ChangeEvent e) {
AbstractButton abstractButton = (AbstractButton) e.getSource();
ButtonModel buttonModel = abstractButton.getModel();
boolean selected = buttonModel.isSelected();
if (selected) {
panel.setBackground(Color.BLACK);
label.setForeground(Color.WHITE);
} else {
panel.setBackground(Color.WHITE);
label.setForeground(Color.BLACK);
}
}
});
// create a new JTable
JTable table = new JTable(100, 5);
// remame the columns of the table
table.getColumnModel().getColumn(0).setHeaderValue("Title");
table.getColumnModel().getColumn(1).setHeaderValue("Artist");
table.getColumnModel().getColumn(2).setHeaderValue("Tracks");
table.getColumnModel().getColumn(3).setHeaderValue("Price");
table.getColumnModel().getColumn(4).setHeaderValue("Year");
// the table is ///not/// editable
table.setEnabled(true);
// create a new JScrollPane
JScrollPane scroll = new JScrollPane(table);
// add the scroll to the panel
panel.add(scroll);
// create a new JButton
JButton add = new JButton("Add");
// add the add button to the panel
panel.add(add);
// create a new JButton
JButton addFromTable = new JButton("Add from table");
// add the addFromTable button to the panel
panel.add(addFromTable);
// create a new JButton
JButton load = new JButton("Load from file");
// add the load button to the panel
panel.add(load);
// create a new JButton
JButton save = new JButton("Save to file");
// add the save button to the panel
panel.add(save);
// create a new JButton
JButton remove = new JButton("Remove");
// add the remove button to the panel
panel.add(remove);
// create a new JButton
JButton search = new JButton("Search");
// add the search button to the panel
panel.add(search);
// create a new JButton
JButton print = new JButton("Print");
// add the print button to the panel
panel.add(print);
// create a new JButton
JButton refresh = new JButton("Refresh");
// add the refresh button to the panel
panel.add(refresh);
// create a new JButton
JButton sortPrice = new JButton("Sort by price");
// add the sortPrice button to the panel
panel.add(sortPrice);
// create a new JButton
JButton removeDuplicates = new JButton("Remove duplicates");
// add the removeDuplicates button to the panel
panel.add(removeDuplicates);
// create a new JButton
JButton sortArtist = new JButton("Sort Artist");
// add the sortArtist button to the panel
panel.add(sortArtist);
// create a new JButton
JButton sortTitle = new JButton("Sort Name");
panel.add(sortTitle);
JButton sortYear = new JButton("Sort Year");
panel.add(sortYear);
// add the panel to the frame
frame.add(panel);
frame.setResizable(true);
// make the frame half the size of the screen
frame.setLocationRelativeTo(null);
// make the frame visible
frame.setVisible(true);
// ask if the user returns or is new
String[] options = new String[] {"New", "Returning"};
int response =
JOptionPane.showOptionDialog(
null,
"Are you new or returning?",
"New or Returning",
JOptionPane.DEFAULT_OPTION,
JOptionPane.PLAIN_MESSAGE,
null,
options,
options[0]);
// if the user is new, ask for their name and create a new account
if (response == 0) {
String name = JOptionPane.showInputDialog("What is your name?");
String password = JOptionPane.showInputDialog("What is your password?");
String password2 = JOptionPane.showInputDialog("Please re-enter your password");
if (password.equals(password2)) {
shelf.register(name, password);
JOptionPane.showMessageDialog(null, "Account created");
} else {
JOptionPane.showMessageDialog(null, "Passwords do not match");
}
}
// if the user is returning, ask for their name and password
if (response == 1) {
String name = JOptionPane.showInputDialog("What is your name?");
String password = JOptionPane.showInputDialog("What is your password?");
// if the name and password match, allow the user to access the shelf
if (shelf.login(name, password)) {
JOptionPane.showMessageDialog(null, "Welcome back " + name);
} else {
JOptionPane.showMessageDialog(null, "Incorrect name or password");
// when the message is closed, the program will exit
System.exit(0);
}
}
// if one of the above messages is closed or nothing is entered, the program will exit
if (response == -1 || response == JOptionPane.CLOSED_OPTION) {
System.exit(0);
}
// on every click of the table, it refreshes the table
table.addMouseListener(
new java.awt.event.MouseAdapter() {
@Override
public void mouseClicked(java.awt.event.MouseEvent evt) {
for (int i = 0; i < shelf.getNumcds(); i++) {
table.setValueAt(shelf.getCds()[i].getTitle(), i, 0);
table.setValueAt(shelf.getCds()[i].getArtist(), i, 1);
table.setValueAt(shelf.getCds()[i].getTracks(), i, 2);
table.setValueAt(shelf.getCds()[i].getPrice(), i, 3);
table.setValueAt(shelf.getCds()[i].getYear(), i, 4);
}
}
});
// after an user has finished editing a column, it will update the shelf
table
.getModel()
.addTableModelListener(
new TableModelListener() {
@Override
public void tableChanged(TableModelEvent e) {
int row = e.getFirstRow();
int column = e.getColumn();
TableModel model = (TableModel) e.getSource();
String columnName = model.getColumnName(column);
Object data = model.getValueAt(row, column);
if (columnName.equals("Title")) {
shelf.getCds()[row].setTitle((String) data);
}
if (columnName.equals("Artist")) {
shelf.getCds()[row].setArtist((String) data);
}
if (columnName.equals("Tracks")) {
shelf.getCds()[row].setTracks((int) data);
}
if (columnName.equals("Price")) {
shelf.getCds()[row].setPrice((double) data);
}
if (columnName.equals("Year")) {
shelf.getCds()[row].setYear((int) data);
}
}
});
add.addActionListener(
new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
// create a new cd
String title = JOptionPane.showInputDialog("Enter the title of the cd");
String artist = JOptionPane.showInputDialog("Enter the artist of the cd");
int tracks =
Integer.parseInt(
JOptionPane.showInputDialog("Enter the number of tracks on the cd"));
double price =
Double.parseDouble(JOptionPane.showInputDialog("Enter the price of the cd"));
int year =
Integer.parseInt(
JOptionPane.showInputDialog("Enter the year of publication of the cd"));
cd c = new cd(title, artist, tracks, price, year);
// add the cd to the shelf
shelf.add(c);
// update the table and write all the cds to the table, only replace duplicates
for (int i = 0; i < shelf.getNumcds(); i++) {
table.setValueAt(shelf.getCds()[i].getTitle(), i, 0);
table.setValueAt(shelf.getCds()[i].getArtist(), i, 1);
table.setValueAt(shelf.getCds()[i].getTracks(), i, 2);
table.setValueAt(shelf.getCds()[i].getPrice(), i, 3);
table.setValueAt(shelf.getCds()[i].getYear(), i, 4);
}
}
});
// button.actionPerformed
remove.addActionListener(
new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
// remove the cd from the shelf
int i =
Integer.parseInt(
JOptionPane.showInputDialog("Enter the index of the cd you want to remove"));
shelf.remove(i);
// update the table
for (int z = 0; z < shelf.getNumcds(); z++) {
table.setValueAt(shelf.getCds()[z].getTitle(), z, 0);
table.setValueAt(shelf.getCds()[z].getArtist(), z, 1);
table.setValueAt(shelf.getCds()[z].getTracks(), z, 2);
table.setValueAt(shelf.getCds()[z].getPrice(), z, 3);
table.setValueAt(shelf.getCds()[z].getYear(), z, 4);
}
}
});
// button.actionPerformed
search.addActionListener(
new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
// search for a cd by title
int result = shelf.search(JOptionPane.showInputDialog("Enter your search query"));
// if the cd is found, display the cd
if (result != -1) {
JOptionPane.showMessageDialog(
null,
shelf.getCds()[result].getTitle()
+ " "
+ shelf.getCds()[result].getArtist()
+ " "
+ shelf.getCds()[result].getTracks()
+ " "
+ shelf.getCds()[result].getPrice());
}
}
});
// button.actionPerformed
print.addActionListener(
new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
// print the shelf
shelf.print();
}
});
// button.actionPerformed
load.addActionListener(
new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
// load the shelf from a file
try {
shelf.load();
shelf.print();
} catch (IOException e) {
e.printStackTrace();
}
}
});
// button.actionPerformed
save.addActionListener(
new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
// save the shelf to a file
try {
shelf.save();
// print the shelf
shelf.print();
} catch (IOException e) {
e.printStackTrace();
}
}
});
refresh.addActionListener(
new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
// refresh the table
for (int i = 0; i < shelf.getNumcds(); i++) {
table.setValueAt(shelf.getCds()[i].getTitle(), i, 0);
table.setValueAt(shelf.getCds()[i].getArtist(), i, 1);
table.setValueAt(shelf.getCds()[i].getTracks(), i, 2);
table.setValueAt(shelf.getCds()[i].getPrice(), i, 3);
table.setValueAt(shelf.getCds()[i].getYear(), i, 4);
}
}
});
removeDuplicates.addActionListener(
new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
// remove duplicates
try {
shelf.removeDuplicates();
} catch (IOException e) {
e.printStackTrace();
}
// refresh the table
for (int i = 0; i < shelf.getNumcds(); i++) {
table.setValueAt(shelf.getCds()[i].getTitle(), i, 0);
table.setValueAt(shelf.getCds()[i].getArtist(), i, 1);
table.setValueAt(shelf.getCds()[i].getTracks(), i, 2);
table.setValueAt(shelf.getCds()[i].getPrice(), i, 3);
table.setValueAt(shelf.getCds()[i].getYear(), i, 4);
}
}
});
// map the buttons to quicksort to the functions
sortArtist.addActionListener(
new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
// sort the shelf by title
shelf.quickSortArtist(0, shelf.getNumcds() - 1);
// refresh the table
for (int i = 0; i < shelf.getNumcds(); i++) {
table.setValueAt(shelf.getCds()[i].getTitle(), i, 0);
table.setValueAt(shelf.getCds()[i].getArtist(), i, 1);
table.setValueAt(shelf.getCds()[i].getTracks(), i, 2);
table.setValueAt(shelf.getCds()[i].getPrice(), i, 3);
table.setValueAt(shelf.getCds()[i].getYear(), i, 4);
}
}
});
// map the buttons to quicksort to the functions
sortTitle.addActionListener(
new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
// sort the shelf by title
shelf.quickSortTitle(0, shelf.getNumcds() - 1);
// refresh the table
for (int i = 0; i < shelf.getNumcds(); i++) {
table.setValueAt(shelf.getCds()[i].getTitle(), i, 0);
table.setValueAt(shelf.getCds()[i].getArtist(), i, 1);
table.setValueAt(shelf.getCds()[i].getTracks(), i, 2);
table.setValueAt(shelf.getCds()[i].getPrice(), i, 3);
table.setValueAt(shelf.getCds()[i].getYear(), i, 4);
}
}
});
// map the buttons to quicksort to the functions
sortPrice.addActionListener(
new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
// sort the shelf by title
shelf.quickSortPrice(0, shelf.getNumcds() - 1);
// refresh the table
for (int i = 0; i < shelf.getNumcds(); i++) {
table.setValueAt(shelf.getCds()[i].getTitle(), i, 0);
table.setValueAt(shelf.getCds()[i].getArtist(), i, 1);
table.setValueAt(shelf.getCds()[i].getTracks(), i, 2);
table.setValueAt(shelf.getCds()[i].getPrice(), i, 3);
table.setValueAt(shelf.getCds()[i].getYear(), i, 4);
}
}
});
// map the buttons to quicksort to the functions
sortYear.addActionListener(
new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
// sort the shelf by title
shelf.quickSortYear(0, shelf.getNumcds() - 1);
// refresh the table
for (int i = 0; i < shelf.getNumcds(); i++) {
table.setValueAt(shelf.getCds()[i].getTitle(), i, 0);
table.setValueAt(shelf.getCds()[i].getArtist(), i, 1);
table.setValueAt(shelf.getCds()[i].getTracks(), i, 2);
table.setValueAt(shelf.getCds()[i].getPrice(), i, 3);
table.setValueAt(shelf.getCds()[i].getYear(), i, 4);
}
}
});
// add entries which are inside the table but not in the shelf to the shelf
addFromTable.addActionListener(
new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
System.out.println("addFromTable");
// print the new entries with all the information about name, artist, tracks, price and
// year
for (int i = 0; i < table.getRowCount(); i++) {
if (table.getValueAt(i, 0) != null
&& table.getValueAt(i, 1) != null
&& table.getValueAt(i, 2) != null
&& table.getValueAt(i, 3) != null
&& table.getValueAt(i, 4) != null) {
System.out.println(
table.getValueAt(i, 0)
+ " "
+ table.getValueAt(i, 1)
+ " "
+ table.getValueAt(i, 2)
+ " "
+ table.getValueAt(i, 3)
+ " "
+ table.getValueAt(i, 4)
+ " ");
// add the new entries to the shelf
shelf.addcd(
(String) table.getValueAt(i, 0),
(String) table.getValueAt(i, 1),
(int) Integer.valueOf(table.getValueAt(i, 2).toString()),
(double) Double.parseDouble(table.getValueAt(i, 3).toString()),
(int) Integer.valueOf(table.getValueAt(i, 4).toString()));
}
}
}
});
}
}