import java.util.*; /** * Represents one user's account information. * @author Marty Stepp * @version 2010-02-08, CSE 403 Spring 2010 */ public class Account { private String name; private String password; /** * Constructs a new account with the given name and password. * @throws NullException if name or password is null. * @throws IllegalArgumentException if password is <= 6 letters or has * fewer than 3 unique letters. */ public Account(String name, String password) { ensureNotNull(name); this.name = name; setPassword(password); } /** * Returns this account's name. */ public String getName() { return name; } /** * Returns this account's password. */ public String getPassword() { return password; } /** * Sets this account to use the given password. * @throws NullException if password is null. * @throws IllegalArgumentException if password is <= 6 letters or has * fewer than 3 unique letters. */ public void setPassword(String password) { ensureNotNull(password); if (!validPassword(password)) { throw new IllegalArgumentException("bogus password: " + password); } this.password = password; } // returns true if the given password meets the criteria: // length at least 6, and at least 3 unique letters. private boolean validPassword(String password) { Set uniqChars = new HashSet(Arrays.asList(password.split(""))); return password.length() >= 6 && uniqChars.size() - 1 >= 3; } // Throws a NullPointerException if o is null. private void ensureNotNull(Object o) { if (o == null) { throw new NullPointerException(); } } }