-
Notifications
You must be signed in to change notification settings - Fork 0
/
Frame.java
395 lines (320 loc) · 12 KB
/
Frame.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
import java.awt.BorderLayout;
import java.awt.FlowLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.util.ArrayList;
import javax.swing.*;
public class Frame extends JFrame implements ActionListener, MouseListener{
private JButton rentMovie;
private JButton rentGame;
private JButton returnItem;
JMenuItem initialize = new JMenuItem("Initialize item list");
JMenuItem load = new JMenuItem("Load current rents");
JMenuItem save = new JMenuItem("Save current rents");
JTabbedPane tabs;
private static mainApp methods = new mainApp();
private static JMenuBar menu = new JMenuBar();
private static JFileChooser explorer = new JFileChooser();
private static ArrayList<Info> Movies = new ArrayList<>();
private static ArrayList<Info> Games = new ArrayList<>();
private static ArrayList<RentInfo> Rented = new ArrayList<>();
private static DefaultListModel movieList = new DefaultListModel();
private static DefaultListModel gameList = new DefaultListModel();
private DefaultListModel rentList = new DefaultListModel();
JPanel moviesTab = new JPanel();
JPanel gamesTab = new JPanel();
JPanel rentsTab = new JPanel();
private static String path;
private static String rentalsPath;
private static Info currentMovie;
private static Info currentGame;
private static RentInfo currentRent;
private JList mList; // movie titles
private JList gList; // game titles
private JList rList; // rental titles
/*
Initializes the Jlists and the DefaultListModel objects for the items
*/
public void setItemList(ArrayList<Info> list){
try {
if (list.get(0).getClass().getName().equalsIgnoreCase("movie")) {
this.Movies = list;
for(Info i:Movies){
movieList.addElement(i.getTitle());
}
} else if (list.get(0).getClass().getName().equalsIgnoreCase("game")) {
this.Games = list;
for(Info i:Games){
gameList.addElement(i.getTitle());
}
}
}catch(IndexOutOfBoundsException e) {
message("Error", e.toString());
}
}
/*
Initializes the Jlists and the DefaultListModel objects for the rentals
*/
public void setRented(ArrayList<RentInfo> list){
this.Rented = list;
for(RentInfo i:Rented){
rentList.addElement(i.getObject().getTitle());
}
}
public Frame(){
super("Video club");
setDefaultLookAndFeelDecorated(true);
setBounds(150, 150, 500, 400);
setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
addMenu();
addTabs();
setVisible(true);
}
/*
Adds the JMenu object to the mainframe
*/
private void addMenu(){
this.setJMenuBar(menu);
JMenu items = new JMenu("Items");
menu.add(items);
initialize.addActionListener(this);
items.add(initialize);
items.addSeparator();
JMenu rents = new JMenu("Rents");
menu.add(rents);
load.addActionListener(this);
rents.add(load);
rents.addSeparator();
save.addActionListener(this);
rents.add(save);
}
/*
Adds the tabs to the mainframe
*/
private void addTabs(){
tabs = new JTabbedPane(JTabbedPane.TOP,JTabbedPane.SCROLL_TAB_LAYOUT);
gamesTab.setLayout(new FlowLayout());
tabs.addTab("Movies", moviesTab);
tabs.addTab("Games",gamesTab);
moviesTab.setLayout(new BorderLayout());
gamesTab.setLayout(new BorderLayout());
rentMovie = new JButton("Rent Movie");
moviesTab.add(rentMovie, BorderLayout.EAST);
rentMovie.setEnabled(false);
rentGame = new JButton("Rent Game");
gamesTab.add(rentGame, BorderLayout.EAST);
rentGame.setEnabled(false);
returnItem = new JButton("Return");
rentsTab.add(returnItem,BorderLayout.EAST);
returnItem.setEnabled(false);
for(Info i:Movies){
movieList.addElement(i.getTitle());
}
mList = new JList(movieList);
moviesTab.add(mList);
mList.addListSelectionListener(lse -> {});
JScrollPane mscroller = new JScrollPane(mList);
moviesTab.add(mscroller);
mList.addMouseListener(this);
for(Info i:Games){
gameList.addElement(i.getTitle());
}
gList = new JList(gameList);
gamesTab.add(gList);
JScrollPane gscroller = new JScrollPane(gList);
gamesTab.add(gscroller);
gList.addMouseListener(this);
setRentTab();
setVisible(true);
add(tabs);
}
/*
Opens the JFileChooser so the user can select a file
*/
private String FileExplorer(){
explorer.setCurrentDirectory(new java.io.File("."));
explorer.setDialogTitle("Choose file.");
explorer.setFileSelectionMode(JFileChooser.FILES_AND_DIRECTORIES);
if (explorer.showOpenDialog(null) == JFileChooser.APPROVE_OPTION) {
path =explorer.getSelectedFile().getPath().toString();
} else {
message("Error", "No Selection!");
path=" ";
}
return path;
}
/*
Prints a message.
*/
public void message(String title, String text){
JOptionPane.showMessageDialog(null, text, title, JOptionPane.INFORMATION_MESSAGE);
}
/*
Gets input from the user.
*/
public String input(String title, String message){
return JOptionPane.showInputDialog(this,message,title,JOptionPane.INFORMATION_MESSAGE);
}
/*
Rents an item.
*/
public void Rent(Info current){
try {
int response = JOptionPane.showConfirmDialog(this, current.toString() + "\nDo you want to rent this item?\n", "Rent", 2, 3, new ImageIcon(current.getIconPath()));
if (response == JOptionPane.OK_OPTION && current.getAvailable() > 0) {
String name = JOptionPane.showInputDialog(this, "Insert your Username:");
if (name.length() > 0) {
String phone = JOptionPane.showInputDialog(this, "Insert your phone:");
if(phone.length()>=10){
methods.RentData(current, name , phone);
}else{
message("Error","Wrong phone format!");
}
}
}else if(response == JOptionPane.OK_OPTION){
message("Cannot complete purchase", "There are no available copies left ");
}
refreshList();
}catch(Exception e){
message("Information","Go to item and click refresh to continue!");
}
refreshList();
}
/*
Returns a rented Item.
*/
public void Return(RentInfo current){
int response = JOptionPane.showConfirmDialog(this, current.toString() + "\nDo you want to return this rent?\n", "Rent", 2);
if(response==JOptionPane.OK_OPTION) {
Rented.remove(current);
if(current.getObject().getClass().getName().equalsIgnoreCase("movie")) {
for(Info i: Movies){
if(current.getObject().getTitle().equals(i.getTitle())){
i.setAvailable(i.getAvailable()+1);
}
}
}else{
for(Info i: Games){
if(current.getObject().getTitle().equals(i.getTitle())){
i.setAvailable(i.getAvailable()+1);
}
}
}
while (true) {
try {
int days = Integer.parseInt(input("Days", "Enter the days of possecion"));
methods.CalculateCost(current, days);
break;
} catch (Exception e) {
message("Error", "Invalid input!");
}
}
message("Congratulations", "Your payment is done. The final cost is: " + current.getCost());
Rented.remove(current);
//methods.save(Rented,rentalsPath);
refreshList();
}
}
public void actionPerformed(ActionEvent e){
if(e.getSource()==rentMovie){
Rent(currentMovie);
rentMovie.setEnabled(false);
}else if(e.getSource()==rentGame){
Rent(currentGame);
rentGame.setEnabled(false);
}else if(e.getSource()==returnItem){
Return(currentRent);
returnItem.setEnabled(false);
}
if(e.getSource()==initialize){
methods.Initialize(FileExplorer());
initialize.setEnabled(false);
}else if(e.getSource()==load){
rentalsPath = FileExplorer();
methods.LoadRents(rentalsPath);
load.setEnabled(false);
}else if(e.getSource()==save){
methods.save(Rented,FileExplorer());
}
}
@Override
public void mouseClicked(MouseEvent e){
int i;
int y;
int z;
if (mList.getSelectedIndex()!=-1){
rentMovie.setEnabled(true);
i=mList.getSelectedIndex();
currentMovie = Movies.get(i);
if(e.getClickCount()==2){
Rent(currentMovie);
}
}else if (gList.getSelectedIndex()!=-1){
rentGame.setEnabled(true);
y=gList.getSelectedIndex();
currentGame=Games.get(y);
if(e.getClickCount()==2){
Rent(currentGame);
}
}else if (rList.getSelectedIndex()!=-1){
returnItem.setEnabled(true);
z=rList.getSelectedIndex();
currentRent = Rented.get(z);
if(e.getClickCount()==2){
Return(currentRent);
}
}
}
/*
Deletes the previous JLists and Models and initializes
them again with the changes that have been made to them.
*/
public void refreshList(){
rentGame.setEnabled(false);
rentMovie.setEnabled(false);
returnItem.setEnabled(false);
movieList.clear();
mList.removeAll();
gameList.clear();
gList.removeAll();
rentList.clear();
rList.removeAll();
rentsTab.removeAll();
if(path.endsWith("ITEM_LIST.txt")){
methods.Initialize(path.replace("ITEM_LIST.txt","Refresh.txt"));
}else if(path.endsWith("RENT_LIST.txt")){
methods.Initialize(path.replace("RENT_LIST.txt","Refresh.txt"));
}
setRentTab();
}
/*
Sets the Rentals tab.
*/
public void setRentTab(){
tabs.addTab("Rentals",rentsTab);
rentsTab.setLayout(new BorderLayout());
returnItem = new JButton("Return");
rentsTab.add(returnItem,BorderLayout.EAST);
returnItem.setEnabled(false);
for(RentInfo i:Rented){
rentList.addElement(i.getObject().getTitle());
}
rList = new JList(rentList);
rentsTab.add(rList);
JScrollPane rscroller = new JScrollPane(rList);
rentsTab.add(rscroller);
rList.addMouseListener(this);
returnItem.addActionListener(this);
setVisible(true);
}
@Override
public void mousePressed(MouseEvent e) {}
@Override
public void mouseReleased(MouseEvent e) {}
@Override
public void mouseEntered(MouseEvent e) {}
@Override
public void mouseExited(MouseEvent e) {}
}