java 模拟用户登录

使用集合存储多个用户对象。 录入用户和密码,对比用户信息,模拟用户登录。

//初始版本,单纯的对比对象的属性,简单粗暴
/**
*程序入口
*/
public class Test {

	public static void main(String[] args) {
		User p1 = new User("张三","1213");
		User p2 = new User("李四","1113");
		User p3 = new User("王二","2223");
		
		ArrayList<User> list = new ArrayList<>();
		list.add(p1);
		list.add(p2);
		list.add(p3);
		
		UserLogin (list);

	}
	public static void UserLogin(ArrayList<User> list) {
		Scanner input = new Scanner(System.in);
		System.out.println("请输入用户名:");
		String name = input.nextLine();
		int count=0;
		for(User user:list) {
			if(user.getName().equals(name)) {
				count++;
			}
			if(count==0) {
				System.out.println("没有该用户");
				return;
			}else if(count==1) {
				System.out.println("请输入密码:");
				String passWord = input.nextLine();
				if(user.getPassWord().equals(passWord)) {
					System.out.println("登陆成功");
				}else {
					System.out.println("密码错误");
				}
				return;
			}
			
		}
	}
}
//用户类
public class User {
	private String name;
	private String passWord;
	
	public User(String name,String passWord) {
		this.name=name;
		this.passWord=passWord;
	}
	public String getName() {
		return name;
	}
	public void setName(String name) {
		this.name = name;
	}
	public String getPassWord() {
		return passWord;
	}
	public void setPassWord(String passWord) {
		this.passWord = passWord;
	}

}

2.0版本,用户类重写了equals()方法,通过用户对象来判断是否登陆成功

public class Test {

	public static void main(String[] args) {
		User p1 = new User("张三","1213");
		User p2 = new User("李四","1113");
		User p3 = new User("王二","2223");
		
		ArrayList<User> list = new ArrayList<>();
		list.add(p1);
		list.add(p2);
		list.add(p3);
		
		UserLogin (list);

	}
	public static void UserLogin(ArrayList<User> list) {
		Scanner input = new Scanner(System.in);
		User user = new User();
		System.out.println("请输入用户名:");
		user.setName(input.nextLine());
		System.out.println("请输入密码");
		user.setPassWord(input.nextLine());
		int count = 0;
		for(User u:list) {
			if(u.getName().equals(user.getName())) {
				count++;
			}
		}
		if(count==0) {
			System.out.println("没有该用户");
			return;
		}
		for(User u:list) {
			if(u.equals(user)) {
				System.out.println("登陆成功");
				count++;
				return;
			}
		}
		System.out.println("密码错误");
	}
}

3.0版本,为防止注册用户时出现用户名重复,弃用ArrayList,使用HashSet来存储对象

需重写用户类的equals()和hashCode()方法,只要用户名相同,便判为同一对象。但equals()方法修改后,在登录方法中就不能再用equals()来判断两个对象是否相等

//全部代码贴上
public class Test {

	public static void main(String[] args) {
		User p1 = new User("张三","1213");
		User p2 = new User("李四","1113");
		User p3 = new User("王二","2223");
		User p4 = new User("张三","121");
		
		HashSet<User> set = new HashSet<>();
		set.add(p1);
		set.add(p2);
		set.add(p3);
		set.add(p4);
		
		UserLogin (set);

	}
	public static void UserLogin(HashSet<User> list) {
		Scanner input = new Scanner(System.in);
		System.out.println("请输入用户名:");
		String name = input.nextLine();
		int count=0;
		for(User user:list) {
			if(user.getName().equals(name)) {
				count++;
			}
			if(count==0) {
				System.out.println("没有该用户");
				return;
			}else if(count==1) {
				System.out.println("请输入密码:");
				String passWord = input.nextLine();
				if(user.getPassWord().equals(passWord)) {
					System.out.println("登陆成功");
				}else {
					System.out.println("密码错误");
				}
				return;
			}
			
		}
	}
}

public class User {
	private String name;
	private String passWord;
	
	@Override
	public int hashCode() {
		final int prime = 31;
		int result = 1;
		result = prime * result + ((name == null) ? 0 : name.hashCode());
		return result;
	}
	@Override
	public boolean equals(Object obj) {
		if (this == obj)
			return true;
		if (obj == null)
			return false;
		if (getClass() != obj.getClass())
			return false;
		User other = (User) obj;
		if (name == null) {
			if (other.name != null)
				return false;
		} else if (!name.equals(other.name))
			return false;
		return true;
	}
	public User() {}
	public User(String name,String passWord) {
		this.name=name;
		this.passWord=passWord;
	}
	public String getName() {
		return name;
	}
	public void setName(String name) {
		this.name = name;
	}
	public String getPassWord() {
		return passWord;
	}
	public void setPassWord(String passWord) {
		this.passWord = passWord;
	}

}

 

展开阅读全文

没有更多推荐了,返回首页

©️2019 CSDN 皮肤主题: 深蓝海洋 设计师: CSDN官方博客
应支付0元
点击重新获取
扫码支付

支付成功即可阅读