教师管理系统

news/2024/12/28 11:14:51/

大概功能:

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;}
}


http://www.ppmy.cn/news/1558773.html

相关文章

vue2/3,Spring Boot以及生产环境跨域解决方案

vue2和vue3跨域解决方案 Vue 2 (基于 Webpack) 的跨域解决方案 1. 创建或编辑 vue.config.js 文件 Vue CLI为Webpack项目提供了简单的代理配置方式。你可以通过创建或编辑项目的根目录下的 vue.config.js 文件来设置开发服务器的代理规则&#xff1a; // vue.config.js mod…

第七节:GLM-4v-9b模型的视觉模型源码解读

文章目录 前言一、EVA2CLIPModel视觉编码模块结构二、PatchEmbedding图像分块源码解读三、GLM的transformer结构源码解读四、GLU映射方法源码解读前言 清华智普的GLM-4v-9b模型,作为优化的多模态大模型,特别适用于国内应用场景,解决了国外模型本地化不足的问题。本专栏提供…

Docker离线安装简易指南

Docker离线安装简易指南&#xff1a; &#x1f4e6; 下载安装包&#xff1a;浏览器访问Docker官网下载https://download.docker.com/linux/static/stable/x86_64/docker-27.4.1.tgz &#xff0c;并上传服务器 scp docker-27.4.1.tgz root服务器IP:/服务器目录/&#x1f4be; 解…

掌握Docker命令与Dockerfile实战技巧:快速构建高效容器化应用

1. 介绍 Docker 是现代开发和运维的必备工具&#xff0c;集成了容器技术的优势。本文将记录 Docker 的常用指令&#xff0c;并会随着使用经验的积累进行不定期更新。 2. 常用命令 2.1 启动容器&#xff08;前台交互模式&#xff09; docker run --privileged --volume /hom…

npm : 无法加载文件 D:\Nodejs\node_global\npm.ps1,因为在此系统上禁止运行脚本

npm : 无法加载文件 D:\Nodejs\node_global\npm.ps1&#xff0c;因为在此系统上禁止运行脚本 1、问题详情 npm : 无法加载文件 D:\Nodejs\node_global\npm.ps1&#xff0c;因为在此系统上禁止运行脚本&#xff0c;具体如下图所示&#xff1a; 2、解决方法 1.使用命令 get-Ex…

PDF书籍《手写调用链监控APM系统-Java版》第7章 插件与链路的结合:Tomcat插件实现

本人阅读了 Skywalking 的大部分核心代码&#xff0c;也了解了相关的文献&#xff0c;对此深有感悟&#xff0c;特此借助巨人的思想自己手动用JAVA语言实现了一个 “调用链监控APM” 系统。本书采用边讲解实现原理边编写代码的方式&#xff0c;看本书时一定要跟着敲代码。 作者…

Vue 3 具名插槽传值详解

Vue 3 具名插槽传值详解 Vue 3 的具名插槽不仅支持内容插入&#xff0c;还支持通过作用域插槽传递值&#xff08;又称插槽作用域&#xff09;。这使得父组件能够动态接收来自子组件的数据&#xff0c;从而实现更加灵活的组件开发。 在本文中&#xff0c;我们将深入讲解 Vue 3…

《人工智能如何加速药物研发进程:从新药发现到临床试验的突破》

在当今医药领域&#xff0c;药物研发的复杂性和高成本使得新药的推出面临诸多挑战。而人工智能&#xff08;AI&#xff09;正以其强大的能力为药物研发带来新的契机&#xff0c;助力加速新药发现和临床试验过程。 新药发现阶段 靶点识别与筛选 药物研发的第一步是确定药物作…