APP
package test4; import student1.StudentSystem; import java.util.ArrayList; import java.util.Random; import java.util.Scanner; public class APP { public static void main(String[] args) { ArrayList<User> list = new ArrayList<>(); loop: while (true) { System.out.println("欢迎来到学生管理系统"); System.out.println("请选择操作1登录 2注册 3忘记密码 4退出"); Scanner sc = new Scanner(System.in); System.out.println("请输入选择"); String choose = sc.next(); switch (choose) { case "1": register(list); break; case "2": login(list); break; case "3": forgetPassword(list); break; case "4": System.out.println("4.退出"); break loop; default: System.out.println("没有这个选项"); } } } //注册功能: public static void login(ArrayList<User> list) { //用户名、密码、身份证号码、手机号码 //1.键盘录入用户名 Scanner sc = new Scanner(System.in); String usernamne; while (true) { System.out.println("请输入用户名"); usernamne = sc.next(); boolean flag1 = checkUsername(usernamne); if (!flag1) { System.out.println("不满足条件重新输入,请重新输入"); continue; } boolean flag2 = contains(list, usernamne); if (flag2) { System.out.println("用户名存在 " + usernamne + "请重新输入"); } else { System.out.println("用户名不存在 " + usernamne + "可用"); break; } } //2.键盘录入密码 String password; while (true) { System.out.println("请输入注册密码"); password = sc.next(); System.out.println("请再次输入密码"); String password1 = sc.next(); if (!password.equals(password1)) { System.out.println("两次密码输入不一样,请在此输入"); continue; } else { System.out.println("两次密码录入一样 继续录入其他数字"); break; } } //3.键盘录入身份证号码 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; } } //4.键盘录入手机号 String phoneNumber; while (true) { System.out.println("请输入手机号码"); phoneNumber = sc.next(); boolean flag = checkphoneNumber(phoneNumber); if (flag) { System.out.println("手机号码格式正确,请继续添加信息"); break; } else { System.out.println("手机号码格式有误,请重新输入"); continue; } } //5.放到用户对象,创建集合 User u = new User(usernamne, password, personID, phoneNumber); list.add(u); System.out.println("注册成功"); //遍历集合 for (int i = 0; i < list.size(); i++) { User u1 = list.get(i); System.out.println(u1.getName() + "," + u1.getPassword() + "," + u1.getPersonID() + "," + u1.getPhoneNumber()); } } //手机号码验证 private static boolean checkphoneNumber(String phoneNumber) { //长度为11 //不能以0开头 //必须是数字 if (phoneNumber.length() != 11) { return false; } if (phoneNumber.charAt(0) == '0') { return false; } for (int i = 0; i < phoneNumber.length(); i++) { char c = phoneNumber.charAt(i); if (!(c >= '0' && c <= '9')) { return false; } } return true; } //身份证号码验证 private static boolean checkpersonID(String personID) { //长度为18 //不能0开头 //前17位必须是数字 //最后一位可以是数字也可以是 X x if (personID.length() != 18) { return false; } char c = personID.charAt(0); if (c == '0') { return false; } for (int i = 0; i < personID.length() - 1; i++) { char c1 = personID.charAt(i); if (!(c1 >= '0' && c1 <= '9')) { return false; } } char endChar = personID.charAt(personID.length() - 1); if ((endChar >= '0' && endChar <= '9') || (endChar == 'X') || (endChar == 'x')) { return true; } else { return false; } } //登录功能: public static void register(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("用户名未注册,请先注册"); return; } System.out.println("请输入密码"); String password = sc.next(); String reishtcode = getCode(); while (true) { System.out.println("当前正确的验证码为:" + reishtcode); System.out.println("请输入验证码"); String code = sc.next(); if (code.equalsIgnoreCase(reishtcode)) { System.out.println("验证码正确"); break; } else { System.out.println("验证码错误,请重新输入"); continue; } } //验证用户名和密码是否正确 //集合中的用户名密码 //创建个整体 直接用就行 不需要零散的数据 User UserInfo = new User(username, password, null, null); boolean result = checkUserInfo(list, UserInfo); if (result) { System.out.println("登录成功,可以开始使用学生管理系统"); StudentSystem ss = new StudentSystem(); ss.startStudentSystem(); break; } else { System.out.println("登录失败,用户名或密码错误"); } if (i == 2) { System.out.println("当前账号"+username +"被锁定,请联系黑马程序员客服:xXX-xxXXX"); return; }else { System.out.println("用户名或密码错误,还剩下"+(2-i)+"次机会"); } } } //判断账号密码是否相等 private static boolean checkUserInfo(ArrayList<User> list, User userInfo) { //遍历集合判断 for (int i = 0; i < list.size(); i++) { User u1 = list.get(i); if (u1.getName().equals(userInfo.getName()) && u1.getPassword().equals(userInfo.getPassword())) { return true; } }return false; } //忘记密码: public static void forgetPassword(ArrayList<User> list) { Scanner sc = new Scanner(System.in); System.out.println("请输入用户名"); String userName = sc.next(); boolean flag = contains(list, userName); if (!flag) { System.out.println("当前未注册"); return; } //键盘录入身份证 手机号 System.out.println("请输入身份证号码"); String personID = sc.next(); System.out.println("请输入手机号码"); String phoneNumber = sc.next(); //比较身份证和手机号是否相等 //需要先把用户对象通过索引找拿出来 int index=finaIndex(list,userName); User user = list.get(index); //比较身份证和手机号是否相等 if(!(user .getPersonID().equalsIgnoreCase(personID) && user.getPhoneNumber().equals(phoneNumber))){ System.out.println("身份证号码或者手机号码有误"); return; } //修改密码 String password; while (true) { System.out.println("请输入密码"); password = sc.next(); System.out.println("请再次输入密码"); String password1 = sc.next(); if (!password.equals(password1)) { System.out.println("两次密码输入不一样,请在此输入"); continue; } else { System.out.println("两次密码录入一样 继续录入其他数字"); break; } } //直接修改即可 user.setPassword(password); System.out.println("密码修改成功"); } //需要先把用户对象拿出来 private static int finaIndex(ArrayList<User> list, String userName) { for (int i = 0; i < list.size(); i++) { User u1 = list.get(i); if (u1.getName().equals(userName)) { return i; } } return -1; } //用户名判断 public 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 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; } // 用户名唯一 public static boolean contains(ArrayList<User> list, String username) { for (int i = 0; i < list.size(); i++) { User user = list.get(i); String rightname = user.getName(); if (rightname.equals(username)) { return true; } } return false; } //生成一个验证码 public 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(); //获得4个随机字母 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 nextInt = r.nextInt(10); sb.append(nextInt); //数字添加到随机位置 char[] arr = sb.toString().toCharArray(); for (int i = 0; i < arr.length; i++) { int randomIndex = r.nextInt(arr.length); //最大索引跟随机索引交换 int temp = arr[randomIndex]; arr[randomIndex] = arr[i]; arr[i] = (char) temp; } return new String(arr); } }
student
package test4; public class Student { private String id; private String name; private int age; private String address; public Student() { } public Student(String address, int age, String id, String name) { this.address = address; this.age = age; this.id = id; this.name = name; } public String getAddress() { return address; } public void setAddress(String address) { this.address = address; } public int getAge() { return age; } public void setAge(int age) { this.age = age; } public String getId() { return id; } public void setId(String id) { this.id = id; } public String getName() { return name; } public void setName(String name) { this.name = name; } }
studentsystem
package test4;
import student1.Student;
import java.util.ArrayList;
import java.util.Scanner;
public class StudentSystem {
public static void main(String[] args) {
ArrayList<student1.Student> list = new ArrayList<>();
loop:
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("请输入您的选择:");
Scanner sc = new Scanner(System.in);
String choose = sc.next();
switch (choose) {
case "1":
addStudent(list);
break;
case "2":
deleteStudent(list);
break;
case "3":
updateStudent(list);
break;
case "4":
queryStudent(list);
break;
case "5":
System.out.println("退出");
break loop;
default:
System.out.println("没有这个选项");
}
}
}
//添加学生
public static void addStudent(ArrayList<student1.Student> list) {
student1.Student s = new student1.Student();
Scanner sc = new Scanner(System.in);
String id = null;
while (true) {
System.out.println("请输入ID");
id = sc.next();
boolean flag = containsStudent(list, id);
if (flag) {
System.out.println("id 已经存在 请重新录入");
} else {
s.setId(id);
break;
}
}
System.out.println("请输入姓名");
String name = sc.next();
s.setName(name);
System.out.println("请输入年龄");
int age = sc.nextInt();
s.setAge(age);
System.out.println("请输入家庭住址");
String score = sc.next();
s.setAddress(score);
list.add(s);
System.out.println("学生信息添加成功");
}
//删除学生
public static void deleteStudent(ArrayList<student1.Student> list) {
Scanner sc = new Scanner(System.in);
System.out.println("请输入要删除的id");
String id = sc.next();
//查询Id在集合的索引
int index = getIndex(list, id);
if (index >= 0) {
list.remove(index);
System.out.println("id为"+id+"的学生被删除");
}else {
System.out.println("ID不存在");
}
}
//修改学生
public static void updateStudent(ArrayList<student1.Student> list) {
Scanner sc = new Scanner(System.in);
System.out.println("请输入要修改的学生id");
String id = sc.next();
int index = getIndex(list, id);
if (index == -1) {
System.out.println("id不存在");
return;
}
student1.Student stu = list.get(index);
System.out.println("请输入新的姓名");
String newName = sc.next();
stu.setName(newName);
System.out.println("请输入新的年级");
int newAge = sc.nextInt();
stu.setAge(newAge);
System.out.println("请输入新的家庭住址");
String newAddress = sc.next();
stu.setAddress(newAddress);
System.out.println("学生信息修改完成");
}
// 查询学生
public static void queryStudent(ArrayList<student1.Student> list) {
if (list.size() == 0) {
System.out.println("没有这个信息,请添加后再查询");
return;
}
System.out.println("id \t姓名 \t年龄\t 家庭住址");
for (int i = 0; i < list.size(); i++) {
student1.Student stu = list.get(i);
System.out.println(stu.getId() + "\t" + stu.getName() + "\t" + stu.getAge() + "\t" + stu.getAddress());
}
}
//判断唯一性
public static boolean containsStudent(ArrayList<student1.Student> list, String ID) {
/* for (int i = 0; i < list.size(); i++) {
Student stu = list.get(i);
String uid = stu.getId();
if (uid.equals(ID)) {
return true;
}
}*/
return getIndex(list, ID) >= 0;
}
public static int getIndex(ArrayList<student1.Student> list, String ID){
for (int i = 0; i < list.size(); i++) {
Student stu = list.get(i);
String uid = stu.getId();
if (uid.equals(ID)) {
return i;
}
}return -1;
}
}
user
package test4; public class User { //用户名、密码、身份证号码、手机号码 private String name; private String password; private String personID; private String phoneNumber; public User() { } public User(String name, String password, String personID, String phoneNumber) { this.name = name; this.password = password; this.personID = personID; this.phoneNumber = phoneNumber; } 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; } public String getPersonID() { return personID; } public void setPersonID(String personID) { this.personID = personID; } public String getPhoneNumber() { return phoneNumber; } public void setPhoneNumber(String phoneNumber) { this.phoneNumber = phoneNumber; } }