在学生管理系统的基础上增加注册、登录、忘记密码等功能
1.
用户注册:
private static void register(ArrayList<User> list){
//键盘录入用户名
Scanner sc=new Scanner(System.in);
String username;
while (true) {
System.out.println("请输入用户名");
username= sc.next();
boolean flag1=checkUsername(username);
if(flag1){
System.out.println("格式不满足条件,继续输入");
continue;
}
//校验是否唯一
boolean flag2=contains(list,username);
if(flag2){
System.out.println("用户名"+username+"已存在,请重新录入");
}else {
System.out.println("用户名可用");
break;
}
}
//键盘录入密码
String password;
while (true) {
System.out.println("请输入密码");
password = sc.next();
System.out.println("请再次输入密码");
String againpassword = sc.next();
if(!password.equals(againpassword)){
System.out.println("密码不一致请再次输入");
}else {
System.out.println("密码输入成功");
break;
}
}
//键盘录入身份证号码
String personID;
while (true) {
System.out.println("请输入身份证号码");
personID = sc.next();
boolean flag=checkPersonID(personID);
if(flag){
System.out.println("身份证号码满足要求");
break;
}else {
System.out.println("身份证号码输入有误,请重新输入");
continue;
}
}
//键盘录入手机号码
String phoneNumber;
while (true) {
System.out.println("请输入手机号码");
phoneNumber = sc.next();
boolean flag=checkPhoneNumber(phoneNumber);
if(flag){
System.out.println("手机号码格式正确");
break;
}else{
System.out.println("手机号码格式有误,重新输入");
}
}
//用户名,密码,身份证号,手机号放到用户对象中
User user = new User(username, password, personID, phoneNumber);
list.add(user);
System.out.println("注册成功");
//遍历集合
printlist(list);
}
//contains方法判断用户是否唯一
private static boolean contains(ArrayList<User> list, String username) {
for (int i = 0; i < list.size(); i++) {
User user=list.get(i);
String rightUsername=user.getUsername();
if(rightUsername.equals(username)){
return true;
}
}
//当循环结束
return false;
}
//密码校验
private static boolean checkUsername(String username){
//长度校验3-15
int len=username.length();
if(len<3||len>15){
return false;
}
//字母加数字组合
for (int i = 0; i < username.length(); i++) {
char c = username.charAt(i);
if(!((c>='a'&&c<='z')||(c>='A'&&c<='Z')||(c>='0'&&c<='9'))){
return true;
}
}
//当代码执行到这里满足两个校验
//不能只是数字校验
int count=0;
for (int i = 0; i < username.length(); i++) {
char c = username.charAt(i);
if(!((c>='a'&&c<='z')||(c>='A'&&c<='Z'))){
count++;
break;
}
}
return count>0;
}
2.
登录
输入用户名首先遍历list看是否已经注册若没有注册直接结束程序。
如果已经注册则输入密码跟验证码判断是否正确。
private static void Login(ArrayList<User> list) {
Scanner sc=new Scanner(System.in);
for (int i = 0; i < 3; i++) {
System.out.println("请输入用户名");
String username = sc.next();
//判断用户名是否存在
boolean flag = contains(list,username);
if(!flag){
System.out.println("用户名"+username+"未注册,请先注册再登陆");
return;
}
System.out.println("请输入密码");
String password = sc.next();
while (true) {
String righCode = getCode();
System.out.println("正确验证码为"+righCode);
System.out.println("请输入验证码");
String code = sc.next();
if(code.equalsIgnoreCase(righCode)){
System.out.println("验证码正确");
break;
}else {
System.out.println("验证码错误");
continue;
}
}
//定义一个方法验证用户名密码是否正确
//封装思想的利用
User useInfo=new User(username,password,null,null);
boolean result = checkPersonID(list,useInfo);
if(result){
System.out.println("登录成功");
studentsystem ss=new studentsystem();
ss.startStudentSystem();
break;
}else {
System.out.println("登录失败");
if(i==2){
System.out.println("当前账号被锁定"+username+"被锁定");
return;
}else{
System.out.println("用户名或密码错误还剩下"+(2-i)+"次");
}
}
}
}
private static boolean checkPersonID(ArrayList<User> list, User useInfo) {
//遍历集合判断用户是否存在存在登录成功 不存在登录失败
for (int i = 0; i < list.size(); i++) {
User user = list.get(i);
if(user.getUsername().equals(useInfo.getUsername())&&user.getPassWord().equals(useInfo.getPassWord())){
return true;
}
}
return false;
}
//验证码生成方法
//验证码生成
private static String getCode(){
ArrayList<Character> list=new ArrayList<>();
for (int i = 0; i < 26; i++) {
list.add((char)('a'+i));
list.add((char)('A'+i));
}
StringBuilder sb=new StringBuilder();
Random r=new Random();
for (int i = 0; i < 4; i++) {
int index=r.nextInt(list.size());
char c=list.get(index);
sb.append(c);
}
//把随机数字添加到末尾
int number=r.nextInt(10);
sb.append(number);
//把子符串变成字符数组,在数组中修改然后在创建一个新的字符串
char[] arr=sb.toString().toCharArray();
int randomIndex=r.nextInt(arr.length-1);
char temp=arr[randomIndex];
arr[randomIndex]=arr[arr.length-1];
arr[arr.length-1]=temp;
return new String(arr);
}
需要源码私我