package Menu;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Iterator;
import java.util.Scanner;
public class UserDao {
//用户的数据都储存到该集合中
private static ArrayList<User> users =new ArrayList<User>();
//注册
public void reg() {
Scanner scanner =new Scanner(System.in);
String userName =null;
while(true) {
System.out.println("请输入用户名:");
userName =scanner.next();
//用户名首位不能以数字开头, 6位英文字母与数组组成
if(userName.matches("[a-zA-Z][a-zA-Z0-9]{5}")){
break;
}else{
System.out.println("用户名不符合规则");
}
}
String password =null;
while(true) {
System.out.println("请输入密码:");
password = scanner.next();
if(password.matches("\\d{6}")) {
break;
}else {
System.out.println("密码不符合规则");
}
}
//使用新注册的信息生产一个用户对象
User user =new User(userName,password);
users.add(user);
System.out.println("注册功能...");
System.out.println("当前用户有:"+users);
}
//登录
public void login() {
while(true) {
Scanner scanner =new Scanner(System.in);
System.out.println("请输入用户名:");
String userName =scanner.next();
if(users.contains(new User(userName,null))) {
System.out.println("请输入密码:");
Scanner scanner1 = new Scanner(System.in);
String password = scanner1.next();
Iterator it = users.iterator();
while(it.hasNext()) {
User u = (User) it.next();
if(password.equals(u.getPassword())) {
System.out.println("登录成功,欢迎登录图书管理系统!");
break;
}
}
}else {
System.out.println("用户名不存在,请重新输入");
}
}
}
}
xixiyingyi
xixiyingyi
相关分类