Java知识【老师管理系统】 超、凢脫俗 2023-10-02 21:29 1阅读 0赞 **目录** 1:老师管理系统(应用) 1.1:需求说明 1.2:实现步骤 1.3:代码实现 -------------------- ### 1:老师管理系统(应用) ### #### 1.1:需求说明 #### * 添加老师: 通过键盘录入老师信息(id,name,age,birthday) 使用数组存储老师信息,要求老师的id不能重复 * 删除老师: 通过键盘录入要删除老师的id值,将该老师从数组中移除,如果录入的id在数组中不存在,需要重新录入 * 修改老师: 通过键盘录入要修改老师的id值和修改后的老师信息 将数组中该老师的信息修改,如果录入的id在数组中不存在,需要重新录入 * 查询老师: 将数组中存储的所有老师的信息输出到控制台 #### 1.2:实现步骤 #### 环境搭建实现步骤 <table> <thead> <tr> <th>包</th> <th>存储的类</th> <th>作用</th> </tr> </thead> <tbody> <tr> <td>com.czxy.edu.info.manager.domain</td> <td>Student.java Teacher.java</td> <td>封装学生信息 封装老师信息</td> </tr> <tr> <td>com.czxy.edu.info.manager.dao</td> <td>StudentDao.java TeacherDao.java</td> <td>访问存储数据的数组,进行赠删改查(库管)</td> </tr> <tr> <td>com.czxy.edu.info.manager.service</td> <td>StudentService.java TeacherService.java</td> <td>业务的逻辑处理(业务员)</td> </tr> <tr> <td>com.czxy.edu.info.manager.controller</td> <td>StudentController.java TeacherController.java</td> <td>和用户打交道(客服接待)</td> </tr> <tr> <td>com.czxy.edu.info.manager.entry</td> <td>InfoManagerEntry.java</td> <td>程序的入口类,提供一个main方法</td> </tr> </tbody> </table> * 菜单搭建实现步骤 1. 展示欢迎页面,用输出语句完成主界面的编写 2. 获取用户的选择,用Scanner实现键盘录入数据 3. 根据用户的选择执行对应的操作,用switch语句完成操作的选择 * 添加功能实现步骤 ![8b0aa2358cfe498c8f9c470c46292dcf.png][] 查询功能实现步骤 ![d985bf2fe2c6474c99dad4c2b4c346d0.png][] 删除功能实现步骤 ![1acbc0a663e844b9a43176b6ad6a44f8.png][] 修改功能实现步骤 ![2a265620e2734d1d85d8b86d3d0fe7f9.png][] * 系统优化 * 把updateTeacher和deleteTeacherById中录入老师id代码抽取到一个方法(inputTeacherId)中 该方法的主要作用就是录入老师的id,方法的返回值为String类型 * 把addTeacher和updateTeacher中录入老师信息的代码抽取到一个方法(inputTeacherInfo)中 该方法的主要作用就是录入老师的信息,并封装为老师对象,方法的返回值为Teacher类型 #### 1.3:代码实现 #### 老师类 public class Teacher extends Person{ private String id; private String name; private String age; private String birthday; String address; public Teacher() { } public Teacher(String id, String name, String age, String birthday) { this.id = id; this.name = name; this.age = age; this.birthday = birthday; } 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 getAge() { return age; } public void setAge(String age) { this.age = age; } public String getBirthday() { return birthday; } public void setBirthday(String birthday) { this.birthday = birthday; } } TeacherController类 public class TeacherController { private Scanner sc = new Scanner(System.in); private TeacherService teacherService = new TeacherService(); public void start() { teacherLoop: while (true) { System.out.println("--------欢迎来到 <老师> 管理系统--------"); System.out.println("请输入您的选择: 1.添加老师 2.删除老师 3.修改老师 4.查看老师 5.退出"); String choice = sc.next(); switch (choice) { case "1": // System.out.println("添加老师"); addTeacher(); break; case "2": // System.out.println("删除老师"); deleteTeacherById(); break; case "3": // System.out.println("修改老师"); updateTeacher(); break; case "4": // System.out.println("查看老师"); findAllTeacher(); break; case "5": System.out.println("感谢您使用老师管理系统, 再见!"); break teacherLoop; default: System.out.println("您的输入有误, 请重新输入"); break; } } } public void updateTeacher() { String id = inputTeacherId(); Teacher newTeacher = inputTeacherInfo(id); // 调用业务员的修改方法 teacherService.updateTeacher(id,newTeacher); System.out.println("修改成功"); } public void deleteTeacherById() { String id = inputTeacherId(); // 2. 调用业务员中的删除方法, 根据id, 删除老师 teacherService.deleteTeacherById(id); // 3. 提示删除成功 System.out.println("删除成功"); } public void findAllTeacher() { // 1. 从业务员中, 获取老师对象数组 Teacher[] teachers = teacherService.findAllTeacher(); // 2. 判断数组中是否有元素 if (teachers == null) { System.out.println("查无信息, 请添加后重试"); return; } // 3. 遍历数组, 取出元素, 并打印在控制台 System.out.println("学号\t\t姓名\t年龄\t生日"); for (int i = 0; i < teachers.length; i++) { Teacher t = teachers[i]; if (t != null) { System.out.println(t.getId() + "\t" + t.getName() + "\t" + t.getAge() + "\t\t" + t.getBirthday()); } } } public void addTeacher() { String id; while (true) { // 1. 接收不存在的老师id System.out.println("请输入老师id:"); id = sc.next(); // 2. 判断id是否存在 boolean exists = teacherService.isExists(id); if (exists) { System.out.println("id已被占用, 请重新输入:"); } else { break; } } Teacher t = inputTeacherInfo(id); // 5. 将封装好的老师对象, 传递给TeacherService继续完成添加操作 boolean result = teacherService.addTeacher(t); if (result) { System.out.println("添加成功"); } else { System.out.println("添加失败"); } } // 录入老师id public String inputTeacherId(){ String id; while(true){ System.out.println("请输入id"); id = sc.next(); boolean exists = teacherService.isExists(id); if(!exists){ System.out.println("您输入的id不存在, 请重新输入:"); }else{ break; } } return id; } // 录入老师信息, 封装为老师对象 public Teacher inputTeacherInfo(String id){ System.out.println("请输入老师姓名:"); String name = sc.next(); System.out.println("请输入老师年龄:"); String age = sc.next(); System.out.println("请输入老师生日:"); String birthday = sc.next(); Teacher t = new Teacher(); t.setId(id); t.setName(name); t.setAge(age); t.setBirthday(birthday); return t; } } TeacherService类 public class TeacherService { private TeacherDao teacherDao = new TeacherDao(); public boolean addTeacher(Teacher t) { return teacherDao.addTeacher(t); } public boolean isExists(String id) { // 1. 获取库管对象中的数组 Teacher[] teachers = teacherDao.findAllTeacher(); boolean exists = false; // 2. 遍历数组, 取出每一个元素, 进行判断 for (int i = 0; i < teachers.length; i++) { Teacher teacher = teachers[i]; if(teacher != null && teacher.getId().equals(id)){ exists = true; break; } } return exists; } public Teacher[] findAllTeacher() { Teacher[] allTeacher = teacherDao.findAllTeacher(); boolean flag = false; for (int i = 0; i < allTeacher.length; i++) { Teacher t = allTeacher[i]; if(t != null){ flag = true; break; } } if(flag){ return allTeacher; }else{ return null; } } public void deleteTeacherById(String id) { teacherDao.deleteTeacherById(id); } public void updateTeacher(String id, Teacher newTeacher) { teacherDao.updateTeacher(id,newTeacher); } } TeacherDao类 public class TeacherDao { private static Teacher[] teachers = new Teacher[5]; public boolean addTeacher(Teacher t) { int index = -1; for (int i = 0; i < teachers.length; i++) { Teacher teacher = teachers[i]; if(teacher == null){ index = i; break; } } if(index == -1){ return false; }else{ teachers[index] = t; return true; } } public Teacher[] findAllTeacher() { return teachers; } public void deleteTeacherById(String id) { // 1. 查询id在数组中的索引位置 int index = getIndex(id); // 2. 将该索引位置的元素, 使用null进行替换 teachers[index] = null; } public int getIndex(String id){ int index = -1; for (int i = 0; i < teachers.length; i++) { Teacher t = teachers[i]; if(t != null && t.getId().equals(id)){ index = i; break; } } return index; } public void updateTeacher(String id, Teacher newTeacher) { int index = getIndex(id); teachers[index] = newTeacher; } } [8b0aa2358cfe498c8f9c470c46292dcf.png]: https://img-blog.csdnimg.cn/8b0aa2358cfe498c8f9c470c46292dcf.png [d985bf2fe2c6474c99dad4c2b4c346d0.png]: https://img-blog.csdnimg.cn/d985bf2fe2c6474c99dad4c2b4c346d0.png [1acbc0a663e844b9a43176b6ad6a44f8.png]: https://img-blog.csdnimg.cn/1acbc0a663e844b9a43176b6ad6a44f8.png [2a265620e2734d1d85d8b86d3d0fe7f9.png]: https://img-blog.csdnimg.cn/2a265620e2734d1d85d8b86d3d0fe7f9.png
还没有评论,来说两句吧...