- import java.util.*;
- class User
- {
- private String userName;
- private String password;
- String getUserName()
- {
- return userName;
- }
- String getPassword()
- {
- return password;
- }
-
- User(String userName,String password)
- {
- this.userName = userName;
- this.password = password;
- }
- public int hashCode()//复写hashCode方法 ,用户的登录名不能重复
- {
- return userName.hashCode();
- }
- public boolean equals(Object obj)
- {
- if(!(obj instanceof User))
- {
- return false;
- }
- User u = (User)obj;
- return this.userName.equals(u.userName)&&this.password==u.password;
- }
- }
- class UserManagement extends User
- {
- UserManagement(String userName, String password) {
- super(userName, password);
- }
- //定义用户集合
- static HashSet hs = new HashSet();
- public static void main(String[] args)
- {
- //用户李四注册
- regist("李四","123456");
- //用户李四登录
- logIn("李四","123456");
-
- }
- //注册方法。
- public static boolean regist(String userName,String password)
- {
-
- return hs.add(new User(userName,password));
- }
- //登录方法
- public static boolean logIn(String userName,String password)
- {
- boolean flag = false;
- for(Iterator it = hs.iterator(); it.hasNext();)
- {
- User p = (User) it.next();
- flag = userName.equals(p.getUserName())&&password.equals(p.getPassword());
- if(flag)
- return true;
- }
- return false;
- }
- }
复制代码 |