-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathAdminFrame.java
501 lines (390 loc) · 13.2 KB
/
AdminFrame.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
489
490
491
492
493
494
495
496
497
498
499
500
501
import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.*;
import java.util.ArrayList;
import java.util.Scanner;
public class AdminFrame extends JFrame
{
private static File usersFile=new File("./Users");
private static final File dataBase=new File("./dataBase");
private static AdminFrame deleteFrame;
private Customer info;
private AdminFrame balance;
private JPanel mainButtons;
private JLabel WelcomeAdmin;
private JButton addAccount;
private JButton deleteAccount;
private JButton printList;
private JButton totalBalance;
private JButton customersDisconnected;
private JButton standardDeviation;
private JButton Exit;
private JPanel deletePan;
private JLabel DeleteAccount;
private JLabel delUser;
private JLabel delPass;
private JTextField delUserField;
private JTextField delPassField;
private JButton Back;
private JButton DeleteAccountB;
private JLabel balanceTotal;
private JLabel getBalanceTotal;
private JPanel totalBalPanel;
private JButton totalBack;
public AdminFrame()
{
setTitle("WAPA");
setSize(400,400);
buildButtonPanel();
add(mainButtons);
setVisible(true);
}
public AdminFrame(int i)
{
setTitle("WAPA");
setSize(350,350);
buildDelete();
add(deletePan);
setVisible(true);
}
public AdminFrame(int i,int j)
{
setTitle("WAPA");
setSize(200,200);
buildTotalBalancePanel();
add(totalBalPanel);
setVisible(true);
}
public void buildDelete()
{
DeleteAccount=new JLabel("Delete Account");
JPanel sub01=new JPanel();
sub01.setLayout(new FlowLayout());
sub01.add(DeleteAccount,CENTER_ALIGNMENT);
delUser=new JLabel("Account Username ");
delUserField=new JTextField(10);
JPanel sub02=new JPanel();
sub02.setLayout(new GridLayout(1,1));
sub02.add(delUser);
sub02.add(delUserField);
delPass=new JLabel("Account Password: ");
delPassField=new JTextField(10);
JPanel sub03=new JPanel();
sub03.setLayout(new GridLayout(1,1));
sub03.add(delPass);
sub03.add(delPassField);
Back=new JButton("Back");
Back.addActionListener(new backListener());
DeleteAccountB=new JButton("Delete Account");
DeleteAccountB.addActionListener(new deleteInsideListener());
JPanel sub04=new JPanel();
sub04.setLayout(new GridLayout(1,1));
sub04.add(Back);
sub04.add(DeleteAccountB);
deletePan=new JPanel();
deletePan.setLayout(new GridLayout(4,1));
deletePan.add(sub01);
deletePan.add(sub02);
deletePan.add(sub03);
deletePan.add(sub04);
}
public void buildTotalBalancePanel()
{
listFilesForFolder(dataBase);
balanceTotal=new JLabel("Total Customers Balance: ");
getBalanceTotal=new JLabel();
JPanel sub01=new JPanel();
sub01.setLayout(new GridLayout(2,1));
sub01.add(balanceTotal);
sub01.add(getBalanceTotal);
totalBack=new JButton("Back");
totalBack.addActionListener(new totalBackListener());
totalBalPanel=new JPanel();
totalBalPanel.setLayout(new GridLayout(2,2));
totalBalPanel.add(sub01);
totalBalPanel.add(totalBack);
}
public void buildButtonPanel()
{
WelcomeAdmin=new JLabel("Welcome Administrator");
JPanel sub01=new JPanel();
sub01.setLayout(new FlowLayout());
sub01.add(WelcomeAdmin,CENTER_ALIGNMENT);
addAccount=new JButton("Add Admin Account");
addAccount.addActionListener(new addAccountListener());
deleteAccount=new JButton("Delete Account");
deleteAccount.addActionListener(new deleteListener());
printList=new JButton("List of Customers with High Consumption");
totalBalance=new JButton("Total Balance Owed by All Customers");
totalBalance.addActionListener(new menuBalanceListener());
customersDisconnected=new JButton("Disconnected Customers");
standardDeviation=new JButton("Standard Deviation");
Exit=new JButton("Exit");
Exit.addActionListener(new exitListener());
mainButtons=new JPanel();
mainButtons.setLayout(new GridLayout(8,1));
mainButtons.add(sub01);
mainButtons.add(addAccount);
mainButtons.add(deleteAccount);
mainButtons.add(printList);
mainButtons.add(totalBalance);
mainButtons.add(customersDisconnected);
mainButtons.add(standardDeviation);
mainButtons.add(Exit);
}
public static void modifyFile(String filePath, String oldString, String newString)
{
File fileToBeModified = new File(filePath);
String oldContent = "";
BufferedReader reader = null;
FileWriter writer = null;
try
{
reader = new BufferedReader(new FileReader(fileToBeModified));
//Reading all the lines of input text file into oldContent
String line = reader.readLine();
while (line != null)
{
oldContent = oldContent + line + System.lineSeparator();
line = reader.readLine();
}
//Replacing oldString with newString in the oldContent
String newContent = oldContent.replaceAll(oldString, newString);
//Rewriting the input text file with newContent
writer = new FileWriter(fileToBeModified);
writer.write(newContent);
}
catch (IOException e)
{
e.printStackTrace();
}
finally
{
try
{
reader.close();
writer.close();
}
catch (IOException e)
{
e.printStackTrace();
}
}
}
private class deleteInsideListener implements ActionListener
{
@Override
public void actionPerformed(ActionEvent e)
{
try
{
Boolean deleteCheck;
Boolean adminCheck;
int objID;
String user = delUserField.getText();
String pass = delPassField.getText();
Scanner userReader = new Scanner(usersFile);
while (userReader.hasNext())
{
if (userReader.next().equals(user) && userReader.next().equals(pass))
{
adminCheck = Boolean.parseBoolean(userReader.next());
objID=Integer.parseInt(userReader.next());
deleteCheck = true;
if (deleteCheck)
{
String FileName = String.valueOf(objID) + ".dat";
File objFile = new File(dataBase, FileName);
if (!objFile.createNewFile())
{
if (objFile.delete())
{
modifyFile(usersFile.getAbsolutePath(),user,"null");
modifyFile(usersFile.getAbsolutePath(),pass,"null");
System.out.println("File ID " + FileName + " Was Successfully Deleted");
JOptionPane.showMessageDialog(null,"File ID " + FileName + " Was Successfully Deleted");
}
else
{
System.out.println("File Does Exist: Wasn't Able to be Deleted");
}
}
else
{
JOptionPane.showMessageDialog(null,"File Doesn't Exist");
System.out.println("File Doesn't Exist");
}
}
}
else
{
System.out.println("Scanning Files");
}
}
}
catch (Exception ex)
{
System.out.println(ex + "Caught");
}
}
}
private class deleteListener implements ActionListener
{
@Override
public void actionPerformed(ActionEvent e)
{
try
{
setVisible(false);
deleteFrame=new AdminFrame(1);
}
catch (Exception ex)
{
System.out.println(ex + "Caught");
}
}
}
public static Object ReadObjectFromFile(String filepath)
{
try
{
FileInputStream fileIn = new FileInputStream(filepath);
ObjectInputStream objectIn = new ObjectInputStream(fileIn);
Object obj = objectIn.readObject();
System.out.println("The Object has been read from the file");
objectIn.close();
return obj;
}
catch (Exception ex)
{
ex.printStackTrace();
return null;
}
}
public static Object dirList(File dir,int objID)
{
Object one=new Object();
String[] fileList = dir.list();
String dirPath = dir.getAbsolutePath();
for (int i = 0; i < fileList.length; i++)
{
File file = new File(dirPath + "/" + fileList[i]);
if (file.isFile())
{
String userID=String.valueOf(objID);
if (LoginFrame.recountSt(file.getName(),userID)==1)
{
one=ReadObjectFromFile(file.getAbsolutePath());
}
System.out.println(file.getName());
} else
{
dirList(file,objID);
}
}
return one;
}
public void listFilesForFolder(final File folder) {
for (final File fileEntry : folder.listFiles()) {
if (fileEntry.isDirectory()) {
listFilesForFolder(fileEntry);
} else {
System.out.println(fileEntry.getName());
}
}
}
public double getBalanceTotal(ArrayList customerArray)
{
int objInt=0;
double totalBalance=0;
for (int i = 0; i <customerArray.size() ; i++)
{
info=(Customer) dirList(dataBase,objInt);
totalBalance=totalBalance+info.getBalance();
objInt++;
}
return totalBalance;
}
private class backListener implements ActionListener
{
@Override
public void actionPerformed(ActionEvent e)
{
try
{
deleteFrame.setVisible(false);
AdminFrame main=new AdminFrame();
}
catch (Exception ex)
{
System.out.println(ex + "Caught");
}
}
}
private class totalBackListener implements ActionListener
{
@Override
public void actionPerformed(ActionEvent e)
{
try
{
setVisible(false);
AdminFrame main=new AdminFrame();
}
catch (Exception ex)
{
System.out.println(ex + "Caught");
}
}
}
private class menuBalanceListener implements ActionListener
{
@Override
public void actionPerformed(ActionEvent e)
{
try
{
setVisible(false);
balance =new AdminFrame(1,1);
}
catch (Exception ex)
{
System.out.println(ex + "Caught");
}
}
}
private class addAccountListener implements ActionListener
{
@Override
public void actionPerformed(ActionEvent e)
{
try
{
setVisible(false);
AccountCreator first=new AccountCreator(true);
}
catch (Exception ex)
{
System.out.println(ex + "Caught");
}
}
}
private class exitListener implements ActionListener
{
@Override
public void actionPerformed(ActionEvent e)
{
try
{
setVisible(false);
LoginFrame main=new LoginFrame();
}
catch (Exception ex)
{
System.out.println(ex + "Caught");
}
}
}
}