教师管理系统

大概功能:

1.显示所有教师 2.按姓名查找教师 3.按工号查找教师 4.增加教师 5.删除教师 6.退出

数据会保存到 txt 文件里面

姓名:必须是中文

手机号码:必须是11位,必须是数字

效果展示:

代码展示:

TeacherSystem类:

java 复制代码
package TEACHERSYSTEM;

import java.io.*;
import java.util.ArrayList;
import java.util.Objects;
import java.util.Random;
import java.util.Scanner;

public class TeacherSystem {
    public static void main(String[] args) {
        ArrayList<User> list = new ArrayList<>();
        User u1 = new User("1238", "张三", "18716699212", "35612341243123@qq.com", 701);
        User u2 = new User("1234", "李四", "18716699212", "35612341243123@qq.com", 801);
        User u3 = new User("12345", "王五", "18716699212", "35612341243123@qq.com", 901);
        list.add(u1);
        list.add(u2);
        list.add(u3);

        // 从文件加载数据
        LoadFromFile(list);

        // 启动登录流程
        login(list);

        // 保存数据到文件
        SaveToFile(list);
    }

    private static void login(ArrayList<User> list) {
        Scanner sc = new Scanner(System.in);
        System.out.println("请输入工号:");
        while (true) {
            String inputID = sc.next();
            boolean found = false;
            for (User user : list) {
                if (user.getId().equals(inputID)) {
                    found = true;
                    break;
                }
            }
            if (found) {
                break;
            } else {
                System.out.println("账号 " + inputID + " 不存在,请重新输入");
            }
        }

        System.out.println("请输入姓名:");
        while (true) {
            String inputName = sc.next();
            boolean found = false;
            for (User user : list) {
                if (Objects.equals(user.getName(), inputName)) {
                    found = true;
                    break;
                }
            }
            if (found) {
                break;
            } else {
                System.out.println("姓名 " + inputName + " 不存在,请重新输入");
            }
        }

        // 获取验证码
        while (true) {
            String rightCode = getCode();
            System.out.println("当前正确的验证码为:" + rightCode);
            System.out.print("请输入验证码:");
            String code = sc.next();
            if (code.equalsIgnoreCase(rightCode)) {
                System.out.println("验证码正确");
                OperaterTeacher(list);
                break;
            } else {
                System.out.println("验证码错误,请重新输入");
            }
        }
        sc.close();
    }

    public static void OperaterTeacher(ArrayList<User> list) {
        Scanner sc = new Scanner(System.in);
        while (true) {
            System.out.println("---------------------------------------------------------------------");
            System.out.println("Welcome to the use 教师管理系统");
            System.out.println("请选择你需要进行的操作:1.显示所有教师 2.按姓名查找教师 3.按工号查找教师 4.增加教师 5.删除教师 6.退出");
            System.out.println("---------------------------------------------------------------------");
            int choose = sc.nextInt();
            switch (choose) {
                case 1:
                    ShowAllTeacher(list);
                    break;
                case 2:
                    SeekName(list);
                    break;
                case 3:
                    SeekId(list);
                    break;
                case 4:
                    AddTeacher(list);
                    break;
                case 5:
                    DeleteTeacher(list);
                    break;
                case 6:
                    System.out.println("退出管理员操作页面");
                    return;
                default:
                    System.out.println("没有该选项");
            }
        }
    }

    public static void ShowAllTeacher(ArrayList<User> list) {
        System.out.println("工号\t\t\t姓名\t\t\t电话\t\t\t\t邮箱\t\t\t\t\t班级");
        for (User user : list) {
            System.out.println(user.getId() + "\t\t\t" + user.getName() + "\t\t\t" + user.getPhoneNumber() + "\t\t\t" + user.getLetterbox() + "\t\t" + user.getClassNumber());
        }
    }

    public static void SeekName(ArrayList<User> list) {
        Scanner sc = new Scanner(System.in);
        while (true) {
            System.out.println("请输入姓名:");
            String inputName = sc.next();
            boolean found = false;
            for (User user : list) {
                if (user.getName().equals(inputName)) {
                    System.out.println("工号\t\t\t姓名\t\t\t电话\t\t\t\t邮箱\t\t\t\t班级");
                    System.out.println(user.getId() + "\t\t\t" + user.getName() + "\t\t\t" + user.getPhoneNumber() + "\t\t\t" + user.getLetterbox() + "\t\t" + user.getClassNumber());
                    found = true;
                }
            }
            if (!found) {
                System.out.println("输入的姓名不存在,请重新输入");
            } else {
                return; // 如果找到教师信息,退出循环
            }
        }
    }

    public static void SeekId(ArrayList<User> list) {
        Scanner sc = new Scanner(System.in);
        while (true) {
            System.out.println("请输入工号:");
            String inputID = sc.next();
            boolean found = false;
            for (User user : list) {
                if (user.getId().equals(inputID)) {
                    System.out.println("工号\t\t\t姓名\t\t\t电话\t\t\t\t邮箱\t\t\t\t班级");
                    System.out.println(user.getId() + "\t\t\t" + user.getName() + "\t\t\t" + user.getPhoneNumber() + "\t\t\t" + user.getLetterbox() + "\t\t" + user.getClassNumber());
                    found = true;
                }
            }
            if (!found) {
                System.out.println("输入的工号不存在,请重新输入");
            } else {
                return;
            }
        }
    }

    public static void AddTeacher(ArrayList<User> list) {
        Scanner sc = new Scanner(System.in);
        System.out.println("请输入新教师工号:");
        String newID = sc.next();
        if (contain(list, newID)) {
            System.out.println("该工号已经存在,请选择其他工号");
            return;
        }
        String newName;
        while (true) {
            System.out.println("请输入新教师的姓名:");
            newName = sc.next();
            if (checkChineseName(newName)) {
                break;
            } else {
                System.out.println("输入无效,请输入中文姓名。");
            }
        }

        String newPhone;
        while (true) {
            System.out.println("请输入新教师的手机号码:");
            sc.nextLine();
            newPhone = sc.nextLine();
            boolean flag = checkPhoneNumber(newPhone);
            if (flag) {
                break;
            } else {
                System.out.println("手机号码格式错误,请重新输入");
            }
        }

        System.out.println("请输入新教师的邮箱账号:");
        String newLetterBox = sc.next();
        System.out.println("请输入新教师的所在班级:");
        int newClassNumber = Integer.parseInt(sc.next());

        User newUser = new User(newID, newName, newPhone, newLetterBox, newClassNumber);
        list.add(newUser);
        System.out.println("教师添加成功");
    }

    public static void DeleteTeacher(ArrayList<User> list) {
        Scanner sc = new Scanner(System.in);
        System.out.println("请输入要删除的工号:");
        String IDToDelete = sc.next();
        User AimUser = findUserByAccount(list, IDToDelete);
        if (AimUser == null) {
            System.out.println("用户账号不存在");
            return;
        }
        list.remove(AimUser);
        System.out.println("用户删除成功");
    }

    private static boolean checkChineseName(String name) {
        return name.matches("^[\\u4e00-\\u9fa5]+$"); // 判断是否全为中文
    }

    private static boolean checkPhoneNumber(String phoneNumber) {
        int len = phoneNumber.length();
        // 检查手机号码长度并且只包含数字
        return len == 11 && phoneNumber.matches("\\d+");
    }

    // 用户者寻找用户
    private static User findUserByAccount(ArrayList<User> list, String ID) {
        for (User user : list) {
            if (user.getId().equals(ID)) {
                return user;
            }
        }
        return null;
    }

    public static boolean contain(ArrayList<User> list, String ID) {
        for (User user : list) {
            if (ID.equals(user.getId())) {
                return true;
            }
        }
        return false;
    }

    private 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));
        }
        Random r = new Random();
        StringBuilder sb = new StringBuilder();
        for (int i = 0; i < 4; i++) {
            int index = r.nextInt(list.size());
            char c = list.get(index);
            sb.append(c);
        }
        int number = r.nextInt(10);
        sb.append(number);
        char[] arr = sb.toString().toCharArray();
        int randomIndex = r.nextInt(arr.length);
        char temp = arr[randomIndex];
        arr[randomIndex] = arr[arr.length - 1];
        arr[arr.length - 1] = temp;
        return new String(arr);
    }

    private static void LoadFromFile(ArrayList<User> list) {
        try (BufferedReader br = new BufferedReader(new FileReader("teachers.txt"))) {
            String line;
            line = br.readLine(); // 读取并忽略第一行标题
            while ((line = br.readLine()) != null) {
                String[] parts = line.split(",");
                if (parts.length == 5) {
                    String id = parts[0];
                    String name = parts[1];
                    String phone = parts[2];
                    String email = parts[3];
                    int classNumber = Integer.parseInt(parts[4]);

                    User user = new User(id, name, phone, email, classNumber);
                    list.add(user);
                }
            }
        } catch (IOException e) {
            System.out.println("加载文件失败,可能是文件不存在。");
        }
    }

    private static void SaveToFile(ArrayList<User> list) {
        try (BufferedWriter bw = new BufferedWriter(new FileWriter("teachers.txt"))) {
            bw.write("工号\t\t\t姓名\t\t\t电话\t\t\t\t邮箱\t\t\t\t\t班级");
            bw.newLine();
            for (User user : list) {
                bw.write(user.getId() + "\t\t\t" + user.getName() + "\t\t\t" + user.getPhoneNumber() + "\t\t\t" + user.getLetterbox() + "\t\t" + user.getClassNumber());
                bw.newLine();
            }
        } catch (IOException e) {
            System.out.println("保存文件失败");
        }
    }
}

User类:

java 复制代码
package TEACHERSYSTEM;

public class User {
    private String id;
    private String name;
    private String PhoneNumber;
    private String Letterbox;
    private int ClassNumber;

    public User(){}

    public User(String id, String name, String phoneNumber, String letterbox, int classNumber) {
        this.id = id;
        this.name = name;
        PhoneNumber = phoneNumber;
        Letterbox = letterbox;
        ClassNumber = classNumber;
    }

    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 String getPhoneNumber() {
        return PhoneNumber;
    }

    public void setPhoneNumber(String phoneNumber) {
        PhoneNumber = phoneNumber;
    }

    public String getLetterbox() {
        return Letterbox;
    }

    public void setLetterbox(String letterbox) {
        Letterbox = letterbox;
    }

    public int getClassNumber() {
        return ClassNumber;
    }

    public void setClassNumber(int classNumber) {
        ClassNumber = classNumber;
    }
}
相关推荐
佛系小嘟嘟32 分钟前
Android Jetpack Compose开发小组件【入门篇】
android·开发语言·android jetpack·小组件
Java知识日历1 小时前
【内含例子代码】Spring框架的设计模式应用(第二集)
java·开发语言·后端·spring·设计模式
old_power2 小时前
Linux(Ubuntu24.04)安装Eigen3库
linux·c++·人工智能
尘浮生4 小时前
Java项目实战II基于微信小程序的家庭大厨(开发文档+数据库+源码)
java·开发语言·数据库·微信小程序·小程序·maven
Java知识技术分享4 小时前
spring boot通过文件配置yaml里面的属性
java·spring boot·后端
军训猫猫头4 小时前
36.Add的用法 C#例子
开发语言·c#
一休哥助手4 小时前
深入解析Spring Boot项目的类加载与启动流程
java·spring boot·后端
丁总学Java4 小时前
定义一个名为 MyCache 的 Spring 配置类
java·spring
m0_634601665 小时前
2025.1.2
java·服务器·数据库