forked from nusCS2113-AY1819S1/addressbook-level3
-
Notifications
You must be signed in to change notification settings - Fork 5
/
passwordUpdater.java
101 lines (91 loc) · 4.79 KB
/
passwordUpdater.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
import java.io.BufferedReader;
import java.io.File;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.Scanner;
public class passwordUpdater {
public static void main(String args[]) throws IOException {
Scanner console = new Scanner(System.in);
boolean hasUpdatedPassword = false;
int wrongPasswordCounter=5;
while(!hasUpdatedPassword) {
System.out.print("Enter Current Password : ");
String currentPassword = console.nextLine();
int enteredCurrentPassword = currentPassword.hashCode();
File originalFile = new File("passwordStorage.txt");
BufferedReader br = new BufferedReader(new FileReader(originalFile));
File tempFile = new File("tempfile.txt");
PrintWriter pw = new PrintWriter(new FileWriter(tempFile));
String line = null;
int numberOfPasswords = 2;
while (numberOfPasswords > 0) {
line = br.readLine();
String storedCurrPassword = line.substring(line.lastIndexOf(" ") + 1, line.length());
if (storedCurrPassword.equals(Integer.toString(enteredCurrentPassword))) {
System.out.print("Enter New Alphanumeric Password: ");
boolean isValidPassword = false;
String newPassword = null;
while (!isValidPassword) {
String newEnteredPassword = console.nextLine();
int enteredNewPassword= newEnteredPassword.hashCode();
if(storedCurrPassword.equals(Integer.toString(enteredNewPassword))){
System.out.println("Your new password cannot be the same as your old password. Please try again.");
System.out.print("Enter New Alphanumeric Password: ");
}
else if (newEnteredPassword.matches(".*\\d+.*") && !newEnteredPassword.matches(".*[a-zA-Z]+.*")) {
System.out.println("Your new password must contain at least one alphabet. Please try again.");
System.out.print("Enter New Alphanumeric Password: ");
} else if (!newEnteredPassword.matches(".*\\d+.*") && newEnteredPassword.matches(".*[a-zA-Z]+.*")) {
System.out.println("Your new password must contain at least one number. Please try again.");
System.out.print("Enter New Alphanumeric Password: ");
} else if (newEnteredPassword.matches(".*\\d+.*") && newEnteredPassword.matches(".*[a-zA-Z]+.*")) {
newPassword = newEnteredPassword;
isValidPassword = true;
} else {
System.out.println("Your new password can only be alphanumeric");
System.out.print("Enter New Alphanumeric Password: ");
}
}
int storedNewPassword = newPassword.hashCode();
if (storedCurrPassword != null || !storedCurrPassword.trim().isEmpty()) {
line = line.substring(0, line.lastIndexOf(" ") + 1) + Integer.toString(storedNewPassword);
System.out.println("You have updated your password to : " + newPassword);
hasUpdatedPassword = true;
}
}
pw.println(line);
pw.flush();
numberOfPasswords--;
}
pw.close();
br.close();
// Delete the original file
if (!originalFile.delete()) {
System.out.println("Could not delete file");
return;
}
// Rename the new file to the filename the original file had.
if (!tempFile.renameTo(originalFile)) {
System.out.println("Could not rename file");
}
if (!hasUpdatedPassword) {
System.out.println("Wrong Password. Please try again.");
if(wrongPasswordCounter==1){
System.out.println("You have " + wrongPasswordCounter + " attempt left.");
System.out.println("System will shut down if password is incorrect");
wrongPasswordCounter--;
}
else if (wrongPasswordCounter==0){
System.out.println("System shutting down.");
return;
}
else {
System.out.println("You have " + wrongPasswordCounter + " attempts left.");
wrongPasswordCounter--;
}
}
}
}
}