需求分析
先创建一个UserDeom类
public class UserDeom {
String userName;
String userPassword;
String userId;
String userTelephone;
public UserDeom() {
}
public UserDeom(String userName, String userPassword, String userId, String userTelephone) {
this.userName = userName;
this.userPassword = userPassword;
this.userId = userId;
this.userTelephone = userTelephone;
}
/**
* 获取
* @return userName
*/
public String getUserName() {
return userName;
}
/**
* 设置
* @param userName
*/
public void setUserName(String userName) {
this.userName = userName;
}
/**
* 获取
* @return userPassword
*/
public String getUserPassword() {
return userPassword;
}
/**
* 设置
* @param userPassword
*/
public void setUserPassword(String userPassword) {
this.userPassword = userPassword;
}
/**
* 获取
* @return userId
*/
public String getUserId() {
return userId;
}
/**
* 设置
* @param userId
*/
public void setUserId(String userId) {
this.userId = userId;
}
/**
* 获取
* @return userTelephone
*/
public String getUserTelephone() {
return userTelephone;
}
/**
* 设置
* @param userTelephone
*/
public void setUserTelephone(String userTelephone) {
this.userTelephone = userTelephone;
}
public String toString() {
return "UserDeom{userName = " + userName + ", userPassword = " + userPassword + ", userId = " + userId + ", userTelephone = " + userTelephone + "}";
}
在创建一个Sutdnet类
public class Student {
private String name;
private int age;
private String family;
private String id;
public Student() {
}
public Student(String name, int age, String family, String id) {
this.name = name;
this.age = age;
this.family = family;
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
public String getFamily() {
return family;
}
public void setFamily(String family) {
this.family = family;
}
public String getId() {
return id;
}
public void setId(String id) {
this.id = id;
}
主方法
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
UserDeom ud = new UserDeom("zy2","123456","123456789123456789","12345678911");
ArrayList <UserDeom> user= new ArrayList<>();
user.add(ud);
int i=3;
System.out.println("欢迎来到学生管理系统");
// System.out.println("请选择操作1登录 2注册 3忘记密码");
look: while(true){
System.out.println("请选择操作1登录 2注册 3忘记密码");
String a = sc.nextLine();
switch (a){
case "1" -> {
while (i>0){
System.out.println("请输入用户名");
String username = sc.next();
System.out.println("请输入密码");
String password = sc.next();
boolean b = UserDengLu(user,username,password);
if(b){
System.out.println("登录成功");
break look;
}else{
System.out.println("还有" + --i +"次机会");
}
}
}
case "2" -> {
userZhuChe(user);
for (int y = 0 ; y<user.size();y++){
System.out.println(user.get(y).getUserName() + " " + user.get(y).getUserPassword());
}
}
case "3" -> {
System.out.println("请输入用户名");
String username = sc.next();
userWamgJi(user,username);
for (int y = 0 ; y<user.size();y++){
System.out.println(user.get(y).getUserName() + " " + user.get(y).getUserPassword());
}
}
default -> System.out.println("没有这个选项");
}
}
Student s = new Student("郭辉",18,"郭辉是一坨史","1");
Student s1 = new Student("张莹",1,"是","2");
ArrayList <Student> list = new ArrayList <>();
list.add(s);
list.add(s1);
xingTong(list);
// xiuGai(list,"2");
// System.out.println(list.get(1).getId() + " " + list.get(1).getName() + " " + list.get(1).getAge() + " " + list.get(1).getFamily()) ;
// 删除的demo
// shanChu(list,"1");
// System.out.println(list.size());
//
}
// 学生管理系统
public static void xingTong(ArrayList<Student> list){
Scanner sc = new Scanner(System.in);
aaa: while (true){
System.out.println("-------------欢迎来到黑马学生管理系统--------------");
System.out.println("1:添加学生");
System.out.println("2:删除学生");
System.out.println("3:修改学生");
System.out.println("4:查询学生");
System.out.println("5:退出");
System.out.println("请输入您的选择:");
int choice = sc.nextInt();
switch (choice){
case 1 -> {
System.out.println("请输入你的id");
String studentId = sc.nextLine();
System.out.println("请输入你的姓名");
String name = sc.nextLine();
System.out.println("请输入你的年龄");
int age = sc.nextInt();
System.out.println("请输入你的家庭地址");
String familyA = sc.next();
tianJia(list,name,age,familyA,studentId);
}
case 2 -> {
System.out.println("请输入id");
String id = sc.next();
shanChu(list,id);
}
case 3 -> {
System.out.println("请输入id");
String id = sc.next();
xiuGai(list,id);
}
case 4 -> {
chaXuan(list);
}
case 5 -> {
System.out.println("退出");
// 第一种退出的写法
break aaa;
// 第二种退出的写法
// System.exit(0);
}
}
}
}
// 忘记密码方法
public static boolean userWamgJi(ArrayList <UserDeom> user, String userName ){
Scanner sc = new Scanner(System.in);
System.out.println("请输入身份证");
String id = sc.nextLine();
System.out.println("请输入手机号码");
String telephone = sc.nextLine();
for (int i = 0; i < user.size(); i++) {
if(user.get(i).getUserName().equals(userName)){
System.out.println("有该用户");
if(user.get(i).getUserId().equals(id)&&user.get(i).getUserTelephone().equals(telephone)){
System.out.println("有身份证和手机号");
while (true){
System.out.println("请输入新密码");
String password1 = sc.nextLine();
System.out.println("请再次确定新密码");
String password2 = sc.nextLine();
boolean b = userPasswordJiaoYan(password1,password2);
if(b){
user.get(i).setUserPassword(password1);
break;
}else {
System.out.println("密码不一致");
}
}
System.out.println("修改成功");
return true;
}
}else{
System.out.println("未注册,请先去注册");
return false;
}
}
return false;
}
// 登录方法
public static boolean UserDengLu(ArrayList <UserDeom> user, String userName,String userPassword){
for(int i=0;i<user.size();i++){
boolean b1 = user.get(i).getUserName().equals(userName);
boolean b2 = user.get(i).getUserPassword().equals(userPassword);
System.out.println(b1);
System.out.println(b2);
if(b1 && b2 ){
return true;
}
}
return false;
}
// 注册方法
public static void userZhuChe(ArrayList<UserDeom> user){
Scanner sc = new Scanner(System.in);
while(true){
System.out.println("请输入用户名");
String username = sc.nextLine();
boolean flag = userJiaoYan(user,username);
if(!flag) {
System.out.println("用户名错误"+flag);
System.out.println("请重新注册");
continue;
};;
System.out.println(flag);
System.out.println("请输入密码");
String userpassword1 = sc.nextLine();
System.out.println("请再次输入密码");
String userpassword2 = sc.nextLine();
boolean flag1 = userPasswordJiaoYan(userpassword1,userpassword2);
if(!flag1) {
System.out.println("密码错误"+flag1);
System.out.println("请重新注册");
continue;
};
System.out.println("请输入电话");
String usertelephone = sc.nextLine();
boolean flag2 = userTelephone(usertelephone);
if(!flag2) {
System.out.println("电话错误" +flag2);
System.out.println("请重新注册");
continue;
};
System.out.println(flag2);
System.out.println("请输入身份证号");
String userid = sc.nextLine();
boolean flag3 = userIdJiaoYan(userid);
System.out.println(flag3);
if(!flag3) {
System.out.println("身份证号错误" +flag3);
System.out.println("请重新注册");
continue;
};
UserDeom ud = new UserDeom(username,userpassword1,userid,usertelephone);
user.add(ud);
if(flag && flag1 && flag2 && flag3) {
System.out.println("注册成功");
break;
};
}
}
// 登录用户名的校验
public static boolean userJiaoYan(ArrayList <UserDeom>userS,String userName){
for (int i = 0; i < userS.size(); i++){
boolean b1 = userS.get(i).getUserName().equals(userName); //判断是不是唯一
// System.out.println(b1);
if(b1){
System.out.println("用户已经存在");
return false;
}
}
for (int i = 0; i<userName.length();i++){
int a = userName.charAt(i);
if(a>=65&&a<=90 || a>=97&&a<=122 || a>=48&&a<=57){
if( a > 57){
System.out.println("不是纯数字");
return true;
}
}else{
System.out.println("要求输入的是字符加数字");
return false;
}
}
if(userName.length()>=3 && userName.length()<=15){
// System.out.println("长度符合");
} else {
return false;
}
//判断用户的长度
return true;
}
// 校验密码
public static boolean userPasswordJiaoYan(String s1,String s2){
return s1.equals(s2);
}
// 身份证验证
public static boolean userIdJiaoYan(String s1){
if(s1.length() == 18){
// System.out.println("长度为18");
} else{
System.out.println("长度不为18");
return false ;
}
if (s1.charAt(0) != '0') {
// System.out.println("首位不为0");
}else{
System.out.println("首位不能为0");
return false;
}
for (int i = 0; i < s1.length()-1; i++) {
if(s1.charAt(i) >=48 && s1.charAt(i) <=57){
// System.out.println("对的");
}else{
System.out.println("要求全部是数字");
return false;
}
}
if(s1.charAt(s1.length()-1) == 'x' || s1.charAt(s1.length()-1) == 'X' || s1.charAt(s1.length()-1) >=48 && s1.charAt(s1.length()-1) <=57){
// System.out.println("是数字或者大小写的x");
}else{
System.out.println("要求最后一位必须是数字或者是X或x");
return false;
}
return true;
}
// 手机号的校验
public static boolean userTelephone(String s){
if(s.length() == 11){
// System.out.println("长度正确");
}else{
System.out.println("长度不正确");
return false;
}
if(s.charAt(0) != '0'){
// System.out.println("不为0");
}else{
System.out.println("不能以0开头");
return false;
}
for (int i = 0; i <s.length()-1; i++) {
int a = s.charAt(i);
if(a>=48&&a<=57){
// System.out.println("全部是数字");
}else{
System.out.println("必须全部是数字");
return false;
}
}
return true;
}
// 判断是否正确
public static int weiYi (ArrayList <Student> s, String id){
int temp = -1;
for(int i=0;i<s.size();i++){
if(s.get(i).getId().equals(id)){
temp=i;
break;
}
}
return temp;//不相等就返回false
}
// 添加功能
public static void tianJia(ArrayList <Student> s,String name, int age, String family, String id){
if(weiYi(s,id) != -1){
Student stu = new Student(name,age,family,id);
s.add(stu);
System.out.println("添加成功");
}else{
System.out.println("id已经存在");
}
}
// 查询内容
public static void chaXuan(ArrayList <Student> s){
System.out.print("id\t\t姓名\t\t年龄\t\t家庭住址\n");
for(int i=0;i<s.size();i++){
System.out.print(s.get(i).getId()+"\t\t" + s.get(i).getName()+"\t\t"+s.get(i).getAge()+"\t\t"+s.get(i).getFamily()+"\n");
}
}
// 删除系统
public static void shanChu(ArrayList <Student> s,String id){
int temp = -1;
temp = weiYi(s,id);
if(temp!=-1){
s.remove(temp);
}else{
System.out.println("id不存在");
}
}
// 修改功能
public static void xiuGai(ArrayList <Student> s,String id){
int temp = -1;
Scanner sc = new Scanner(System.in);
temp = weiYi(s,id);
if(temp!=-1){
System.out.println("请输入你的id");
String studentId = sc.nextLine();
s.get(temp).setId(studentId);
System.out.println("请输入你的姓名");
String name = sc.nextLine();
s.get(temp).setName(name);
System.out.println("请输入你的年龄");
int age = sc.nextInt();
s.get(temp).setAge(age);
System.out.println("请输入你的家庭地址");
String familyA = sc.next();
s.get(temp).setFamily(familyA);
System.out.println("修改成功");
}else{
System.out.println("id不存在");
}
}