分类分包思想----信息管理系统

项目介绍

1.通过数组或者集合来作为储存容器(对比:数据结构课设使用的是链表:通过指针连接结点)

2.使用了继承,抽象类和接口来简化程序
3.使用多态来实现统一化编程,来简化代码

使用分类分包思想

1.各种包中的角色各司其职
control:和用户进行交互:向上调用service中提供的方法
service:业务处理层:处理control提交的任务(service也可以调用其他的serviice提供的方法)
dao:专门用来操作数据库或者数据存储容器:处理sercice提交的需求
2.在mhl中其实也是分类思想,但是mhl中是将entry(程序入口)和control合二为一了,这样显得太臃肿了。而在在这个程序中则是将二者分开了,这样就很清晰了
按照一般习惯我们写代码的时候,习惯从control层往下层dao写。完成了control层然后完成所需求的下一次,以此往复

具体代码

  • 包的结构

    control包
  • BaseStudentControl
package com.itheima.edu.info.manager.controller;

import com.itheima.edu.info.manager.domain.Student;
import com.itheima.edu.info.manager.service.StudentService;

import java.util.Scanner;

//客服接待,和用户打交道
public abstract class BaseStudentController {
private Scanner sc = new Scanner(System.in);
 private StudentService studentService = new StudentService();

    //开启学生管理系统并展示菜单
    public final void start() {

        System.out.println("--------------欢迎来到学生管理系统..............................");

        studentLoop:  while (true) {
            System.out.println("请输入你的选择:1.添加学生 2.删除学生 3.修改学生 4.查看学生 5.退出");
            final String choice = sc.next();
            switch (choice) {
                case "1":
                    addStudent();
                    break;
                case "2":
                    // System.out.println("删除学生");
                    deleteStudentById();
                    break;
                case "3":
                    // System.out.println("修改学生");
                    updateStudent();
                    break;
                case "4":
                    //System.out.println("查看学生");
                    findStudent();
                    break;
                case "5":
                    System.out.println("欢迎使用学生管理系统,再见");

                    break studentLoop;//这里需要跳转到总菜单
                default:
                    System.out.println("输入有误");
            }
        }
    }


    //修改学生
    public final void updateStudent() {
        final String id = inputStudentId();
        //3.录入新的学生信息,并封装成对象
        Student student = inputStudentInfo(id);
        //4.调用studentService中的updateStudent方法修改学生信息,修改学生并提示修改成功
        studentService.updateStudent(id,student);
        System.out.println("修改成功");
    }

    public final void deleteStudentById() {
        final String id = inputStudentId();
        //3.调用业务员中的deleteStudentById方法根据id删除学生
        studentService.deleteStudentById(id);
        //4.提示删除成功
        System.out.println("删除成功");
    }

    //查看学生
    public final void findStudent() {
        //1.调用业务员中的获取方法,得到学生的对象数组
        Student[] students = studentService.findStudent();
        //2.判断数组的内存地址,是否为null
        if (students == null) {
            System.out.println("查无信息,请添加后重试");
            return;
        }
        //3.遍历数组,获取学生信息并打印在控制台上
        System.out.println("学号\t\t姓名\t年龄\t生日");
        for (Student student : students) {
            if (student != null) {
                System.out.println(student.getId() + "\t" + student.getName() + "\t\t" + student.getAge() + "\t\t" + student.getBirthday());
            }
        }
    }

    public final void addStudent() {
        //1.    键盘接收学生信息
        StudentService studentService = new StudentService();

        String id;
        while (true) {
            System.out.println("请输入学生id:");
            id = sc.next();
            //判断id是否重复

            boolean reason = studentService.isExact(id);
            if (!reason) {//如果id没有重复则跳出循环
                break;
            } else {
                System.out.println("id已存在,请重新输入");
            }
        }

        //键盘录入学生信息
        final Student student = inputStudentInfo(id);
        //3.将学生对象传递给StudentService(业务员)中的addStudent方法(调用StudentService中的addStudent方法来实现功能)

        boolean reason = studentService.addStudent(student);
        //4.根据返回的boolean类型结果,在控制台打印成功/失败
        if (reason) {
            System.out.println("添加成功");
        } else {
            System.out.println("添加失败");
        }

    }
    //键盘录入id
    public final String  inputStudentId(){
        String id;
        while (true) {
            System.out.println("请输入学生id:");
            id = sc.next();
            //判断id是否重复

            boolean reason = studentService.isExact(id);
            if (reason) {//如果id重复则跳出循环
                break;
            } else {
                System.out.println("id不存在,请重新输入");
            }
        }
        return id;
    }
    //键盘录入学生信息
    public abstract Student inputStudentInfo(String id);//定义为抽象方法,由子类重写


}
  • OtherStudentControl
package com.itheima.edu.info.manager.controller;

import com.itheima.edu.info.manager.domain.Student;
import com.itheima.edu.info.manager.service.StudentService;

import java.util.Scanner;

//客服接待,和用户打交道
public class OtherStudentController extends BaseStudentController {
    private Scanner sc = new Scanner(System.in);
    //键盘录入学生信息
    @Override
    public Student inputStudentInfo(String id){

        System.out.println("请输入学生姓名:");
        final String name = sc.next();
        System.out.println("请输入学生年龄:");
        final int age = sc.nextInt();
        System.out.println("请输入学生生日:");
        final String birthday = sc.next();
        //2.将学生信息封装成对象
        Student student = new Student(id, name, age, birthday);//对象封装对象进行修改

        return student;
    }
}
  • StudentControl
package com.itheima.edu.info.manager.controller;

import com.itheima.edu.info.manager.domain.Student;
import com.itheima.edu.info.manager.service.StudentService;

import java.util.Scanner;

//客服接待,和用户打交道
public class StudentController extends BaseStudentController {

private Scanner sc = new Scanner(System.in);
    //键盘录入id

    //键盘录入学生信息
    @Override
    public Student inputStudentInfo(String id){

        System.out.println("请输入学生姓名:");
        final String name = sc.next();
        System.out.println("请输入学生年龄:");
        final int age = sc.nextInt();
        System.out.println("请输入学生生日:");
        final String birthday = sc.next();
        //2.将学生信息封装成对象
        Student student = new Student();
        student.setId(id);
        student.setName(name);
        student.setAge(age);
        student.setBirthday(birthday);
        return student;
    }
}
  • TheatherControl
package com.itheima.edu.info.manager.controller;

import com.itheima.edu.info.manager.domain.Student;
import com.itheima.edu.info.manager.domain.Teacher;
import com.itheima.edu.info.manager.service.TeacherService;

import java.util.Scanner;

//客服接待
public class TeacherController {
    private Scanner sc = new Scanner(System.in);
    private TeacherService teacherService = new TeacherService();

    //老师管理的菜单
    public void start() {
        teacherLoop:
        while (true) {
            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("修改老师");
                    updateTeacherById();
                    break;
                case "4":
                    // System.out.println("查看老师");
                    findTeacher();
                    break;
                case "5":
                    System.out.println("欢迎使用老师管理系统,再见");

                    break teacherLoop;//这里需要退出到总菜单
                default:
                    System.out.println("输入有误");
                    break;
            }
        }
    }

    public void updateTeacherById() {//修改学生
        final String id = inputId();
        //2.键盘录入新的学生信息并封装成对象
        Teacher teacher = inputTeacherInfo(id);
        //3.将对象传递给teacherService中的updateTeacher方法修改老师信息
        teacherService.updateTeacherById(id,teacher);//由业务员进行信息的修改
        //4.打印修改成功
        System.out.println("修改成功");
    }

    public void deleteTeacherById() {//通过id删除老师
     String id = inputId();
        //3.调用teacherService中的deleteTeacherById方法根据id删除老师
        teacherService.deleteTeacherById(id);
        //4.提示删除成功
        System.out.println("删除成功");
    }

    private void findTeacher() {//查询老师的信息
        //1.请求teacherService的findTeacher方法返回数组
        Teacher[] teachers = teacherService.findTeacher();
        //2.遍历数组并打印老师信息
        if (teachers != null) {//数组不为空,则开始打印数组
            System.out.println("id\t\t姓名\t年龄\t生日");
            for (Teacher teacher : teachers) {
                if(teacher != null){//判断该元素是否为空位
                    System.out.println("" + teacher.getId() + "\t" + teacher.getName() + "\t" + teacher.getAge() + "\t\t" + teacher.getBirthday());

                }
            }
        } else {
            System.out.println("暂无信息,请添加后重试");
        }
    }

    //实现老师的添加
    void addTeacher() {
        String id = null;
        while (true) {
            //1.键盘接收老师的信息,并将信息打包成对象
            System.out.println("请输入老师id:");
            id = sc.next();
            boolean flag = teacherService.isExact(id);//判断输入的id是否存在
            if (!flag) {
                break;
            } else {
                System.out.println("该id已存在,请重新输入");
            }
        }
        Teacher teacher =  inputTeacherInfo(id);//录入学生信息并封装成对象
        //2.将对象传递给TeacherService的addTeacher方法完成具体的添加操作
        boolean reason = teacherService.addTeacher(teacher);
        //3.打印添加成功或者失败
        System.out.println("添加成功");
    }

//录入学生id(需要id存在)
    public String  inputId() {
        String id;
        while (true) {
            //1.接收客户输入的id
            System.out.println("请输入要删除的id:");
            id = sc.next();
            //2.判断id是否存在
            final boolean flag = teacherService.isExact(id);
            if (flag) {
                break;
            }else{
                System.out.println("该id不存在,请重新输入!");
            }
        }
        return id;
    }
    //键盘录入学生信息并封装成对象
    public Teacher inputTeacherInfo(String id) {
        System.out.println("请输入姓名:");
        String name = sc.next();
        System.out.println("请输入年龄:");
        int age = sc.nextInt();
        System.out.println("请输入生日:");
        String birthday = sc.next();
        Teacher teacher = new Teacher(id, name, age, birthday);
        return teacher;
    }
}

dao包

  • BaseStudentDao
package com.itheima.edu.info.manager.dao;

import com.itheima.edu.info.manager.domain.Student;

import java.util.ArrayList;

//dao包库管,对数据进行操作
public interface BaseStudentDao {
    public abstract boolean addStudent(Student student) ;

    public abstract Student[] findAllStudent() ;

    public abstract void deleteStudentById(String id) ;
    //通过id找到他在数组中的索引
    public abstract int getIndex(String id);

    public abstract void updateStudent(String id, Student student);
}

  • OtherStudentDao
package com.itheima.edu.info.manager.dao;

import com.itheima.edu.info.manager.domain.Student;

import java.util.ArrayList;

//dao包库管,对数据进行操作
public class OtherStudentDao implements BaseStudentDao  {
   //使用集合作为容器
  //private static Student[] students = new Student[5];
    private static ArrayList<Student> students = new ArrayList<>();
   //静态代码块初始化学生数据
    static {
        Student s1 = new Student("001", "张三", 18, "1990-01-01");
        Student s2 = new Student("002", "李四", 23, "1937-03-01");
        students.add(s1);
        students.add(s2);
   }

   @Override
    public boolean addStudent(Student student) {
       //因为集合装不满,所以不需要进行判断,直接添加即可
        students.add(student);
        return true;



    }
@Override
    public Student[] findAllStudent() {
        //1.我们可以选择返回数组或者即可
        //2.返回集合将会修改Service和Controller中的代码,我们选择返回数组
        //3.然后将集合中的元素全部装回数组中
        Student[] stu = new Student[students.size()];//数组的长度和集合长度保持一致
        stu =students.toArray(stu);//将集合变成数组返回
        return stu;

    }
@Override
    public void deleteStudentById(String id) {
        //1.找到该id在数组中对应的索引
        int index = getIndex(id);
        //2.将该索引的对象使用null替代
        students.remove(index);
    }
    //通过id找到他在数组中的索引
    @Override
    public int getIndex(String id){//(改为遍历集合即可)
        int index=-1;//记录id对应索引的的位置
        for (int i = 0; i < students.size(); i++) {
            if(students.get(i)!=null&&id.equals(students.get(i).getId())){
                index=i;
                break;
            }
        }
        return index;
    }
@Override
    public void updateStudent(String id, Student student) {
        //1.查找id在容器中的索引
        int index = getIndex(id);
        //2.将该索引的位置,使用新的学生对象替换
        students.set(index, student);
    }
}
  • StudentDao
package com.itheima.edu.info.manager.dao;

import com.itheima.edu.info.manager.domain.Student;

//dao包库管,对数据进行操作
public class StudentDao    implements BaseStudentDao {
   //创建Student学生数组长度为5
   private static Student[] students = new Student[5];
   //静态代码块初始化学生数据
    static {
        Student s1 = new Student("001", "张三", 18, "1990-01-01");
        Student s2 = new Student("002", "李四", 23, "1937-03-01");
        students[0] = s1;
        students[1] = s2;
   }
   @Override

    public boolean addStudent(Student student) {


        //2.将接收到的学生对象添加到数组中
        //2.1定义遍历index=-1
        int index = -1;
        //2.2遍历数组中的每一个元素,判断是否为null
        for (int i = 0; i < students.length; i++) {
            if (students[i] == null) {
                index = i;
                break;
            }
        }
        //3.返回是否添加成功
      if (index == -1) {
           //装满了
            return false;
        } else {
          //没有装满,正常添加,返回true
          students[index] = student;
            return true;
        }
    }
@Override
    public Student[] findAllStudent() {
        //返回我们创建的对象数组
        return students;
    }
@Override
    public void deleteStudentById(String id) {
        //1.找到该id在数组中对应的索引
        int index = getIndex(id);
        //2.将该索引的对象使用null替代
        students[index] = null;
    }
    //通过id找到他在数组中的索引
    @Override
    public int getIndex(String id){
        int index=-1;//记录id对应索引的的位置
        for (int i = 0; i < students.length; i++) {
            if(students[i]!=null&&id.equals(students[i].getId())){
                index=i;
                break;
            }
        }
        return index;
    }
@Override
    public void updateStudent(String id, Student student) {
        //1.查找id在容器中的索引
        int index = getIndex(id);
        //2.将该索引的位置,使用新的学生对象替换
        students[index] = student;
    }
}
  • TeacherDao
package com.itheima.edu.info.manager.dao;

import com.itheima.edu.info.manager.domain.Teacher;

public class TeacherDao {
    //数据存储容器
    private static Teacher[] teachers = new Teacher[5];
    //静态代码块初始化数据
    static {
        Teacher t1 = new Teacher("001", "张老师", 18, "1970-01-01");
        Teacher t2 = new Teacher("002", "李老师", 23, "1947-03-01");
        teachers[0] = t1;
        teachers[1] = t2;
    }
    public boolean addTeacher(Teacher teacher) {//具体实现添加的工作
        //1.遍历我们的数组,判读是否为null
        int index = -1;//记录数组的下标
        for (int i = 0; i < teachers.length; i++) {
            if(teachers[i]==null){
                index = i;
                break;
            }

        }
        //2.判断是否为null
        if(index==-1){
            //装满了
            return false;
        }
        else{
            //没有装满,正常添加,返回true
            teachers[index] = teacher;
            return true;
        }
    }

    public Teacher[] findAllTeachers() {//直接返回我们的数组
        return teachers;
    }
}

domain

  • Person
package com.itheima.edu.info.manager.domain;

public class Person {
    private String id;
    private String name;
    private int age;
    private String birthday;

    public Person() {
    }

    public Person(String id, String name, int age, String birthday) {
        this.id = id;
        this.name = name;
        this.age = age;
        this.birthday = birthday;
    }

    /**
     * 获取
     * @return id
     */
    public String getId() {
        return id;
    }

    /**
     * 设置
     * @param id
     */
    public void setId(String id) {
        this.id = id;
    }

    /**
     * 获取
     * @return name
     */
    public String getName() {
        return name;
    }

    /**
     * 设置
     * @param name
     */
    public void setName(String name) {
        this.name = name;
    }

    /**
     * 获取
     * @return age
     */
    public int getAge() {
        return age;
    }

    /**
     * 设置
     * @param age
     */
    public void setAge(int age) {
        this.age = age;
    }

    /**
     * 获取
     * @return birthday
     */
    public String getBirthday() {
        return birthday;
    }

    /**
     * 设置
     * @param birthday
     */
    public void setBirthday(String birthday) {
        this.birthday = birthday;
    }

    public String toString() {
        return "Person{id = " + id + ", name = " + name + ", age = " + age + ", birthday = " + birthday + "}";
    }
}
  • Student
package com.itheima.edu.info.manager.domain;
//domain包中的类用于封装数据
public class Student extends Person {


public Student(String id, String name, int age, String birthday) {

    super(id, name, age, birthday);
}

    public Student() {
    }




}
  • Teacher
package com.itheima.edu.info.manager.domain;

public class Teacher extends Person {
    //空参构造
    public Teacher() {

    }
    //有参构造
    public Teacher(String id, String name, int age, String birthday) {
        super(id, name, age, birthday);
    }
}

entry

  • InfoMassageEntry
package com.itheima.edu.info.manager.entry;

import com.itheima.edu.info.manager.controller.OtherStudentController;
import com.itheima.edu.info.manager.controller.StudentController;
import com.itheima.edu.info.manager.controller.TeacherController;

import java.util.Scanner;

//entry包负责程序的入口
public class InfoManagerEntry {
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        System.out.println("--------------欢迎来到黑马信息管理系统------------------");
 while (true) {
            System.out.println("请输入你的选中:1.学生管理 2.老师管理 3.退出");
            final String choice = sc.next();
            switch (choice) {
                case "1":
                  //  System.out.println("学生管理");
                    //StudentController studentController = new StudentController();
                    //studentController.start();
                    OtherStudentController otherStudentController = new OtherStudentController();//换用的逻辑
                    otherStudentController.start();
                    break;
                case "2":
                   // System.out.println("老师管理");
                    TeacherController teacherController = new TeacherController();
                    teacherController.start();
                    break;
                case "3":
                    System.out.println("退出");
                    System.exit(0);//退出当前正在运行的虚拟机

                    break ;
                default:
                    System.out.println("输入有误");
                    break;
            }
        }

    }
}

factroy

  • StudentDaoFactory
package com.itheima.edu.info.manager.factory;

import com.itheima.edu.info.manager.dao.BaseStudentDao;
import com.itheima.edu.info.manager.dao.StudentDao;

//专门用来创建不同的Dao对象
public class StudentDaoFactory {
    //将返回类型定义为功能的父类
    public static BaseStudentDao getStudentDao(){
        return new StudentDao();
    }
}

service

  • StudentService
package com.itheima.edu.info.manager.service;

import com.itheima.edu.info.manager.dao.BaseStudentDao;
import com.itheima.edu.info.manager.dao.OtherStudentDao;
import com.itheima.edu.info.manager.dao.StudentDao;
import com.itheima.edu.info.manager.domain.Student;
import com.itheima.edu.info.manager.factory.StudentDaoFactory;

import java.awt.desktop.OpenFilesHandler;

//service包负责业务逻辑
public class StudentService {
   //将Dao类型改为多态实现
  private BaseStudentDao baseStudentDao =  StudentDaoFactory.getStudentDao();

public boolean addStudent(Student student) {
        //暂时不考虑id是否存在

      boolean reason =  baseStudentDao.addStudent(student);
        //2.接收方法的boolean返回值,将结果返还给StudentController
        return reason;
    }

    public boolean isExact(String id) {//进行id是否重复的业务逻辑判断

        //1.从StudentDao中获取存储学生对象的数组(掉哟StudentDao的findAllStudent方法)
        Student[] students = baseStudentDao.findAllStudent();
        //2.遍历获取数组中的每一个学生,判断学号是否存在
        for (Student student : students) {
            if (student != null &&id.equals(student.getId())) {//用student.getId().equals(id)当数组为空将导致空指针异常
                return true;
            }
        }
        //3.返回true或false
        return false;
    }

    public Student[] findStudent() {//注意:不能通过数组长度判断(因为数组的长度就是表示长度,而集合表示的是集合内数据的个数)
        //1.调用库管的findStudent方法,获取数组
        Student[] students = baseStudentDao.findAllStudent();
        //2.判断获取的数组是否有学生信息(有:返回地址,没有:返回Null)
        boolean flag = false;//默认数组中没有学生
        for (int i = 0; i < students.length; i++) {
          if( students[i] != null) {
              flag = true;
              break;
          }
        }
     if(flag == true) {
         return students;
     }else {
         return null;//没有学生返回null
     }

    }

    public void deleteStudentById(String id) {
    //调用studentDao的deleteStudentById方法删除学生
        baseStudentDao.deleteStudentById(id);
    }

    public void updateStudent(String id,Student student) {
    //调用库管studentService的updateStudent方法
        baseStudentDao.updateStudent(id,student);


    }
}
  • TeacherService
package com.itheima.edu.info.manager.service;

import com.itheima.edu.info.manager.dao.TeacherDao;
import com.itheima.edu.info.manager.domain.Student;
import com.itheima.edu.info.manager.domain.Teacher;

public class TeacherService {
    TeacherDao teacherDao = new TeacherDao();
    public boolean addTeacher(Teacher teacher) {
        //1.将对象传递给TeacherDao的teacherDao的addTeacher方法完成具体的添加操作
        boolean reason = teacherDao.addTeacher(teacher);
        //2.接收TeacherDao的addTeacher方法添加的结果
    return  reason;
    }

    public boolean isExact(String id) {//由teacherService判读id是否重复
        //1.由TeacherDao将整个数组交给TeacherService
         Teacher [] teachers = teacherDao.findAllTeachers();
        //2.判断id是否重复
        for (int i = 0; i < teachers.length; i++) {
            if(teachers[i] != null &&teachers[i].getId().equals(id)){
                return true;
            }
        }
        //3.将结果返回
        return  false;
    }

    public Teacher[] findTeacher() {
        //1..请求TeacherDao的findAllTeacher方法返回老师数组
        Teacher [] teachers = teacherDao.findAllTeachers();
        //2.判断数组是否为null 并返回null或者地址(判断数组是否为空:需要遍历数组判断里面的每一个元素)
        boolean flag = isNull(teachers);

      if(flag){
          return teachers;
      }
      else{
          return null;
      }

    }
    //判断数组是否为空
    public boolean isNull(Teacher[] teachers) {
        boolean flag = false;//开始数组默认为空
        for (int i = 0; i < teachers.length; i++) {
            if(teachers[i] != null){
                flag = true;
                break;
            }
        }
        return flag;

    }
    public void deleteTeacherById(String id) {//具体执行删除老师
        //1.通过teacherDao的findAllTeacher方法返回老师数组
        Teacher [] teachers = teacherDao.findAllTeachers();
        //2.遍历数组找到该id所对应的索引
        int deleteIndex = getIndexById(id,teachers);
        //3.将该元素赋值为null
        teachers[deleteIndex] = null;
    }

    public void updateTeacherById(String id,Teacher teacher) {
        //1.由teacherDao的findAllTeacher方法返回老师数组
        Teacher [] teachers = teacherDao.findAllTeachers();
        //2.遍历数组找到该id所对应的索引
        final int updateIndex = getIndexById(id, teachers);
        //3.给该索引位置的元素重新赋值
        teachers[updateIndex] = teacher;
    }
    //找到具体id元素所对应的索引
    public int getIndexById(String id,Teacher[] teachers) {

        //2.遍历数组找到该id所对应的索引
        int index = -1;
        for (int i = 0; i < teachers.length; i++) {
            if(teachers[i] != null &&teachers[i].getId().equals(id)){
                index = i;
                break;
            }
        }
        return index;
    }

}
posted @ 2023-07-18 07:26  一往而深,  阅读(18)  评论(0编辑  收藏  举报