Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

USERIDS.TXT: Part II. (80 pts.) This program uses sets and maps. Write an applic

ID: 3833864 • Letter: U

Question

USERIDS.TXT:

Part II. (80 pts.) This program uses sets and maps. Write an application that stores a collection of user ids with a password history of the last three passwords in a map. The user requests to change his or her current password, and if accepted, the map is updated. A valid password satisfies the rules: at least six characters each character comes from the set of upper case letters, lower case letters, digits, five punctuation symbols contains at least one lower case letter contains at least one upper case letter contains at least one digit or one punctuation 1. Write class Password with a private String data member password. Include a default constructor, accessor, mutator, and isValid method. The setPassword mutator method throws an Invalid PasswordException exception if it is passed an invalid parameter. Use a TreeSet object for the set of valid characters for use in the isValid method. 2. Write class InvalidPasswordException that contains a toString method that returns message: Invalid Password 3. Write class PasswordManager. The original userld password history data is stored in file "useridstxt'' with one entry per line and should be read into a TreeMap object. The main loop allows the user to process as many data sets as desired. For each data set, enter the user id and current password. Report appropriate error messages if the user id is not in the map or the current password is incorrect. The new password is entered looping until a valid password, not used in up to the last three passwords. The map is updated.

Explanation / Answer

import java.util.TreeSet;


public class Password {
private String password;
TreeSet<Character> validChar = new TreeSet<>();
public Password()
{
validChar.add('?');
validChar.add('!');
validChar.add('_');
validChar.add('%');
validChar.add('#');
for(int i = 0; i < 9; i++)
{
validChar.add((char) ('0' + i));
}
  
for(int i = 0; i < 26; i++)
{
validChar.add((char) ('A' + i));
validChar.add((char) ('a' + i));
}
}

public boolean isValid(String pass)
{
if(pass == null || pass.isEmpty() || pass.length() < 6)
{
return false;
}

boolean isLower = false;
boolean isUpper = false;
boolean isDigit = false;
boolean isPunct = false;
for(int i = 0; i < pass.length(); i++)
{
char c = pass.charAt(i);
if(!validChar.contains(c))
{
return false;
}
if(Character.isAlphabetic(c))
{
if(Character.isLowerCase(c))
{
isLower = true;
}
else
{
isUpper = true;
}
}
else if(Character.isDigit(c))
{
isDigit = true;
}
else if(c == '?' || c == '!' || c == '_' || c == '%' || c == '#')
{
isPunct = true;
}
}
  
return (isLower && isUpper && (isDigit || isPunct));
}
  
public String getPassword() {
return password;
}

public void setPassword(String password) throws InvalidPasswordException {
if(isValid(password))
{
this.password = password;
}
else
{
throw new InvalidPasswordException();
}
}

}


public class InvalidPasswordException extends Exception{

/**
*
*/
private static final long serialVersionUID = 1L;

@Override
public String toString() {
return "** Invalid Password **";
}
  
  

}