-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtask2.java
49 lines (43 loc) · 1.43 KB
/
task2.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
import java.lang.Character;
// Make the password length a global variable
public class PasswordValidator {
private static final int MIN_PASSWORD_LENGTH = 8;
public static boolean isValidPassword(String password) {
if (password.length() < MIN_PASSWORD_LENGTH) {
return false;
}
boolean hasUpperCase = hasUpperCaseLetter(password);
boolean hasLowerCase = hasLowerCaseLetter(password);
boolean hasNumber = hasNumber(password);
return hasUpperCase && hasLowerCase && hasNumber;
}
private static boolean hasUpperCaseLetter(String password) {
for (char c : password.toCharArray()) {
if (Character.isUpperCase(c)) {
return true;
}
}
return false;
}
private static boolean hasLowerCaseLetter(String password) {
for (char c : password.toCharArray()) {
if (Character.isLowerCase(c)) {
return true;
}
}
return false;
}
private static boolean hasNumber(String password) {
for (char c : password.toCharArray()) {
if (Character.isDigit(c)) {
return true;
}
}
return false;
}
public static void main(String[] args) {
String password = "veryStrongP@ss";
boolean valid = isValidPassword(password);
System.out.println("Password is valid: " + valid);
}
}