-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathDatabase.java
More file actions
1722 lines (1502 loc) · 79.7 KB
/
Database.java
File metadata and controls
1722 lines (1502 loc) · 79.7 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
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
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/*--------------------------------------------------------------------------------------*/
/* */
/* Database.java - A text-based and menu-driven database manager that allows users to */
/* view, edit and manage individual custom-made databases. */
/* */
/* Options include: */
/* - View database */
/* - Edit database (add entry, delete entry, change entry) */
/* - Search for entry in specified category */
/* - Sort by category in ascending or descending order */
/* - Change the name/password of database */
/* - Empty database */
/* - Delete database */
/* */
/* Users can create new databases or use existing ones. */
/* All the databases are stored as ".ssf" (super secure files) */
/* Database names and passwords are stored in namespasswords.ssf */
/* */
/*--------------------------------------------------------------------------------------*/
/* */
/* Author: Davis Liu */
/* Date: Monday, May 13th, 2019 */
/* */
/*--------------------------------------------------------------------------------------*/
/* */
/* Input: Database (.ssf) Files, Backup Filename, Search Query, Sort category, Entry */
/* to Delete/Edit, DB Category Names, DB names/passwords (namespasswords.ssf), */
/* Various menu choices, Entry values, New DB name/password */
/* */
/* Output: Various input validation/confirmation prompts, Table of Entries, New DB, */
/* Sorted DB, DB File, DB Backup, Results from search, User interface elements */
/* Various menus and loading screens */
/* */
/*--------------------------------------------------------------------------------------*/
import java.io.*;
import java.util.*;
import java.text.*;
public class Database
{
// Adds a singular entry to the current database
static void addEntry (String[] c1, String[] c2, String[] c3, String[] c4, String[] c5, String[] categoryTitles, int[] count, BufferedReader stdin) throws IOException
{
clearScreen ();
validateString (stdin, count [0], c1, 0, "What would like to add under category #1?" + " (" + categoryTitles [0] + ") "); // Asks the user for input to each category (1-5)
validateString (stdin, count [0], c2, 1, "What would like to add under category #2?" + " (" + categoryTitles [1] + ") "); // The inputs are validated for length/blanks
validateString (stdin, count [0], c3, 2, "What would like to add under category #3?" + " (" + categoryTitles [2] + ") ");
validateString (stdin, count [0], c4, 3, "What would like to add under category #4?" + " (" + categoryTitles [3] + ") ");
validateString (stdin, count [0], c5, 4, "What would like to add under category #5?" + " (" + categoryTitles [4] + ") ");
count [0]++; // Increments the entries count because a new entry has been added
System.out.print ("\nEntry Successfully Added!\n"); // Confirmation message
}
// Displays the menu that allows the user to change either the name or password of the database they currently have open, receives input
static void changeCredentials (String[] c1, String[] c2, String[] c3, String[] c4, String[] c5, String[] database, String[] names, String[] pws, String[] namePass, int[] count, BufferedReader stdin) throws IOException, InterruptedException
{
char response; // Stores the user's choice from the menu
clearScreen ();
printMenu (null, null, 5); // Prints the menu of ID 5 (Change name or Change Password)
do // Loops until the user has selected a valid choice from the menu
{
response = validateChar (stdin).charAt (0); // Receives input from the user and validates if it can be a char
switch (response) // Change name or password based on user's response
{
case '1': // Changes the name of the current database and exits from the current database
changeName (c1, c2, c3, c4, c5, database, names, pws, namePass, count, stdin);
pressEnter ();
saveDatabase (database, c1, c2, c3, c4, c5, count); // Saves the database entries
database [0] = null; // Exits the current database
break;
case '2': // Changes the password of the current database and exits from the current database
changePassword (c1, c2, c3, c4, c5, database, names, pws, namePass, count, stdin);
saveDatabase (database, c1, c2, c3, c4, c5, count); // Saves the database entries
pressEnter ();
database [0] = null; // Exits the current database
break;
case 'e': // Returns back to the database menu
break;
default:
System.out.print ("That is not a valid choice, please try again: "); // Prints invalid input prompt
response = ' ';
break;
}
}
while (response == (' ')); // Loops until the user has selected a valid choice from the menu
}
// Changes a single entry in the database
static void changeEntry (String[] c1, String[] c2, String[] c3, String[] c4, String[] c5, String[] categoryTitles, int[] count, BufferedReader stdin) throws IOException
{
String response = ""; // Stores the user's input
int responseInt; // Stores the user's entry number of choice once the response is validated
System.out.println (); // Print an empty line for aesthetics
viewDatabase (c1, c2, c3, c4, c5, count); // Displays the database for the user to choose an entry number
System.out.print ("\nWhich entry would you like to change? (entry #, enter 0 to go back) ");
response = validateEntryNumber (count, stdin); // Receives and validates the users input so that it can be converted into an int
responseInt = Integer.parseInt (response); // Convert the response into an int so it can be used in a loop
if (responseInt != 0) // Will only change an entry if the user enters a number other than 0 (entering 0 returns ot the menu)
{
validateString (stdin, responseInt, c1, 0, "\nWhat would you like to change the '" + categoryTitles [0] + "' to? "); // Asks the user for input to each category (1-5)
validateString (stdin, responseInt, c2, 1, "What would you like to change the '" + categoryTitles [1] + "' to? "); // The inputs are validated for length
validateString (stdin, responseInt, c3, 2, "What would you like to change the '" + categoryTitles [2] + "' to? ");
validateString (stdin, responseInt, c4, 3, "What would you like to change the '" + categoryTitles [3] + "' to? ");
validateString (stdin, responseInt, c5, 4, "What would you like to change the '" + categoryTitles [4] + "' to? ");
System.out.print ("\nEntry Successfully Changed!\n"); // Prints confirmatory message
pressEnter ();
}
}
// Changes the name of the current database
static void changeName (String[] c1, String[] c2, String[] c3, String[] c4, String[] c5, String[] database, String[] names, String[] pws, String[] namePass, int[] count, BufferedReader stdin) throws IOException
{
int counter = 0; // Stores the amount of existing databases
String response = "", line; // Response stores the user's reponse and line stores the line that is currently being read from the text file
System.out.print ("\nWhat would you like to rename the database to? "); // Asks the user what they want the new database's name to be
do // Loops until the user enters something other than a blank space
{
response = stdin.readLine (); // Recieves the user's response
if ((response.trim ()).equals ("")) // If the user types in an empty space, they are prompted to enter something
System.out.print ("You cannot have an empty name! Please try again: ");
}
while (response == null || (response.trim ()).equals ("")); // Loops until the user enters something other than a an empty space
BufferedWriter writer = new BufferedWriter (new FileWriter (response + ".ssf")); // Declaration of a file writing object that writes to a file with the desired name
for (int i = 0 ; i < count [0] ; i++) // Cycles through all the existing entries in the database so they can be written to the new file
{
writeLine (writer, c1 [i]); // Writes category 1 of the entry to a line
writeLine (writer, c2 [i]); // Writes category 2 of the entry to a line
writeLine (writer, c3 [i]); // Writes category 3 of the entry to a line
writeLine (writer, c4 [i]); // Writes category 4 of the entry to a line
writeLine (writer, c5 [i]); // Writes category 5 of the entry to a line
}
writer.close (); // Closes the writer object to save the changes
(new File (database [0])).delete (); // Deletes the database file with the old name
counter = readNPFile (names, pws); // Reads through all the existing databases and puts their names and passwords into arrays
for (int i = 0 ; i < counter ; i++) // Cycles through all the database names to look for the old name of the database so it can be changed in the namespasswords file
{
if (names [i].equals (namePass [0])) // When the old name is found, it is changed to the new name
{
names [i] = response;
}
}
writeNPFile (names, pws, counter + 1); // Saves all the names and passwords back to the namespasswords file
clearScreen ();
System.out.println ("\nDatabase Name Change Successfully Saved! A Restart is Required."); // Confirmatory statement that tells user the current database will be closed
}
// Changes the password of the current database
static void changePassword (String[] c1, String[] c2, String[] c3, String[] c4, String[] c5, String[] database, String[] names, String[] pws, String[] namePass, int[] count, BufferedReader stdin) throws IOException
{
int counter = 0; // Stores the amount of existing entries
String response = "", line; // Response stores the user's input and line stores the line that is currently being read from the text file
System.out.print ("\nPlease enter your current password: "); // Asks the user to enter their current password
do // Loops until the user enters in the correct password
{
response = stdin.readLine (); // Receives the input from the user
if (!(response.trim ()).equals (namePass [1])) // If the password does not match, a message is printed
{
System.out.print ("That password does not match! Please try again: ");
}
}
while (response == null || !(response.trim ()).equals (namePass [1])); // Loops until the user enters in the correct password
System.out.print ("\nWhat would you like to set as the new password? "); // Asjs the user what they would like to set the new password to
do // Loops until the user enters a password that is at least 5 characters long
{
response = stdin.readLine (); // Receives input from the user
if ((response.trim ()).equals ("")) // If the password is empty, a message is printed
{
System.out.print ("You cannot have an empty password! Please try again: ");
}
else if (response.length () < 5) // If the password is between 1-5 chaacters long, a message is printed
{
System.out.print ("You must choose a password that is at least 5 characters long, please try again: ");
}
}
while (response == null || (response.trim ()).equals ("") || (response.trim ()).length () < 5); // Loops until the user enters a password that is at least 5 characters long
counter = readNPFile (names, pws); // Reads through all the existing databases and puts their names and passwords into arrays
for (int i = 0 ; i < counter ; i++) // Cycles through all the entries to look for the old password of the database so it can be changed in the namespasswords file
{
if (names [i].equals (namePass [0]) && pws [i].equals (namePass [1])) // If the old name is found, it is changed to the current name
{
pws [i] = response.trim ();
}
}
writeNPFile (names, pws, counter + 1); // Saves all the names and passwords back to the namespasswords file
clearScreen ();
System.out.println ("\nPassword Change Successfully Saved! A Restart is Required."); // Confirmatory statement that tells user the current database will be closed
}
static void checkEmpty (int[] count, boolean[] arrayEmpty) // Returns true or false based on if the current database is empty or not
{
if (count [0] == 0 || count [0] == 1)
{
arrayEmpty [0] = true; // If the count value is 0 or 1 which means the database is empty, true is returned
}
else
{
arrayEmpty [0] = false; // If the database is not empty, returns false
}
}
static void clearScreen () //"Clears" the screen by printing enough lines to push text off the screen
{
for (int i = 0 ; i < 40 ; i++) // Prints 40 lines to pseudo-clear the screen
{
System.out.println ();
}
}
//Creates a backup of the open collection
static void createBackup (String[] c1, String[] c2, String[] c3, String[] c4, String[] c5, String[] database, String[] categoryTitles, String[] names, String[] pws, String[] namePass, int[] count, BufferedReader stdin) throws IOException, InterruptedException
{
String response; // Stores the user's desired name of database backup
int counter, backupNum = 1; //Counts how many existing databases there are, and backupNum counts the number of backup files
clearScreen ();
counter = readNPFile (names, pws); // Reads through all the existing databases and puts the names and passwords into arrays
while ((new File ("Backup " + backupNum + " of " + namePass [0] + ".ssf")).exists ()) // Checks how many backups exist and increments the number if the previuos # exists
{
backupNum++;
}
names [counter] = ("Backup " + backupNum + " of " + namePass [0]); // Adds the new database to the array of database names
pws [counter] = namePass [1]; // Adds the new password to the array of database passwords
counter++; // Increments the counter because a new database has been added
writeNPFile (names, pws, counter + 1); // Writes the arrays to the namespasswords.ssf file
// Declaration of the file writing object that writes the new backup file with original database name and backup number attached
BufferedWriter writer = new BufferedWriter (new FileWriter ("Backup " + backupNum + " of " + namePass [0] + ".ssf"));
for (int i = 0 ; i < count [0] ; i++) // Cycles through all the entries
{
// Writes all the entries into the new backup file
writeLine (writer, c1 [i]); // Writes category 1 of the entry to a line
writeLine (writer, c2 [i]); // Writes category 2 of the entry to a line
writeLine (writer, c3 [i]); // Writes category 3 of the entry to a line
writeLine (writer, c4 [i]); // Writes category 4 of the entry to a line
writeLine (writer, c5 [i]); // Writes category 5 of the entry to a line
}
writer.close (); // Closes the write object so that the changes are saved
System.out.print ("\nCopying Contents"); // Prints a loading screen text
for (int i = 0 ; i < 10 ; i++) // Prints the loading screen text dots
{
Thread.sleep (150);
System.out.print (".");
}
clearScreen ();
// Prints confirmatory message
System.out.println ("\nBackup Successfully Saved as: " + "\"" + "Backup " + backupNum + " of " + namePass [0] + ".ssf\"");
System.out.println ("The backup will have the same password as the current database.");
}
//Creates a new database and allows the user to set a password
static void createDatabase (String[] categoryNames, String[] names, String[] pws, BufferedReader stdin) throws IOException
{
String name = "", pass = "", line; //Stores the name, password, and current line when reading a program
boolean repeat; //Boolean that checks if the entered name already exists
int counter; //Counts how many existing databases there are
clearScreen ();
counter = readNPFile (names, pws); // Reads through all the existing databases and puts the names and passwords into arrays
System.out.print ("What would you like to name the new database? "); //Receives and validates the user's choice of database name
do // Loops until a unique name is entered
{
repeat = false; // Sets the repeat boolean to the default of false
name = stdin.readLine (); // Recieves the name input from the user
for (int i = 0 ; i < counter ; i++) // Loops through all the existing database names to check for repetition
{
if ((name.toLowerCase ()).equals (names [i].toLowerCase ())) // Compares the lowercase version of the entered name to the existing names
{
repeat = true; // If the names are the same, repeat becomes true which causes the loop to repeat
System.out.print ("A database with that name already exists, please enter a unique name: "); // Tells user to enter input again
}
}
if ((name.trim ()).equals ("")) // If the name entered is empty, it causes the loop to repeat
{
System.out.print ("You cannot have an empty name! Please enter a name: "); // Prints message telling the user to enter a new name
}
}
while (repeat || (name.trim ()).equals ("")); // Loops until a unique name is entered
System.out.print ("What would you like to set as the password? (Min. 5 characters) "); //Receives and validates the user's password choice
while (((pass = stdin.readLine ()).trim ()).length () < 5) // Loops until a password of at least 5 characters is entered
{
if (pass.length () < 5) // If the password is less than 5 characters, user will be prompted to enter a new password
{
System.out.print ("That password is too short, please enter a new password: ");
}
}
names [counter] = name; // Adds the new database to the array of database names
pws [counter] = pass; // Adds the new password to the array of database passwords
counter++; // Increments the counter because a new database has been added
writeNPFile (names, pws, counter + 1); // Writes the arrays to the namespasswords.ssf file
BufferedWriter categoryWriter = new BufferedWriter (new FileWriter (name + ".ssf")); // Writer object that writes the category names to the new file
clearScreen ();
System.out.println ("NOTE: Max 21 characters for each category name, if a blank is entered, the category is set to N/A."); //Receives and validates the 5 category names
for (int i = 0 ; i < 5 ; i++) // Loops 5 times for the 5 categories
{
do // Loops until an entry that is 25 or less characters is entered
{
System.out.print ("Please enter the name of category #" + (i + 1) + ": "); // Asks the user to input 1 of the 5 categories
categoryNames [i] = stdin.readLine (); // Receives input from the user
if ((categoryNames [i].trim ()).length () > 21) // If the entered category name is too long, user is prompted
{
System.out.print ("That name is too long! ");
}
else if ((categoryNames [i].trim ()).length () == 0 || (categoryNames [i].trim ()).equals ("")) // If the user enters a blank, the category name is set to N/A
{
System.out.println ("This category has been set to N/A.");
categoryNames [i] = "N/A";
}
}
while (categoryNames [i].length () > 25); // Loops until an entry that is 25 or less characters is entered
}
for (int i = 0 ; i < 5 ; i++) //Writes the category names to the file
{
writeLine (categoryWriter, categoryNames [i]); // Wites each category name to a new line
}
categoryWriter.close (); // Closes the file writer object to save the changes
clearScreen ();
System.out.print ("Database with the name \"" + name + "\" and password \""); // Confirms name and password of the new database
// Prints "hidden" password (example: password = ****word)
for (int i = 0 ; i < pass.length () - 4 ; i++)
System.out.print ("*");
for (int i = 4 ; i > 0 ; i--)
System.out.print (pass.charAt (pass.length () - i));
System.out.print ("\" successfully created!\n"); // Prints confirmatory message
pressEnter ();
clearScreen ();
}
static void credentialsResponse (boolean credentials) throws InterruptedException //Prints a message depending on if the database name/password entered are valid
{
if (!credentials) // If the credentials are wrong, prints a message prompting user to try again
{
System.out.println ("\nIncorrect credentials, please try again!");
Thread.sleep (1000);
clearScreen ();
}
else // If the credentials are correct, prints the login loading screen text
{
System.out.print ("\nNow logging on");
Thread.sleep (750);
for (int i = 0 ; i < 10 ; i++)
{
Thread.sleep (400);
System.out.print (".");
}
}
}
static String databaseMenuSelection (BufferedReader stdin) throws IOException // Receives the input from the user for the main menu (the databasse menu)
{
String response = ""; // Stores the the user's choice form the database menu
System.out.print ("\nPlease select an option: "); // Asks the user to select an option from the database menu
do // Loops until a valid choice from the menu is entered
{
response = stdin.readLine ();
if (!response.equals ("1") && !response.equals ("2") && !response.equals ("3") && !response.equals ("4") && !response.equals ("5") && !response.equals ("6") && !response.equals ("7") && !response.equals ("8") && !response.equals ("e") && !response.equals ("E"))
{
System.out.print ("Incorrect input, please try again: "); // Tells the user if they entered an incorrect choice
}
}
while (!response.equals ("1") && !response.equals ("2") && !response.equals ("3") && !response.equals ("4") && !response.equals ("5") && !response.equals ("6") && !response.equals ("7") && !response.equals ("8") && !response.equals ("e") && !response.equals ("E"));
// Loops until a valid choice from the menu is entered
return response; // Returns a validated response
}
// Deletes the current database
static boolean deleteDatabase (String[] database, String[] names, String[] pws, String[] namePass, BufferedReader stdin) throws IOException, InterruptedException
{
char response; // Stores the user's choice
int counter, deleteLocation = 0; // Counter tracks how many total databases exist, deleteLocation is the location of the deleted file in the names and pws arrays
System.out.print ("\nAre you sure about this? (Y/N) "); // Ask the user if they are sure about their choice
do // Loops until y/n is entered the first time
{
response = validateChar (stdin).charAt (0); // Recieves and validates the user's choice from the menu to be a char
response = setLowercase (response); // Sets the char value of the reponse to be lowercase
switch (response)
{
case 'y':
System.out.print ("\nAre you really super duper sure about this? (Y/N) "); // Ask the user again if they are sure about their choice
do // Loops until y/n is entered the second time
{
response = validateChar (stdin).charAt (0); // Receives and validates the user's choice from the menu to be a char
response = setLowercase (response); // Sets the char value of the reponse to be lowercase
switch (response)
{
case 'y':
System.out.println (); // Prints countdown text of deleting the file
for (int i = 5 ; i > 0 ; i--)
{
System.out.print (i);
Thread.sleep (250);
System.out.print (".");
Thread.sleep (250);
}
System.out.print (" Poof\n"); // Prints loading screen text of deleting the file
counter = readNPFile (names, pws); // Reads the namespasswords.ssf to 2 arrays
for (int i = 0 ; i < counter ; i++) // Cycles through all the database to look for the database that will be deleted
{
if (names [i].equals (namePass [0]) && pws [i].equals (namePass [1])) // Sets the name of the database and its password to null
{
names [i] = null;
pws [i] = null;
deleteLocation = i;
}
}
for (int i = deleteLocation ; i < counter - deleteLocation ; i++) // Pushes back any existing database names and passwords to fill the null created
{
swapTwoValues (names, i); // Pushes the null useername entry 1 element up
swapTwoValues (pws, i); // Pushes the null password entry 1 element up
}
writeNPFile (names, pws, counter); // Writes the new changes to namepasswords.ssf
new File (database [0]).delete (); // Deletes the database file
return true; // Informs the main method that the file was successfully deleted
case 'n':
System.out.println ("\nWhew... that was a close one!"); // Prints a confirmatory message
pressEnter ();
return false; // Informs the main method that the file was not deleted
default:
System.out.print ("That is not a valid choice, please try again: "); // Tells the user that their choice is not valid
break;
}
}
while (response != 'y' && response != 'n'); // Loops until y/n is entered the second time
case 'n':
System.out.println ("\nWhew... that was a close one!"); // Prints a confirmatory message
pressEnter ();
return false; // Informs the main method that the file was not deleted
default:
System.out.print ("That is not a valid choice, please try again: "); // Tells the user that their choice is not valid
break;
}
}
while (response != 'y' && response != 'n'); // Loops until y/n is entered the first time
return false;
}
// Deletes an entry in the database
static void deleteEntry (String[] c1, String[] c2, String[] c3, String[] c4, String[] c5, String[] categoryTitles, int[] count, BufferedReader stdin) throws IOException
{
String response; // String that holds the entry that the user wants to delete
System.out.println ();
viewDatabase (c1, c2, c3, c4, c5, count); // Displays the database for the user to choose an entry to delete
System.out.print ("\nWhich entry would you like to delete (entry #, enter 0 to go back)? "); // Asks the user which entry they'd like to delete
response = validateEntryNumber (count, stdin); // Validates the response to be able to be read as an integer and be an existing entry number
if (Integer.parseInt (response) != 0) // Only deletes a file if a number other than 0 is entered
{
for (int i = Integer.parseInt (response) ; i < count [0] ; i++) // Moves the entry to be deleted to be the highest element number in the array
{
c1 [i] = c1 [i + 1];
c2 [i] = c2 [i + 1];
c3 [i] = c3 [i + 1];
c4 [i] = c4 [i + 1];
c5 [i] = c5 [i + 1];
}
c1 [count [0]] = null; // Sets the entry to be deleted to null
c2 [count [0]] = null;
c3 [count [0]] = null;
c4 [count [0]] = null;
c5 [count [0]] = null;
count [0]--; // Decreases the entry count because an entry was just deleted
System.out.print ("\nEntry Successfully Deleted!\n"); // Confirmatory message
pressEnter ();
}
}
static boolean doubleTest (String testString) // Validates if a given string can be converted into a double
{
try
{
Double.parseDouble (testString); // Tries to convert the String into an double
}
catch (Exception e) // If the string cannot be converted, false is returned
{
return false;
}
return true; // If the string can be converted, true is returned
}
// Displays the menu that allows the user to choose to add, delete, or change an entry in the current database, accepts the user's choice
static void editDatabase (String[] c1, String[] c2, String[] c3, String[] c4, String[] c5, String[] database, String[] categoryTitles, int[] count, BufferedReader stdin, boolean[] emptyArray, int MAX) throws IOException
{
char response; // Stores the user's choice from the menu
clearScreen ();
printMenu (null, null, 2); // Prints menu of ID 2 (add, delete, change)
do // Loops until a valid choice from the menu is chosen
{
response = validateChar (stdin).charAt (0); // Receives input and validates it to be a char
switch (response)
{
case '1': // Adds an new entry to the database
if (count [0] != MAX) // Checks if the database is at max capacity, and only allows entry additions if it isn't
{
addEntry (c1, c2, c3, c4, c5, categoryTitles, count, stdin);
pressEnter ();
}
else // Prompts the user to delete entries if the databae is full
{
System.out.println ("\nThe database is currently full, please delete some entries");
pressEnter ();
}
break;
case '2': // Deletes an entry in the database
checkEmpty (count, emptyArray); // Checks if the database is empty
if (emptyArray [0])
{
System.out.println ("\nThe database is empty!"); // If the dataabse is empty, tells the user it is empty
pressEnter ();
}
else // Only allows the user to delete an entry if the database is not full
{
deleteEntry (c1, c2, c3, c4, c5, categoryTitles, count, stdin);
}
break;
case '3': // Deletes an entry in the database
checkEmpty (count, emptyArray); // Checks if the database is empty
if (emptyArray [0])
{
System.out.println ("\nThe database is empty!"); // If the dataabse is empty, tells the user it is empty
pressEnter ();
}
else // Only allows the user to change an entry if the database is not full
{
changeEntry (c1, c2, c3, c4, c5, categoryTitles, count, stdin);
}
break;
case 'e': // returns the user back to the previous menu
break;
default:
System.out.print ("That is not a valid choice, please try again: "); // Prompts the user to enter a valid input and causes the loop to repeat
response = ' ';
break;
}
}
while (response == ' '); // Loops until a valid choice from the menu is chosen
}
// Clears all entries in the current database
static void emptyDatabase (String[] c1, String[] c2, String[] c3, String[] c4, String[] c5, int[] count, BufferedReader stdin) throws IOException
{
char response; // Stores the user's choice
System.out.print ("\nAre you sure about this? (y/n) "); // Ask the user if they are sure about this choice
do // Validate for y/n response from user
{
response = validateChar (stdin).charAt (0); // Validates that input is a char
response = setLowercase (response); // Converts choice to a lowercase char
switch (response)
{
case 'y': // Empties the database
for (int i = 1 ; i < count [0] ; i++) // Sets all entries to null
{
c1 [i] = null; // Sets entry in category 1 to empty
c2 [i] = null; // Sets entry in category 2 to empty
c3 [i] = null; // Sets entry in category 3 to empty
c4 [i] = null; // Sets entry in category 4 to empty
c5 [i] = null; // Sets entry in category 5 to empty
}
count [0] = 1; // Sets the number of entries back to 0, count is equal to 1 because the category titles count as 1 in the count
System.out.println ("\nDatabase successfully wiped clean!"); // Confirmational message
pressEnter ();
break;
case 'n': // Returns the user back to the previous menu
break;
default:
System.out.print ("That is not a valid choice, please try again: "); // Message prompting user to put in valid input
break;
}
}
while (response != 'y' && response != 'n'); // Keeps looping until y/n is entered
}
//Prints a message for exiting the program then closes the window if closeProgram is set to true
static void exitMessage (boolean closeProgram) throws InterruptedException
{
System.out.print ("\nNow exiting"); // Prints now exiting message
Thread.sleep (500);
for (int i = 0 ; i < 10 ; i++) // Prints 10 dots for the loading screen
{
Thread.sleep (250); // Delays the loading dots
System.out.print ("."); // Prints the loading dots
}
if (closeProgram) // If the boolean passed to the method is equal to true, closes the program
{
System.exit (0); // Closess the window
}
}
static boolean integerTest (String testString) // Tests if an String can be converted to an integer
{
try
{
Integer.parseInt (testString); // Tries to convert the String into an int
}
catch (Exception e) // If the string cannot be converted, false is returned
{
return false;
}
return true; // If the string can be converted, true is returned
}
static void login (String[] namePass, BufferedReader stdin) throws IOException //Prompts the user to enter a database name and password
{
System.out.println ("\nPlease enter your database credentials:\n");
System.out.print ("Database Name (Guest User is \"potato\"): "); // Asks the user to enter a database name
namePass [0] = stdin.readLine (); // Receives the name input from the user
System.out.print ("Password (Guest User is \"pommedeterre\"): "); // Asks the user to enter a database password
namePass [1] = stdin.readLine (); // Receives the name input from the user
}
static char loginMenu (BufferedReader stdin) throws IOException //Prints initial menu and receives input
{
char response; // Stores the user's choice from the menu
printMenu (null, null, 0); // Print menu of ID code 0 (existing database, new database, exit)
do // Loops until the user enters a valid choice from the menu
{
response = validateChar (stdin).charAt (0); // Validates the input to be a char
response = setLowercase (response); // Converts the char to lowercase
if (response != 'a' && response != 'b' && response != 'c' && response != ' ') // If the input is invalid, prints message prompting user to try again
{
System.out.print ("Incorrect input, please try again: ");
}
}
while (response != 'a' && response != 'b' && response != 'c' && response != ' '); // Loops until the user enters a valid choice from the menu
return response; // Returns the validated choice from the menu
}
static void openReadMe () throws IOException // Opens the readme.txt file when the program opens
{
Runtime runtime = Runtime.getRuntime ();
Process process = runtime.exec ("notepad.exe readme.txt");
}
static void openingScreen () throws IOException, InterruptedException // Deals with program opening sequence and giant name printout
{
BufferedReader reader = new BufferedReader (new FileReader ("opening sequence.ssf")); // Creates a reader object to read from the the opening sequence graphics file
String line; //Stores the line that is currently being read in the file
// Prints amessage box that prompts the user to enable fullscreen mode on the program
System.out.println ("\n\n\nO------------------------------------------------------------O");
System.out.println ("| This program is best experienced in fullscreen mode. |\n| Please set the window to fullscreen mode before continuing.|");
System.out.println ("O------------------------------------------------------------O");
pressEnter ();
System.out.println ("\n\n\n\n\n\n\n"); // Prints a bunch of empty lines
Thread.sleep (500); // Dramatic pause
while ((line = reader.readLine ()) != null) // Keeps reading the file until it reaches blank space at the end
{
System.out.println (line); // Prints each line from the text file in a delayed manner
Thread.sleep (150);
}
reader.close (); // Closes the reader
Thread.sleep (1000); // Dramatic pause
clearScreen ();
}
static void pressEnter () throws IOException //Prompts the user to press enter to continue
{
System.out.print ("\nPress ENTER to continue..."); // Prompts the user to press enter
try
{
int read = System.in.read (new byte [2]);
}
catch (IOException e)
{
e.printStackTrace ();
}
}
static void printDatabases (String[] names, String[] pws) throws IOException // Prints out all the current available databases
{
int counter; // Stores the number of existing databases
clearScreen ();
System.out.println ("Existing Databases: \n");
counter = readNPFile (names, pws); // Reads all the existing databases to the arrays and returns the number of existing databases
for (int i = 0 ; i < counter ; i++) // Prints out all the available databases as a nice list when the user needs to enter credentials
{
System.out.println ("~ " + names [i]);
}
}
static void printLongLine (char midChar, char edgeChar) //Prints a long line of char c with char c2 at the ends
{
System.out.print (edgeChar); // Print out the character at the end of the line
for (int i = 0 ; i < 155 ; i++) // Constant value of 155 (the length of the full size window)
{
System.out.print (midChar); // Print out the chaaracters in the middle
}
System.out.println (edgeChar); // Print out the character at the end of the line
}
static void printMenu (String[] database, String[] categoryTitles, int menuID) //Prints all the menus in the program, prints a different menu according to menuID passed
{
switch (menuID) // Displays a different menu depending on which menuID was passed when the method was called
{
case 0: // Opening screen menu --> 0
System.out.println ("Welcome to the Super Cool Database Manager!\n");
System.out.println ("[A] Open an existing database");
System.out.println ("[B] Create a new database (New User)");
System.out.println ("[C] Exit");
System.out.print ("\nPlease select an option: ");
break;
case 1: // Current database options menu --> 1
System.out.println ("Current Database: " + database [0] + "\n");
System.out.println ("[1] View Database");
System.out.println ("[2] Edit Database (Add, Delete, Change)");
System.out.println ("[3] Search for Entry");
System.out.println ("[4] Sort by Category");
System.out.println ("[5] Create Backup");
System.out.println ("[6] Change Database Name/Password");
System.out.println ("[7] Empty Database");
System.out.println ("[8] Delete Database");
System.out.println ("[e] Save and Exit Current Database");
System.out.println ("[E] Save and Exit Program");
break;
case 2: // Edit database menu --> 2
System.out.println ("[1] Add Entry");
System.out.println ("[2] Delete Entry");
System.out.println ("[3] Change Entry");
System.out.println ("[e] Go Back");
System.out.print ("\nPlease select an option: ");
break;
case 3: // Search database menu --> 3
System.out.println ("[1] " + categoryTitles [0]);
System.out.println ("[2] " + categoryTitles [1]);
System.out.println ("[3] " + categoryTitles [2]);
System.out.println ("[4] " + categoryTitles [3]);
System.out.println ("[5] " + categoryTitles [4]);
System.out.println ("[e] Go Back");
System.out.print ("\nUnder which category would you like to search? ");
break;
case 4: // Sort database menu --> 4
System.out.println ("[1] " + categoryTitles [0]);
System.out.println ("[2] " + categoryTitles [1]);
System.out.println ("[3] " + categoryTitles [2]);
System.out.println ("[4] " + categoryTitles [3]);
System.out.println ("[5] " + categoryTitles [4]);
System.out.println ("[e] Go Back");
System.out.print ("\nBy which category would you like to sort? ");
break;
case 5: // Change credentials menu --> 5
System.out.println ("[1] Change Database Name");
System.out.println ("[2] Change Database Password");
System.out.println ("[e] Go Back");
System.out.print ("\nPlease select an option: ");
break;
}
}
static void printPaddedEntry (String entry, boolean smallSize, boolean twoDigit) //Prints an entry and pads it
{
if (smallSize) // Pads the entry numbers much less than the actual entries
if (!twoDigit) // Pads a bit more for 1 digit numbers
{
System.out.print ("| " + entry + " ");
}
else // Pads less for 2 digit number
{
System.out.print ("| " + entry + " ");
}
else // Pads the entries to the same size for printing a nice table
{
int x = 25 - entry.length (); // Calculates how much to pad the entries
System.out.print ("| " + entry);
for (int i = 0 ; i < x ; i++)
{
System.out.print (" ");
}
}
}
static int readDatabaseFile (String[] database, String[] c1, String[] c2, String[] c3, String[] c4, String[] c5) throws IOException //Reads a file into the 5 category arrays
{
BufferedReader reader = new BufferedReader (new FileReader (database [0])); // Creates a file reader object to read the current database file
String line = null;
int count = 0; // Keeps track of the number of entries in the database
while ((line = reader.readLine ()) != null && !line.trim ().equals ("")) // Keeps reading the file until it reaches blank space at the end
{
c1 [count] = line; // Read category 1
line = reader.readLine ();
c2 [count] = line; // Read category 2
line = reader.readLine ();
c3 [count] = line; // Read category 3
line = reader.readLine ();
c4 [count] = line; // Read category 4
line = reader.readLine ();
c5 [count] = line; // Read category 5
count++; // Increment number of entries every 5 lines
}
reader.close (); // Closes the reader
return count; //Returns the number of existing entries in the database
}
static int readNPFile (String[] names, String[] pws) throws IOException // Reads names and passwords from namepasswords.ssf and returns # of databases
{
BufferedReader npReader = new BufferedReader (new FileReader ("namespasswords.ssf"));
String line; // Stores the ;ine currently being read in file
int npCount = 0; // Keeps track of the number of existing databases
while ((line = npReader.readLine ()) != null && !line.trim ().equals ("")) // Keeps reading the file until it reaches blank space at the end
{
names [npCount] = line; // Read database name
line = npReader.readLine ();
pws [npCount] = line; // Read accompanying password
npCount++; // Increment number of databases every 2 lines
}
npReader.close ();
return npCount; // Returns the number of existing databases
}
//Writes the current arrays to the database file
static void saveDatabase (String[] database, String[] c1, String[] c2, String[] c3, String[] c4, String[] c5, int[] count) throws IOException, InterruptedException
{
BufferedWriter writer = new BufferedWriter (new FileWriter (database [0])); // Declares the writer object that will wrtie to the current database file
for (int i = 0 ; i < count [0] ; i++) // Cycles through all the existing entries
{
writeLine (writer, c1 [i]); // Writes the first category
writeLine (writer, c2 [i]); // Writes the second category
writeLine (writer, c3 [i]); // Writes the third category
writeLine (writer, c4 [i]); // Writes the fourth category
writeLine (writer, c5 [i]); // Writes the fifth category
}
writer.close (); // Closes the file writer object to save the changes to the database file
System.out.println ("\nDatabase Successfully Saved!"); // Confirmatory message
Thread.sleep (500);
}
//Searchs a specific category for a matching String
static void searchCategory (BufferedReader stdin, String[] arr, String[] c1, String[] c2, String[] c3, String[] c4, String[] c5, String[] c1Found, String[] c2Found, String[] c3Found, String[] c4Found, String[] c5Found, int[] count) throws IOException
{
String[] response = new String [1]; // Stores the category the user would like to search through
int[] foundCount = new int [1]; // Stores the amount of matching entries found
foundCount [0] = 1; // Initializes the found count to 1
System.out.print ("\nWhat entry value would you like to search for? "); // Ask the user what they would like to search for
response [0] = stdin.readLine (); // Receives the input from the user
for (int i = 1 ; i < count [0] ; i++) // Searches through all the entries to find matches
{
if (arr [i].equalsIgnoreCase (response [0])) // If there is a match, the matching entry is added to the arrays of found entries
{
c1Found [foundCount [0]] = c1 [i]; // Assigns the matching values to arrays with only matching values
c2Found [foundCount [0]] = c2 [i];
c3Found [foundCount [0]] = c3 [i];
c4Found [foundCount [0]] = c4 [i];
c5Found [foundCount [0]] = c5 [i];
foundCount [0]++; // Increment the amount of found meatches by 1
}
}
if (foundCount [0] == 1) // If no matches were found, a message is printed
{
System.out.println ("\nNo results found. ");
}
else // If matches were found, a message is printed and the results are displayed
{
System.out.println ("\nResults Found:\n");
viewDatabase (c1Found, c2Found, c3Found, c4Found, c5Found, foundCount); // Displays the results that were found
}
pressEnter ();
}
//Searches for specified value under a specified category
static void searchDatabase (String[] database, String[] c1, String[] c2, String[] c3, String[] c4, String[] c5, BufferedReader stdin, int[] count, String[] categoryTitles) throws IOException
{
char response; // Stores the user's choice from the menu
// Arrays to store all the found results
String[] c1Found = new String [50], c2Found = new String [50], c3Found = new String [50], c4Found = new String [50], c5Found = new String [50];
c1Found [0] = categoryTitles [0]; // Sets all the first elements in the results arrays to the category titles
c2Found [0] = categoryTitles [1];
c3Found [0] = categoryTitles [2];
c4Found [0] = categoryTitles [3];
c5Found [0] = categoryTitles [4];
clearScreen ();
printMenu (null, categoryTitles, 3); // Prints menu of ID 3 (category1, category2, category3, caetgoery4, category5)
do // Loops until the user enters a valid choice from the menu
{
response = validateChar (stdin).charAt (0); // receives input and validates it to be a char
switch (response)
{
case '1': // Searches under category 1