userids.txt---------------------------------------------------------------------
ID: 3833503 • Letter: U
Question
userids.txt------------------------------------------------------------------------------------------------
sunset rotGut1 PMs2!! 15wasnT?
sunrise hyM8?? sam3_E sugaR2sweet
evening was!AA 612%qzA
nightfall adam_EVE orangE9
midnight 922SQa#
morning daisy4U
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 **";
}
}
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.