-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathAdminGUI.java
More file actions
399 lines (329 loc) · 16.8 KB
/
Copy pathAdminGUI.java
File metadata and controls
399 lines (329 loc) · 16.8 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
import javax.swing.*;
import javax.swing.border.Border;
import java.awt.*;
import java.io.BufferedReader;
import java.io.File;
import java.io.FileReader;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Collections;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import javax.imageio.ImageIO;
public class AdminGUI extends JFrame {
private LoginSignupApp app;
private JPanel mainPanel;
private JList<String> userList;
private JList<String> paymentHistoryList;
private JList<String> returnedLaptopsList;
private DefaultListModel returnedLaptopsListModel;
private DefaultListModel userListModel;
private Image backgroundImage;
private Logic logic;
private Menu menu;
private JButton updateUserListButton;
private JButton updateRentHistoryButton;
private JButton updateReturnedLaptopsButton;
public AdminGUI(LoginSignupApp app) {
this.app = app;
this.userListModel = new DefaultListModel<>(); // Initialize the DefaultListModel
this.userList = new JList<>(userListModel); // Assign the DefaultListModel to the JList
this.returnedLaptopsListModel = new DefaultListModel<>();
this.returnedLaptopsList = new JList<>(returnedLaptopsListModel);
this.logic = new Logic(menu);
mainPanel = new JPanel();
mainPanel.setLayout(null);
mainPanel.setPreferredSize(new Dimension(800, 600));
List<String> users = readFromFile("users.txt");
userList = new JList<>(users.toArray(new String[0]));
List<String> payments = readFromFile("rentedLaptops.txt");
paymentHistoryList = new JList<>(payments.toArray(new String[0]));
List<String> returnedLaptops = readFromFile("returnedLaptops.txt");
returnedLaptopsList = new JList<>(returnedLaptops.toArray(new String[0]));
JScrollPane userScrollPane = new JScrollPane(userList);
JScrollPane paymentHistoryScrollPane = new JScrollPane(paymentHistoryList);
userScrollPane.setBounds(10, 45, 200, 500);
paymentHistoryScrollPane.setBounds(230, 45, 200, 500);
JScrollPane returnedLaptopsScrollPane = new JScrollPane(returnedLaptopsList);
returnedLaptopsScrollPane.setBounds(450, 45, 200, 500);
JLabel userListLabel = new JLabel("Users:");
JLabel things = new JLabel("(Select a user to show details)");
userListLabel.setBounds(10, 10, 100, 20);
things.setBounds(10, 25, 200, 20);
JLabel paymentHistoryListLabel = new JLabel("Rent History:");
JLabel thingy = new JLabel("(Laptop ID:Student ID:Price)");
paymentHistoryListLabel.setBounds(230, 10, 200, 20);
thingy.setBounds(230, 25, 200, 20);
JLabel returnedLaptopsListLabel = new JLabel("Returned Laptops:");
JLabel thingz = new JLabel("(Laptop ID:Student ID)");
returnedLaptopsListLabel.setBounds(450, 10, 200, 20);
thingz.setBounds(450, 25, 200, 20);
updateUserListButton = new JButton("Show User Details");
updateUserListButton.setBounds(30, 550, 150, 30);
updateUserListButton.addActionListener(e -> handleUpdateUserList());
updateRentHistoryButton = new JButton("Show Rent Details");
updateRentHistoryButton.setBounds(250, 550, 150, 30);
updateRentHistoryButton.addActionListener(e -> handleUpdateRentHistory());
updateReturnedLaptopsButton = new JButton("Show Returned Details");
updateReturnedLaptopsButton.setBounds(460, 550, 180, 30);
updateReturnedLaptopsButton.addActionListener(e -> handleUpdateReturnedLaptops());
Border border = BorderFactory.createLineBorder(Color.YELLOW, 3);
mainPanel.setBorder(border);
mainPanel.add(userScrollPane);
mainPanel.add(paymentHistoryScrollPane);
mainPanel.add(userListLabel);
mainPanel.add(things);
mainPanel.add(paymentHistoryListLabel);
mainPanel.add(thingy);
mainPanel.add(returnedLaptopsScrollPane);
mainPanel.add(returnedLaptopsListLabel);
mainPanel.add(thingz);
mainPanel.add(updateUserListButton);
mainPanel.add(updateRentHistoryButton);
mainPanel.add(updateReturnedLaptopsButton);
JButton menuButton = new JButton("Logout");
menuButton.setBounds(675, 550, 100, 30);
menuButton.addActionListener(e -> handleMenu()); // Call method for handling menu
mainPanel.add(menuButton);
JButton dataSummary = new JButton("Show Data");
dataSummary.setBounds(670, 500, 110, 25);
dataSummary.addActionListener(e -> handleDataSummary()); // Call method for handling data summary
mainPanel.add(dataSummary);
try {
backgroundImage = ImageIO.read(new File("bg/Adminpic.png"));
} catch (Exception e) {
e.printStackTrace();
}
}
public JPanel getMainPanel() {
return mainPanel;
}
// Read data from file
private List<String> readFromFile(String fileName) {
List<String> data = new ArrayList<>();
try (BufferedReader br = new BufferedReader(new FileReader(fileName))) {
String line;
while ((line = br.readLine()) != null) {
data.add(line);
}
} catch (IOException e) {
e.printStackTrace();
}
return data;
}
// usually handleLogout() but im too lazy to change it
private void handleMenu() {
app.logout();
}
// method for going to datasummary class
private void handleDataSummary() {
DataSummary dataSummary = new DataSummary();
dataSummary.showDataSummary();
}
private void handleUpdateRentHistory() {
int selectedIndex = paymentHistoryList.getSelectedIndex();
if (selectedIndex != -1) {
List<String> rentHistory = readFromFile("rentedLaptops.txt");
String selectedRent = rentHistory.get(selectedIndex);
List<String> rentDetails = Collections.singletonList(selectedRent);
displayRentDetails(rentDetails);
}
}
// Method to display rent details in a separate window
private void displayRentDetails(List<String> rentDetails) {
JFrame rentDetailsFrame = new JFrame("Rent Details");
rentDetailsFrame.setSize(300, 250);
JPanel rentDetailsPanel = new JPanel();
rentDetailsPanel.setLayout(null);
int labelX = 40;
int yPosition = 40;
int labelWidth = 100;
int textFieldWidth = 150;
int height = 20;
int verticalGap = 30;
Border border = BorderFactory.createLineBorder(Color.GREEN, 3);
rentDetailsPanel.setBorder(border);
for (String rent : rentDetails) {
String[] parts = rent.split(",");
if (parts.length >= 3) {
JLabel idLabel = new JLabel("Laptop ID: ");
JLabel nameLabel = new JLabel("Laptop Name: ");
JLabel studentIdLabel = new JLabel("Student ID:");
JLabel priceLabel = new JLabel("Price:");
idLabel.setBounds(labelX, yPosition, labelWidth, height);
nameLabel.setBounds(labelX, yPosition + verticalGap, labelWidth, height);
studentIdLabel.setBounds(labelX, yPosition + (2 * verticalGap), labelWidth, height);
priceLabel.setBounds(labelX, yPosition + (3 * verticalGap), labelWidth, height);
JTextField idField = new JTextField(parts[0]);
JTextField nameField = new JTextField(getLaptopNameByID(parts[0])); // Pass ID to fetch name
JTextField studentIdField = new JTextField(parts[1]);
JTextField priceField = new JTextField(parts[2]);
idField.setBounds(labelX + labelWidth, yPosition, textFieldWidth, height);
nameField.setBounds(labelX + labelWidth, yPosition + verticalGap, textFieldWidth, height);
studentIdField.setBounds(labelX + labelWidth, yPosition + (2 * verticalGap), textFieldWidth, height);
priceField.setBounds(labelX + labelWidth, yPosition + (3 * verticalGap), textFieldWidth, height);
idField.setEditable(false);
nameField.setEditable(false);
studentIdField.setEditable(false);
priceField.setEditable(false);
rentDetailsPanel.add(idLabel);
rentDetailsPanel.add(nameLabel);
rentDetailsPanel.add(studentIdLabel);
rentDetailsPanel.add(priceLabel);
rentDetailsPanel.add(idField);
rentDetailsPanel.add(nameField);
rentDetailsPanel.add(studentIdField);
rentDetailsPanel.add(priceField);
yPosition += 4 * verticalGap; // Move to the next set of details
} else {
JLabel errorLabel = new JLabel("Invalid rent data format");
errorLabel.setBounds(labelX, yPosition, labelWidth, height);
rentDetailsPanel.add(errorLabel);
yPosition += verticalGap; // Move to the next line
}
}
rentDetailsFrame.add(rentDetailsPanel);
rentDetailsFrame.setVisible(true);
rentDetailsFrame.setResizable(false);
rentDetailsFrame.setLocationRelativeTo(null);
}
private String getLaptopNameByID(String id) {
Map<String, Laptop> availableLaptops = logic.initializeAvailableLaptops(); // Fetch available laptops
if (availableLaptops.containsKey(id)) {
return availableLaptops.get(id).getName();
}
return "Unknown";
}
private void handleUpdateReturnedLaptops() {
int selectedIndex = returnedLaptopsList.getSelectedIndex();
if (selectedIndex != -1) {
List<String> returnedLaptops = readFromFile("returnedLaptops.txt");
String selectedReturn = returnedLaptops.get(selectedIndex);
List<String> returnDetails = Collections.singletonList(selectedReturn);
displayReturnedLaptopDetails(returnDetails);
}
}
// Method to display returned laptops details in a separate window
private void displayReturnedLaptopDetails(List<String> returnDetails) {
JFrame returnDetailsFrame = new JFrame("Returned Laptops Details");
returnDetailsFrame.setSize(300, 200);
JPanel returnDetailsPanel = new JPanel();
returnDetailsPanel.setLayout(null);
int labelX = 40;
int yPosition = 40;
int labelWidth = 100;
int textFieldWidth = 150;
int height = 20;
int verticalGap = 30;
Border border = BorderFactory.createLineBorder(Color.GREEN, 3);
returnDetailsPanel.setBorder(border);
for (String returnData : returnDetails) {
String[] parts = returnData.split(",");
if (parts.length >= 2) {
String laptopID = parts[0];
JLabel idLabel = new JLabel("Laptop ID: ");
JLabel laptopNameLabel = new JLabel("Laptop Name: ");
JLabel studentIdLabel = new JLabel("Student ID:");
idLabel.setBounds(labelX, yPosition, labelWidth, height);
laptopNameLabel.setBounds(labelX, yPosition + verticalGap, labelWidth, height);
studentIdLabel.setBounds(labelX, yPosition + 2 * verticalGap, labelWidth, height);
JTextField idField = new JTextField(laptopID);
JTextField laptopNameField = new JTextField(getLaptopNameByID(laptopID)); // Fetch Name
JTextField studentIdField = new JTextField(parts[1]);
idField.setBounds(labelX + labelWidth, yPosition, textFieldWidth, height);
laptopNameField.setBounds(labelX + labelWidth, yPosition + verticalGap, textFieldWidth, height);
studentIdField.setBounds(labelX + labelWidth, yPosition + 2 * verticalGap, textFieldWidth, height);
idField.setEditable(false);
laptopNameField.setEditable(false);
studentIdField.setEditable(false);
returnDetailsPanel.add(idLabel);
returnDetailsPanel.add(laptopNameLabel);
returnDetailsPanel.add(studentIdLabel);
returnDetailsPanel.add(idField);
returnDetailsPanel.add(laptopNameField);
returnDetailsPanel.add(studentIdField);
yPosition += 3 * verticalGap; // Move to the next set of details
} else {
JLabel errorLabel = new JLabel("Invalid return data format");
errorLabel.setBounds(labelX, yPosition, labelWidth, height);
returnDetailsPanel.add(errorLabel);
yPosition += verticalGap; // Move to the next line
}
}
returnDetailsFrame.add(returnDetailsPanel);
returnDetailsFrame.setVisible(true);
returnDetailsFrame.setResizable(false);
returnDetailsFrame.setLocationRelativeTo(null);
}
private void handleUpdateUserList() {
int selectedIndex = userList.getSelectedIndex();
if (selectedIndex != -1) {
List<String> users = readFromFile("users.txt");
String selectedUser = users.get(selectedIndex);
List<String> userDetails = Collections.singletonList(selectedUser);
displayUserDetails(userDetails);
}
}
// method to display user details in a separate window
private void displayUserDetails(List<String> userDetails) {
JFrame userDetailsFrame = new JFrame("User Details");
userDetailsFrame.setSize(300, 260);
JPanel userDetailsPanel = new JPanel();
userDetailsPanel.setLayout(null);
JLabel usernameLabel = new JLabel("Username:");
JLabel passwordLabel = new JLabel("Password:");
JLabel studentIdLabel = new JLabel("Student ID:");
JLabel firstNameLabel = new JLabel("First Name:");
JLabel lastNameLabel = new JLabel("Last Name:");
int labelX = 40;
int yPosition = 40;
int labelWidth = 100;
int textFieldWidth = 150;
int height = 20;
int verticalGap = 30;
Border border = BorderFactory.createLineBorder(Color.GREEN, 3);
usernameLabel.setBounds(labelX, yPosition, labelWidth, height);
passwordLabel.setBounds(labelX, yPosition + verticalGap, labelWidth, height);
studentIdLabel.setBounds(labelX, yPosition + 2 * verticalGap, labelWidth, height);
firstNameLabel.setBounds(labelX, yPosition + 3 * verticalGap, labelWidth, height);
lastNameLabel.setBounds(labelX, yPosition + 4 * verticalGap, labelWidth, height);
userDetailsPanel.setBorder(border);
userDetailsPanel.add(usernameLabel);
userDetailsPanel.add(passwordLabel);
userDetailsPanel.add(studentIdLabel);
userDetailsPanel.add(firstNameLabel);
userDetailsPanel.add(lastNameLabel);
for (String user : userDetails) {
String[] parts = user.split(" : ");
if (parts.length >= 5) {
JTextField textField = new JTextField(parts[0]);
userDetailsPanel.add(textField);
textField.setBounds(labelX + labelWidth, yPosition, textFieldWidth, height);
textField.setEditable(false);
for (int i = 1; i < parts.length; i++) {
if (i == 1) {
JLabel infoLabel = new JLabel("[Encrypted]");
userDetailsPanel.add(infoLabel);
infoLabel.setBounds(labelX + labelWidth, yPosition + (i * verticalGap), textFieldWidth, height);
} else {
JLabel infoLabel = new JLabel(parts[i]);
userDetailsPanel.add(infoLabel);
infoLabel.setBounds(labelX + labelWidth, yPosition + (i * verticalGap), textFieldWidth, height);
}
}
yPosition += verticalGap * (parts.length - 1); // Update y position for the next user details
} else {
// Handle incomplete or incorrect data for a user
JLabel errorLabel = new JLabel("Invalid user data format");
userDetailsPanel.add(errorLabel);
errorLabel.setBounds(labelX, yPosition, labelWidth, height);
yPosition += verticalGap;
}
}
userDetailsFrame.add(userDetailsPanel);
userDetailsFrame.setVisible(true);
userDetailsFrame.setResizable(false);
userDetailsFrame.setLocationRelativeTo(null);
}
}