前言:突然想起来要记录一下自己之前做的小”项目“。基于Java的学生管理系统及其升级版。
此项目是由小白自己亲手写完未有copy成分的第一个项目,所以意义非凡!
欢迎clone我的GitHub仓库!
https://github.com/Antarctica000/Student-Management-System.git
大体需求:
初级:学生管理系统要实现最基本的增删查改功能。
升级:为学生管理系统书写一个登陆、注册、忘记密码的功能。只有用户登录成功之后,才能进入到学生管理系统中进行增删查改操作。
初级学生管理系统:
一、搭建主菜单(添加Student类)
需求:
- Student类属性:姓名、ID、年龄、家庭住址
- 初始菜单显示四大功能选项(增删查改)
代码:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56
| public class Student { private String name; private int ID; private int age; private String home;
public Student() {}
public Student(String name, int ID, int age, String home) { this.name = name; this.ID = ID; this.age = age; this.home = home; } public String getName() { return name; } public void setName(String name) { this.name = name; } public int getID() { return ID; } public void setID(int ID) { this.ID = ID; } public int getAge() { return age; } public void setAge(int age) { this.age = age; } public String getHome() { return home; } public void setHome(String home) { this.home = home; } public void showAll() { System.out.printf("%-10s %-10s %-10s %-10s%n", "id", "name", "age", "home"); System.out.println(); System.out.printf("%-10d %-10s %-10d %-10s%n", getID(), getName(), getAge(), getHome()); } public void setAll() { Scanner sc = new Scanner(System.in); System.out.println("Enter name:"); setName(sc.next()); System.out.println("Enter age:"); setAge(sc.nextInt()); System.out.println("Enter home:"); setHome(sc.next()); System.out.println("Enter ID:"); setID(sc.nextInt()); } }
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62
| public class StudentSystem { private static final String ADD_STU = "1"; private static final String DELETE_STU = "2"; private static final String REVISE_STU = "3"; private static final String ENQUIRE_STU = "4"; private static final String EXIT = "5";
static ArrayList<Student> list = new ArrayList<>(); static { list.add(new Student("juju", 3, 18, "Antarctica")); }
public static void startStudentSystem() { while (true) { menu(); Scanner sc = new Scanner(System.in); String option = sc.next(); if (choose(option, list)) break; } } public static void menu() { System.out.println("-------------Welcome to Student System-------------"); System.out.println("1:Add Student"); System.out.println("2:Delete Student"); System.out.println("3:revise Student"); System.out.println("4:enquire Student"); System.out.println("5:Exit "); System.out.println("Enter your option"); } public static boolean choose(String option, ArrayList<Student> list) { switch (option) { case ADD_STU: System.out.println("Add student:"); add(list); break; case DELETE_STU: System.out.println("Delete student:"); delete(list); break; case REVISE_STU: System.out.println("Revise student:"); revise(list); break; case ENQUIRE_STU: System.out.println("Enquire student:"); enquire(list); break; case EXIT: System.out.println("Exit"); return true; default: System.out.println("Invalid operation"); } return false; }
|
二、查询功能
1 2 3 4 5 6 7 8 9 10 11
| public static void enquire(ArrayList<Student> list) { if (list.isEmpty()) System.out.println("No student"); else { for (int i = 0; i < list.size(); i++) { list.get(i).showAll(); System.out.println(); } } }
|
三、添加功能
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23
| public static void add(ArrayList<Student> list) { Student s = new Student(); while (true) { s.setAll(); int x = s.getID(); if (judgeID(list, x)) { list.add(s); break; } else System.out.println("ID already exist! Again:"); } System.out.println("Option 1 OK"); }
public static boolean judgeID(ArrayList<Student> list, int x) { for (int i = 0; i < list.size(); i++) { if (list.get(i).getID() == x) return false; } return true; }
|
四、删除功能
1 2 3 4 5 6 7 8 9 10 11 12 13 14
| public static void delete(ArrayList<Student> list) { Scanner sc = new Scanner(System.in); System.out.println("Enter the ID:"); int x = sc.nextInt(); if (!judgeID(list, x)) { for (int i = 0; i < list.size(); i++) { if (list.get(i).getID() == x) list.remove(i); } System.out.println("Option 2 OK"); } else System.out.println("ID not exist!"); }
|
五、修改功能
1 2 3 4 5 6 7 8 9 10 11 12 13 14
| public static void revise(ArrayList<Student> list) { Scanner sc = new Scanner(System.in); System.out.println("Enter the ID:"); int x = sc.nextInt(); if (!judgeID(list, x)) { for (int i = 0; i < list.size(); i++) { if (list.get(i).getID() == x) list.get(i).setAll(); } System.out.println("Option 3 OK"); } else System.out.println("ID not exist!"); }
|
升级学生管理系统:
一、登录界面(添加User类)
需求:
- User类属性:用户名、密码、身份证号码、手机号码
代码:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49
| public class APP { static ArrayList<User> list = new ArrayList<>(); static { list.add(new User("juju000", "1", "111111111111111111", "11111111111")); }
public static void main(String[] args) { while (true) { menu(); Scanner sc = new Scanner(System.in); String option = sc.next(); if (choose(list, option)) break; } } public static void menu() { System.out.println("-------------Welcome to Student System!-------------"); System.out.println("1:Sign in"); System.out.println("2:Register"); System.out.println("3:Forget password"); System.out.println("4:Exit"); System.out.println("Enter your choose:"); } public static boolean choose(ArrayList<User> list, String option) { switch (option) { case "1": System.out.println("Sign in:"); signIn(list); break; case "2": System.out.println("Register:"); register(list); break; case "3": System.out.println("Forget password:"); forgetPassword(list); break; case "4": System.out.println("Exit"); return true; default: System.out.println("Invalid option !"); } return false; }
|
二、注册功能
需求:
用户名:
用户名唯一
用户名长度必须在3~15位之间
只能是字母加数字的组合,但是不能是纯数字
密码输入两次一致才可以进行注册。
身份证号码:
长度为18位
不能以0为开头
前17位,必须都是数字
最为一位可以是数字,也可以是大写X或小写x
手机号验证。
长度为11位
不能以0为开头
必须都是数字
代码:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116
| public static void register(ArrayList<User> list) { Scanner sc = new Scanner(System.in); User u = new User(); list.add(u); while (true) { System.out.println("Enter name:"); String name = sc.next(); u.setName(name); if (judgeName(list, name)) { u.setName(name); break; } System.out.println("Invalid! Again:"); } while (true) { String p1, p2; System.out.println("Enter password"); p1 = sc.next(); System.out.println("Enter password again:"); p2 = sc.next(); u.setPassword(p1); if (p1.equals(p2)) { u.setPassword(p1); break; } System.out.println("Not same! Again:"); } while (true) { System.out.println("Enter ID:"); String ID = sc.next(); u.setID(ID); if (judgeID(ID)) { u.setID(ID); break; } System.out.println("Invalid! Again:"); } while (true) { System.out.println("Enter phone:"); String p = sc.next(); u.setPhone(p); if (judgePhone(p)) { u.setPhone(p); break; } System.out.println("Invalid! Again:"); } System.out.println("Option 1 OK"); }
public static boolean judgeName(ArrayList<User> list, String name) { int length = name.length(); if (length < 3 || length > 15) return false; char[] n = name.toCharArray(); boolean isLetter = false; boolean isNum = false; for (int i = 0; i < length; i++) { if (n[i] < 48 || (n[i] > 57 && n[i] < 65) || (n[i] > 90 && n[i] < 97) || n[i] > 122) return false; if (n[i] > 57) isLetter = true; if (n[i] <= 57) isNum = true; } if (!(isLetter && isNum)) return false; for (int i = 0; i < list.size() - 1; i++) { if (list.get(i).getName().equals(name)) return false; } return true; }
public static boolean judgeID(String ID) { if (ID.length() != 18) return false; if (ID.charAt(0) == '0') return false; char[] ch = ID.toCharArray(); for (int i = 0; i < ID.length() - 1; i++) { if (ch[i] < 48 || ch[i] > 57) return false; } if (!((ch[17] >= 48 && ch[17] <= 57) || ch[17] == 'X' || ch[17] == 'x')) return false; return true; }
public static boolean judgePhone(String p) { if (p.length() != 11) return false; if (p.charAt(0) == '0') return false; char[] ch = p.toCharArray(); for (int i = 0; i < p.length(); i++) { if (ch[i] < 48 || ch[i] > 57) return false; } return true; }
|
三、登录功能
需求:
代码:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68
| public static void signIn(ArrayList<User> list) { Scanner sc = new Scanner(System.in); String name = checkUser(list); if (name.isEmpty()) return; while (true) { String s1 = getCode(); System.out.println("The code:" + s1); System.out.println("Enter the code:"); String s2 = sc.next(); if (s1.equals(s2)) break; System.out.println("Wrong! Again:"); } int count = 0; loop: while (true) { count++; System.out.println("Enter the password:"); String password = sc.next(); for (int i = 0; i < list.size(); i++) { if (list.get(i).getName().equals(name)) { if (list.get(i).getPassword().equals(password)) break loop; } } System.out.println("Wrong! Only " + (3 - count) + " times!"); if (count == 3) return; } System.out.println("Option 2 OK"); StudentSystem ss = new StudentSystem(); ss.startStudentSystem(); }
public static String checkUser(ArrayList<User> list) { Scanner sc = new Scanner(System.in); System.out.println("Enter your name:"); String name = sc.next(); for (int i = 0; i < list.size(); i++) { if (list.get(i).getName().equals(name)) return name; } System.out.println("no exit!Register first!"); return ""; }
public static String getCode() { ArrayList<Character> list = new ArrayList<>(); StringBuilder sb = new StringBuilder(); Random r = new Random(); for (int i = 0; i < 26; i++) { list.add((char) ('a' + i)); list.add((char) ('A' + i)); } for (int i = 0; i < 4; i++) { int x = r.nextInt(52); sb.append(list.get(x)); } int x = r.nextInt(10); int index = r.nextInt(5); sb.insert(index, x); return sb.toString(); }
|
四、忘记密码
需求:
代码:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26
| public static void forgetPassword(ArrayList<User> list) { Scanner sc = new Scanner(System.in); String name = checkUser(list); if (name.isEmpty()) return; System.out.println("Enter phone:"); String phone = sc.next(); System.out.println("Enter ID:"); String ID = sc.next(); for (int i = 0; i < list.size(); i++) { if (list.get(i).getName().equals(name)) { if (list.get(i).getPhone().equals(phone) && list.get(i).getID().equals(ID)) { System.out.println("Enter password:"); list.get(i).setPassword(sc.next()); break; } else { System.out.println("Failed!"); return; } } } System.out.println("Option 3 OK"); }
|