1. 程序入口:
entry包(main入口):
public class ManagerEntry {public static void main(String[] args) {Scanner sc = new Scanner(System.in);while (true) {// 主菜单System.out.println("--------信息管理系统--------");System.out.println("1.学生管理 2.老师管理 3.退出");String choice = sc.next();switch (choice) {case "1":OtherStudentController studentController = new OtherStudentController();studentController.start();break;case "2":TeacherController teacherController = new TeacherController();teacherController.start();break;case "3":System.out.println("感谢您的使用");// 退出当前正在运行的JVM虚拟机System.exit(0);break;default:System.out.println("您的输入有误, 请重新输入");break;}}}}
2. 封装信息:
domain包(类的封装):
//父类public class Person {private String id;private String name;private String age;private String birthday;public Person() {}public Person(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;}}// Student类public class Student extends Person {public Student() {}public Student(String id, String name, String age, String birthday) {super(id, name, age, birthday);}}// Teacher类public class Teacher extends Person {public Teacher() {}public Teacher(String id, String name, String age, String birthday) {super(id, name, age, birthday);}}
3. 用户交互:
controller包:
接收信息、打印显示信息,与用户交互。基础学生控制台(父类):
public abstract class BaseStudentController {// 业务员对象private StudentService studentService = new StudentService();private Scanner sc = new Scanner(System.in);// 开启学生管理系统, 并展示学生管理系统菜单public final void start() {//Scanner sc = new Scanner(System.in);studentLoop:while (true) {System.out.println("--------欢迎来到 <学生> 管理系统--------");System.out.println("1.添加学生 2.删除学生 3.修改学生 4.查看学生 5.退出");String choice = sc.next();switch (choice) {case "1":// System.out.println("添加");addStudent();break;case "2":// System.out.println("删除");deleteStudentById();break;case "3":// System.out.println("修改");updateStudent();break;case "4":// System.out.println("查询");findAllStudent();break;case "5":System.out.println("感谢您使用学生管理系统, 再见!");break studentLoop;default:System.out.println("您的输入有误, 请重新输入");break;}}}// 修改学生方法public final void updateStudent() {String updateId = inputStudentId();Student newStu = inputStudentInfo(updateId);studentService.updateStudent(updateId, newStu);System.out.println("修改成功!");}// 删除学生方法public final void deleteStudentById() {String delId = inputStudentId();// 3. 调用业务员中的deleteStudentById根据id, 删除学生studentService.deleteStudentById(delId);// 4. 提示删除成功System.out.println("删除成功!");}// 查看学生方法public final void findAllStudent() {// 1. 调用业务员中的获取方法, 得到学生的对象数组Student[] stus = studentService.findAllStudent();// 2. 判断数组的内存地址, 是否为nullif (stus == null) {System.out.println("查无信息, 请添加后重试");return;}// 3. 遍历数组, 获取学生信息并打印在控制台System.out.println("学号\t\t姓名\t年龄\t生日");for (int i = 0; i < stus.length; i++) {Student stu = stus[i];if (stu != null) {System.out.println(stu.getId() + "\t" + stu.getName() + "\t" + stu.getAge() + "\t\t" + stu.getBirthday());}}}// 添加学生方法public final void addStudent() {// StudentService studentService = new StudentService();// 1. 键盘接收学生信息String id;while (true) {System.out.println("请输入学生id:");id = sc.next();boolean flag = studentService.isExists(id);if (flag) {System.out.println("学号已被占用, 请重新输入");} else {break;}}Student stu = inputStudentInfo(id);// 3. 将学生对象,传递给StudentService(业务员)中的addStudent方法boolean result = studentService.addStudent(stu);// 4. 根据返回的boolean类型结果, 在控制台打印成功\失败if (result) {System.out.println("添加成功");} else {System.out.println("添加失败");}}// 键盘录入学生idpublic String inputStudentId() {String id;while (true) {System.out.println("请输入学生id:");id = sc.next();boolean exists = studentService.isExists(id);if (!exists) {System.out.println("您输入的id不存在, 请重新输入:");} else {break;}}return id;}// 键盘录入学生信息// 开闭原则: 对扩展内容开放, 对修改内容关闭public abstract Student inputStudentInfo(String id);}
学生控制台:
接受信息,使用set方法赋值。public class StudentController extends BaseStudentController {private Scanner sc = new Scanner(System.in);// 键盘录入学生信息// 开闭原则: 对扩展内容开放, 对修改内容关闭@Overridepublic Student inputStudentInfo(String id) {System.out.println("请输入学生姓名:");String name = sc.next();System.out.println("请输入学生年龄:");String age = sc.next();System.out.println("请输入学生生日:");String birthday = sc.next();Student stu = new Student();stu.setId(id);stu.setName(name);stu.setAge(age);stu.setBirthday(birthday);return stu;}}
开闭原则,进行修改:
接受信息使用构造方法赋值。public class OtherStudentController extends BaseStudentController {private Scanner sc = new Scanner(System.in);// 键盘录入学生信息// 开闭原则: 对扩展内容开放, 对修改内容关闭@Overridepublic Student inputStudentInfo(String id) {System.out.println("请输入学生姓名:");String name = sc.next();System.out.println("请输入学生年龄:");String age = sc.next();System.out.println("请输入学生生日:");String birthday = sc.next();Student stu = new Student(id,name,age,birthday);return stu;}}
教师控制台:
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. 接收不存在的老师idSystem.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("添加失败");}}// 录入老师idpublic 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;}}
4. 逻辑处理:
service包:
学生业务逻辑层:
public class StudentService {// 创建StudentDao (库管)// private OtherStudentDao studentDao = new OtherStudentDao();// 通过学生库管工厂类, 获取库管对象private BaseStudentDao studentDao = StudentDaoFactory.getStudentDao();// 添加学生方法public boolean addStudent(Student stu) {// 2. 将学生对象, 传递给StudentDao 库管中的addStudent方法// 3. 将返回的boolean类型结果, 返还给StudentControllerreturn studentDao.addStudent(stu);}// 判断学号是否存在方法public boolean isExists(String id) {Student[] stus = studentDao.findAllStudent();// 假设id在数组中不存在boolean exists = false;// 遍历数组, 获取每一个学生对象, 准备进行判断for (int i = 0; i < stus.length; i++) {Student student = stus[i];if(student != null && student.getId().equals(id)){exists = true;break;}}return exists;}// 查看学生方法public Student[] findAllStudent() {// 1. 调用库管对象的findAllStudent获取学生对象数组Student[] allStudent = studentDao.findAllStudent();// 2. 判断数组中是否有学生信息 (有: 返回地址, 没有: 返回null)// 思路: 数组中只要存在一个不是null的元素, 那就代表有学生信息boolean flag = false;for (int i = 0; i < allStudent.length; i++) {Student stu = allStudent[i];if(stu != null){flag = true;break;}}if(flag){// 有信息return allStudent;}else{// 没有信息return null;}}public void deleteStudentById(String delId) {studentDao.deleteStudentById(delId);}public void updateStudent(String updateId, Student newStu) {studentDao.updateStudent(updateId, newStu);}}
老师业务逻辑层:
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);}}
5. 数据存储层:
dao包:
接口:
public interface BaseStudentDao {// 添加学生方法public abstract boolean addStudent(Student stu);// 查看学生方法public abstract Student[] findAllStudent();// 删除学生方法public abstract void deleteStudentById(String delId);// 根据id找索引方法public abstract int getIndex(String id);// 修改学生方法public abstract void updateStudent(String updateId, Student newStu);}
秉承开闭原则,进行容器替换:
public class OtherStudentDao implements BaseStudentDao {// 集合容器private static ArrayList<Student> stus = new ArrayList<>();static {Student stu1 = new Student("001","张三","23","2000-00-00");Student stu2 = new Student("002","李四","24","2000-00-00");stus.add(stu1);stus.add(stu2);}// 添加学生方法public boolean addStudent(Student stu) {stus.add(stu);return true;}// 查看学生方法public Student[] findAllStudent() {Student[] students = new Student[stus.size()];for (int i = 0; i < students.length; i++) {students[i] = stus.get(i);}return students;}public void deleteStudentById(String delId) {// 1. 查找id在容器中所在的索引位置int index = getIndex(delId);stus.remove(index);}public int getIndex(String id){int index = -1;for (int i = 0; i < stus.size(); i++) {Student stu = stus.get(i);if(stu != null && stu.getId().equals(id)){index = i;break;}}return index;}public void updateStudent(String updateId, Student newStu) {// 1. 查找updateId, 在容器中的索引位置int index = getIndex(updateId);stus.set(index, newStu);}}
学生存储:
public class StudentDao implements BaseStudentDao {// 创建学生对象数组private static Student[] stus = new Student[5];static {Student stu1 = new Student("001","张三","23","2000-00-00");Student stu2 = new Student("002","李四","24","2000-00-00");stus[0] = stu1;stus[1] = stu2;}// 添加学生方法@Overridepublic boolean addStudent(Student stu) {// 2. 添加学生到数组//2.1 定义变量index为-1,假设数组已经全部存满,没有null的元素int index = -1;//2.2 遍历数组取出每一个元素,判断是否是nullfor (int i = 0; i < stus.length; i++) {Student student = stus[i];if(student == null){index = i;//2.3 如果为null,让index变量记录当前索引位置,并使用break结束循环遍历break;}}// 3. 返回是否添加成功的boolean类型状态if(index == -1){// 装满了return false;}else{// 没有装满, 正常添加, 返回truestus[index] = stu;return true;}}// 查看学生方法public Student[] findAllStudent() {return stus;}public void deleteStudentById(String delId) {// 1. 查找id在容器中所在的索引位置int index = getIndex(delId);// 2. 将该索引位置,使用null元素进行覆盖stus[index] = null;}public int getIndex(String id){int index = -1;for (int i = 0; i < stus.length; i++) {Student stu = stus[i];if(stu != null && stu.getId().equals(id)){index = i;break;}}return index;}public void updateStudent(String updateId, Student newStu) {// 1. 查找updateId, 在容器中的索引位置int index = getIndex(updateId);// 2. 将该索引位置, 使用新的学生对象替换stus[index] = newStu;}}
教师存储:
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;}}
6. 解耦:
factory包:
提供 static 修改的 getStudentDao 方法,该方法用于创建 StudentDao 对象并返回public class StudentDaoFactory {public static BaseStudentDao getStudentDao(){return new OtherStudentDao();}}