定义学生类型
package ljz.studentsystem;
public class Student {
private String id;
private String name;
private int age;
private String address;
public Student() {
}
public Student(String id, String name, int age, String address) {
this.id = id;
this.name = name;
this.age = age;
this.address = address;
}
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;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
public String getAddress() {
return address;
}
public void setAddress(String address) {
this.address = address;
}
}
定义用户类型
package ljz.studentsystem;
public class User {
private String userName;
private String passage;
private String idCard;
private String phoneNumber;
public User() {
}
public User(String userName, String passage, String idCard, String phoneNumber) {
this.userName = userName;
this.passage = passage;
this.idCard = idCard;
this.phoneNumber = phoneNumber;
}
public String getUserName() {
return userName;
}
public void setUserName(String userName) {
this.userName = userName;
}
public String getPassage() {
return passage;
}
public void setPassage(String passage) {
this.passage = passage;
}
public String getIdCard() {
return idCard;
}
public void setIdCard(String idCard) {
this.idCard = idCard;
}
public String getPhoneNumber() {
return phoneNumber;
}
public void setPhoneNumber(String phoneNumber) {
this.phoneNumber = phoneNumber;
}
}
定义初始登录页面
package ljz.studentsystem;
import java.util.ArrayList;
import java.util.Random;
import java.util.Scanner;
public class App {
private static final String LOGIN = "1";
private static final String REGISTER = "2";
private static final String FORGET = "3";
private static final String EXIT = "4";
public static void main(String[] args) {
ArrayList<User> list = new ArrayList<>();
Scanner sc = new Scanner(System.in);
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("请输入您选择的业务编号:");
String choose = sc.next();
switch (choose) {
case LOGIN -> login(list);
case REGISTER -> register(list);
case FORGET -> forget(list);
case EXIT -> {
System.out.println("感谢使用,再见。");
System.exit(0);
}
default -> System.out.println("输入信息有误!");
}
}
}
private static void forget(ArrayList<User> list) {
System.out.println("请输入你的用户名:");
Scanner sc = new Scanner(System.in);
String username = sc.next();
boolean flag1 = repeatabilityJudge(username, list);
if (!flag1) {
System.out.println("当前账户不存在");
return;
}
System.out.println("请输入你的身份证:");
String idcard = sc.next();
System.out.println("请输入你的电话号码:");
String phonenumber = sc.next();
int index = findindex(username, list);
User user = list.get(index);
if ((user.getIdCard().equals(idcard) && (user.getPhoneNumber().equals(phonenumber)))) {
System.out.println("请输入你要修改的密码");
String newpassage = sc.next();
user.setPassage(newpassage);
System.out.println("修改成功!");
} else {
System.out.println("账号信息不匹配,修改失败");
}
}
private static void login(ArrayList<User> list) {
//登录流程
Scanner sc = new Scanner(System.in);
while (true) {
//验证码判断
String rightauthcode = requirecode();
System.out.println("当前的验证码为:" + rightauthcode);
System.out.println("请输入验证码:");
String authcode = sc.next();
if (authcode.equals(rightauthcode)) {
break;
} else {
System.out.println("验证码错误,请重新输入");
}
}
//账户密码判断,只有三次机会
for (int i = 0; i < 3; i++) {
System.out.println("请输入用户名:");
String username = sc.next();
boolean flag1 = repeatabilityJudge(username, list);
if (!flag1) {
System.out.println("用户未注册,请先注册。");
return;
}
System.out.println("请输入密码:");
String passage = sc.next();
User u = new User(username, passage, null, null);
boolean flag2 = accuracyJudge(u, list);
if (flag2) {
System.out.println("登录成功!");
break;
} else {
System.out.println("登录失败,请重试");
int change = 3 - i;
System.out.println("您还剩余" + change + "次机会");
}
}
}
public static void register(ArrayList<User> list) {
//注册流程
Scanner sc = new Scanner(System.in);
User u = new User();
while (true) {
//用户的用户名不能重复
System.out.println("请输入用户名:");
String username = sc.next();
boolean flag1 = formatJudge(username);
boolean flag2 = repeatabilityJudge(username, list);
if (flag1) {
System.out.println("用户名格式有误");
} else if (flag2) {
System.out.println("用户名重复");
} else {
u.setUserName(username);
list.add(u);
break;
}
}
while (true) {
//密码输入两次以确保输入无误
System.out.println("请输入密码");
String massage1 = sc.next();
System.out.println("请再次输入密码");
String massage2 = sc.next();
if (massage1.equals(massage2)) {
u.setPassage(massage1);
list.add(u);
break;
} else {
System.out.println("两次输入密码不一致");
}
}
while (true) {
//输入身份证号
System.out.println("请输入身份证号码");
String idcard = sc.next();
boolean flag3 = idcardJudge(idcard);
if (flag3) {
System.out.println("身份证号码输入有误");
} else {
u.setIdCard(idcard);
list.add(u);
break;
}
}
while (true) {
//输入电话
System.out.println("请输入电话号码");
String phonenumber = sc.next();
boolean flag4 = phoneJudge(phonenumber);
if (flag4) {
System.out.println("电话号码输入有误");
} else {
u.setPhoneNumber(phonenumber);
list.add(u);
break;
}
}
}
public static boolean repeatabilityJudge(String username, ArrayList<User> list) {
//判断属性是否重复
for (int i = 0; i < list.size(); i++) {
User u = list.get(i);
String str = u.getUserName();
if (username.equals(str)) {
return true;
}
}
return false;
}
public static boolean formatJudge(String username) {
//格式判断
//用户名长度必须在3~15位之间
//只能是字母加数字的组合,但是不能是纯数字
int len = username.length();
if (len > 15 || len < 3) {
return true;
}
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++;
}
}
if (count > 0) {
return false;
}
return true;
}
public static boolean idcardJudge(String idcard) {
//身份证格式判断
//长度为18位
//不能以0为开头
//前17位,必须都是数字
//最为一位可以是数字,也可以是大写X或小写x
int len = idcard.length();
if (len != 18) {
return true;
}
if (idcard.charAt(0) == '0') {
return true;
}
for (int i = 0; i < idcard.length(); i++) {
char c = idcard.charAt(i);
if (i < len - 1) {
if (!(c >= '0' && c <= '9')) {
return true;
}
} else {
if (!((c >= '0' && c <= '9') || c == 'x' || c == 'X')) {
return true;
}
}
}
return false;
}
public static boolean phoneJudge(String phonenumber) {
//电话号码格式判断
//长度为11位
//不能以0为开头
//必须都是数字
int len = phonenumber.length();
if (phonenumber.length() != 11) {
return true;
}
if (phonenumber.charAt(0) == '0') {
return true;
}
for (int i = 0; i < phonenumber.length(); i++) {
char c = phonenumber.charAt(i);
if (c < '0' || c > '9') {
return true;
}
}
return false;
}
public static String requirecode() {
//生成验证码
ArrayList<Character> list = new ArrayList<>();
//录入大小写字母
for (int i = 0; i < 26; i++) {
list.add((char) ('a' + i));
list.add((char) ('A' + i));
}
//随机生成一位数字
Random r = new Random();
int num = r.nextInt(10);
//初步拼接
StringBuilder sb = new StringBuilder();
for (int i = 0; i < 4; i++) {
int index = r.nextInt(list.size());
sb.append(list.get(index));
}
sb.append(num);
//格式整理
//由4位大写或者小写字母和1位数字组成,同一个字母可重复
//数字可以出现在任意位置
char[] code = sb.toString().toCharArray();
int index = r.nextInt(code.length - 1);
char temp = '0';
temp = code[index];
code[index] = code[code.length - 1];
code[code.length - 1] = temp;
StringBuilder authcode = new StringBuilder();
for (int i = 0; i < code.length; i++) {
authcode.append(code[i]);
}
String result = authcode.toString();
return result;
}
public static boolean accuracyJudge(User u, ArrayList<User> list) {
//账户密码准确性判断
for (int i = 0; i < list.size(); i++) {
User user = list.get(i);
if ((u.getUserName().equals(user.getUserName())) && (u.getPassage().equals(user.getPassage()))) {
return true;
}
}
return false;
}
public static int findindex(String username, ArrayList<User> list) {
//寻找你要修改账户的索引
for (int i = 0; i < list.size(); i++) {
User user = list.get(i);
if (user.getUserName().equals(username)) {
return i;
}
}
return -1;
}
}
定义初始使用页面
package ljz.studentsystem;
import java.util.ArrayList;
import java.util.Scanner;
public class StudentSystem {
private static final String ADD_INFORMATION = "1";
private static final String DELETE_INFORMATION = "2";
private static final String CHANGE_INFORMATION = "3";
private static final String INQUIRE_INFORMATION = "4";
private static final String EXIT = "5";
public static void main(String[] args) {
//定义初始页面
ArrayList<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 ADD_INFORMATION -> addinformation(list);
case DELETE_INFORMATION -> deleteinformation(list);
case CHANGE_INFORMATION -> changeinformation(list);
case INQUIRE_INFORMATION -> inquireinformation(list);
case EXIT -> {
break loop;
//System.exit(0);停止虚拟机运行
}
default -> System.out.println("输入信息有误!");
}
}
}
public static void addinformation(ArrayList<Student> list) {
//添加学生信息
Scanner sc = new Scanner(System.in);
Student s = new Student();
String id = null;
while (true) {
//判断id是否重复
System.out.println("请输入学生ID:");
id = sc.next();
boolean flag = Judge(list, id);
if (flag) {
System.out.println("输入ID重复请重新输入");
} else {
s.setId(id);
break;
}
}
System.out.println("请输入学生名字:");
String name = sc.next();
System.out.println("请输入学生年龄:");
int age = sc.nextInt();
System.out.println("请输入学生地址:");
String address = sc.next();
s.setName(name);
s.setAge(age);
s.setAddress(address);
list.add(s);
//System.out.println(s.getId() + " " + s.getName() + " " + s.getAge()+ " " + s.getAddress());
}
public static void deleteinformation(ArrayList<Student> list) {
System.out.println("请输入要删除的学生ID:");
Scanner sc = new Scanner(System.in);
String sid = sc.next();
boolean flag = Judge(list, sid);
if (flag) {
int i = index(list, sid);
list.remove(i);
System.out.println("删除成功!");
} else {
System.out.println("输入的学生ID不存在,请重试。");
}
}
public static void changeinformation(ArrayList<Student> list) {
System.out.println("请输入要修改信息学生的ID:");
Scanner sc = new Scanner(System.in);
String sid = sc.next();
boolean flag = Judge(list, sid);
if (flag) {
int i = index(list, sid);
Student stu = list.get(i);
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("学生信息修改成功!");
} else {
System.out.println("你输入的ID不存在请重新输入!");
}
}
public static void inquireinformation(ArrayList<Student> list) {
if (list.size() == 0) {
System.out.println("当前没有学生信息,请添加后查询。");
return;
} else {
System.out.println("id\t姓名\t年龄\t家庭住址\t");
for (int i = 0; i < list.size(); i++) {
Student stu = list.get(i);
System.out.println(stu.getId() + "\t" + stu.getName() + "\t" + stu.getAge() + "\t\t" + stu.getAddress() + "\t");
}
}
}
public static boolean Judge(ArrayList<Student> list, String id) {
//判断id是否重复
for (int i = 0; i < list.size(); i++) {
Student stu = list.get(i);
String str = stu.getId();
if (str.equals(id)) {
return true;
}
}
return false;
}
public static int index(ArrayList<Student> list, String id) {
//获取对应id的索引
for (int i = 0; i < list.size(); i++) {
Student stu = list.get(i);
String str = stu.getId();
if (str.equals(id)) {
return i;
}
}
return -1;
}
}