-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathChangePassword.java
118 lines (108 loc) · 5.33 KB
/
ChangePassword.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
//Importing libraries that I need
import java.util.Scanner;
public class ChangePassword {
//Here I collect all the methods that I created in this class and form an independent method which I will use in the first menu in the Main class
public static void changePassword() {
displayChangePassword();
User user = checkInput();
changePasswordUser(user);
}
//Method for displaying Change Password Page
public static void displayChangePassword() {
System.out.println("-------------------");
System.out.println("Change Password");
System.out.println("-------------------");
}
//Method that ask user about his/her details and check if information was written in a valid form
public static User checkInput() {
@SuppressWarnings("resource")
Scanner scanner = new Scanner(System.in);
//Checking email
System.out.print("Enter your email: ");
String email = scanner.nextLine();
//Loop that checks if user type valid email. Will loop until user type valid email
//Valid: Aa-Zz letters, numbers 0 to 9, dot, underscore, percent symbol, plus or minus sign + @ + Aa-Zz letters, numbers 0 - 9, dot, minus sign + domain has at least 2 letters Aa-Zz
while (!email.matches("[a-zA-Z0-9._%+-]+@[a-zA-Z0-9.-]+\\.[a-zA-Z]{2,}")) {
System.out.print("Enter a valid email: ");
email = scanner.nextLine();
}
//Checking security number
System.out.print("Enter your security number: ");
int securityNumber;
System.out.print("Enter your security number (6 digits): ");
//Here I check if user type valid security number
//Valid: six digits
//This loop checks if user type input that is not an integer.
while (!scanner.hasNextInt()) {
System.out.print("Invalid input. Enter your security number: ");
scanner.next();
}
securityNumber = scanner.nextInt();
//This loop check if this number is actually have 6 digits
while (securityNumber < 100000 || securityNumber > 999999) {
System.out.print("Enter a valid security number (6 digits): ");
//The same validation loop as before last one
while (!scanner.hasNextInt()) {
System.out.print("Invalid input. Enter your security number: ");
scanner.next();
}
securityNumber = scanner.nextInt();
}
//Consume the newline character left in the buffer
scanner.nextLine();
//Creating new password
System.out.print("Enter your new password: ");
String newPassword = scanner.nextLine();
//Loop that checks if user type valid password. Will loop until user type valid password
//Valid: min length 8 symbols, max length 127 symbols
while (newPassword.length() < 8 || newPassword.length() > 127) {
System.out.print("Enter a valid password: ");
newPassword = scanner.nextLine();
}
//Confirmation of password
System.out.print("Confirm your new password: ");
String confirmNewPassword = scanner.nextLine();
//Loop that check if first password that user input is the same as the password in confirmation
//Loops will work until user type password that is a valid and the confirmation password
while (!newPassword.equals(confirmNewPassword)) {
System.out.print("Passwords don't match. Enter your new password: ");
newPassword = scanner.nextLine();
while (newPassword.length() < 8 || newPassword.length() > 127) {
System.out.print("Enter a valid password: ");
newPassword = scanner.nextLine();
}
System.out.print("Confirm your new password: ");
confirmNewPassword = scanner.nextLine();
}
//Return an object User with email, security number and new password
return new User("", email, newPassword, securityNumber);
}
//Here I pass user object from the User class to use it within method
public static void changePasswordUser(User user) {
//Check if email and security number that user input exists by using method from another class called "FileManagement"
User existingUser = FileManagement.existsEmailAndSecurityNumber(user.getEmail(), user.getSecurityNumber());
//Check If email was already existed
if (existingUser != null) {
//Check if new password is equal to password that already existed in the file
if (existingUser.getPassword().equals(user.getPassword())) {
System.out.println("\nYou already have this password");
Main.pause();
Main.clearScreen();
//Go back to the menu
Main.main(null);
return;
}
//Updating password on a new one
existingUser.setPassword(user.getPassword());
FileManagement.update(existingUser);
System.out.println("\nPassword successfully changed");
//If user typed wrong email or security number programme will say it
} else {
System.out.println("\nInvalid email or security number");
}
Main.pause();
Main.clearScreen();
////Go back to the menu
Main.main(null);
}
}