java202302java学习笔记第十九天-升级学生管理系统10和验证代码

时间:2022-11-13 00:53:08

java202302java学习笔记第十九天-升级学生管理系统10和验证代码

package StudentSys;

import java.util.ArrayList;
import java.util.Scanner;

public class App {

public static void main(String[] args) {
ArrayList<User> list=new ArrayList<>();
while (true){
System.out.println("欢迎来到学生管理系统");
System.out.println("请选择操作:1登录 2注册 3忘记密码");
Scanner sc = new Scanner(System.in);
String choose = sc.next();
switch (choose) {
case "1": {
login(list);
break;
}
case "2": {
register(list);
break;
}
case "3": {
forgetPassWord(list);
break;
}
case "4": {
System.out.println("谢谢使用");
System.exit(0);
}
default:
System.out.println("没有这个选项");
}
}

}

private static void register(ArrayList<User> list) {
System.out.println("注册");
Scanner sc = new Scanner(System.in);

while (true) {
System.out.println("请输入用户名");
String username = sc.next();
//用户名必须是3-15位之间
boolean flag1 = checkUsername(username);
if (!flag1) {
System.out.println("当前格式不满足要求,请重新输入");
continue;
}
boolean flag2 = contains(list, username);
if (flag2) {
System.out.println("用户名" + username + "已存在");
continue;
} else {
System.out.println("用户名" + username + "可用");
break;
}
}
while (true) {
System.out.println("请输入密码");
String password = sc.next();
System.out.println("请再次输入密码");
String againPassword = sc.next();
if (!password.equals(againPassword)) {
System.out.println("两次密码不一致,请重新输入");
continue;
} else {
System.out.println("两次密码一致,请继续输入");
break;
}
}
//键盘输入身份证号码
while (true) {
System.out.println("请输入身份证号码");
String personId = sc.next();

boolean flag = checkPersonId(personId);
if (flag) {
System.out.println("身份证信息满足条件");
break;
} else {
System.out.println("身份证信息不满足条件");
continue;
}

}
while (true) {
System.out.println("请输入手机号码");
String phoneNumber = sc.next();
boolean flag=checkNumber(phoneNumber);
if (flag) {
System.out.println("手机信息满足条件");
break;
} else {
System.out.println("手机信息不满足条件");
continue;
}
}
}

private static boolean checkNumber(String phoneNumber) {
if(phoneNumber.length()!=11){
System.out.println("手机号必须是18位数字");
return false;
}
if(phoneNumber.startsWith("0")){
System.out.println("手机号不能是0开头");
return false;
}
for (int i = 0; i < phoneNumber.length()-1; i++) {
char c=phoneNumber.charAt(i);
if(!(c>='0'&&c<='9')){
return false;
}
}
return true;
}

private static void login(ArrayList<User> list){
System.out.println("登录");
}
private static void forgetPassWord(ArrayList<User> list){
System.out.println("忘记密码");
}

private static boolean checkUsername(String username){
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 false;
}
}
//用户满足内容要求
//不能是纯数字
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;
}
private static boolean checkPersonId(String personId){
if(personId.length()!=18){
System.out.println("身份证号必须是18位数字");
return false;
}
if(personId.startsWith("0")){
System.out.println("身份证号不能是0开头");
return false;
}
for (int i = 0; i < personId.length()-1; i++) {
char c=personId.charAt(i);
if(!(c>='0'&&c<='9')){
return false;
}
}
char endChar=personId.charAt(personId.length()-1);
if((endChar>='0'&&endChar<='9')||(endChar=='x')||(endChar=='X')){
return true;
}else{
return false;
}
}
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;
}else{
return false;
}
}
return false;
}
}