Java program with wrapper classes included I assume Write th
Java program with wrapper classes included (I assume): Write the following program:
A program that checks a list of passwords entered by the user and verifies the passwords follow the rules below.
#1 Write a class called PasswordInformation, PasswordInformation class should have private fields for:
a. minimum password length - minimum password length is 4
b. maximum password length - maximum password length is 8
c. minimum number of uppercase letters - minimum uppercase letters length is 1
d. minimum number of digits - minimum number of digits is 1
Write a class called PasswordVerifier, PasswordVerifier should have the following methods:
a. Constructor that takes a PasswordInformation object.
b. CheckPassword which takes a String password and returns a String representing the validation results. The String should contain P\'s and F\'s based on which conditions pass or fail.
c. Write a class that uses PasswordInformation and PasswordVerifier. The class should read a text file containig a series of passwords (maximum 3 per line separated by spaces) and should output the passwords containing the password information followed by the passwords and the validation results.
Examples:
besting3 Ballfalls Mello5 W1o
Output example:
Password Information:
Minimum password length: 4
Maximum password length: 8
minimum number of uppercase letters: 1
minimum number of numbers : 1
P = Pass, F = False
besting3 PPFP
Mello5 PPPP
Ballfalls PFPF
M1o FPPP
Solution
public class PasswordInformation { private int minPassLen = 4; private int maxPassLen = 8; private int minUCLet = 1; private int MinDig = 1; public int getMinPassLen() { return minPassLen; } public int getMaxPassLen() { return maxPassLen; } public int getMinUCLet() { return minUCLet; } public int getMinDig() { return MinDig; } } public class PasswordVerifier { private PasswordInformation passwordInformation; public PasswordVerifier(PasswordInformation pi) { this.passwordInformation = pi; } public String checkPassword(String password) { StringBuilder stringBuilder = new StringBuilder(); if (password.length() < passwordInformation.getMinPassLen()) stringBuilder.append(\"F\"); else stringBuilder.append(\"P\"); if (password.length() > passwordInformation.getMaxPassLen()) stringBuilder.append(\"F\"); else stringBuilder.append(\"P\"); if (hasUpperCaseLetter(password)) stringBuilder.append(\"P\"); else stringBuilder.append(\"F\"); if (hasDigit(password)) stringBuilder.append(\"P\"); else stringBuilder.append(\"F\"); return stringBuilder.toString(); } private boolean hasUpperCaseLetter(String password) { int upperCaseLetterCounter = 0; for (int i = 0; i < password.length(); i++) { char ch = password.charAt(i); if (ch >= 65 && ch <= 90) { upperCaseLetterCounter++; } } if (upperCaseLetterCounter >= passwordInformation.getMinUCLet()) return true; return false; } private boolean hasDigit(String password) { int digitCounter = 0; for (int i = 0; i < password.length(); i++) { char ch = password.charAt(i); if (ch >= 48 && ch <= 57) { digitCounter++; } } if (digitCounter >= passwordInformation.getMinDig()) return true; return false; } } import java.io.BufferedReader; import java.io.FileReader; import java.io.IOException; public class MainClass { public static void main(String[] args) { PasswordInformation passwordInformation = new PasswordInformation(); PasswordVerifier passwordVerifier = new PasswordVerifier(passwordInformation); System.out.println(\"Password Information\ \" + \"Minimum password length: 4\ \" + \"Maximum password length: 8\ \" + \"minimum number of uppercase letters: 1\ \" + \"minimum number of numbers : 1\ \" + \"P = Pass, F = False\"); try(BufferedReader br = new BufferedReader(new FileReader(\"passwords.txt\"))) { String line = br.readLine(); while (line != null) { String[] passwords = line.split(\" \"); for (String password: passwords) { System.out.print(password + \" \"); System.out.println(passwordVerifier.checkPassword(password)); } line = br.readLine(); } } catch (IOException e) { e.printStackTrace(); } } }