Banner——第三阶段考核——大学生选课管理系统

2023-04-16 14:36:46 浏览数 (2)

初学者未完善 后期完善 顺序:entity–>dao—>servce–>controller util包可以先写先测试,测试类看自身情况

entity包

User 类

代码语言:javascript复制
package entity;

/**
 * @Author: hyy
 * @Date: 2020/12/18 13:11
 */
public class User {
    private Integer id;
    private String userName;
    private String passWord;
    private String question;
    private String answer;
    private Integer level;

    public Integer getId() {
        return id;
    }

    public void setId(Integer id) {
        this.id = id;
    }

    public String getUserName() {
        return userName;
    }

    public void setUserName(String userName) {
        this.userName = userName;
    }

    public String getPassWord() {
        return passWord;
    }

    public void setPassWord(String passWord) {
        this.passWord = passWord;
    }

    public String getQuestion() {
        return question;
    }

    public void setQuestion(String question) {
        this.question = question;
    }

    public String getAnswer() {
        return answer;
    }

    public void setAnswer(String answer) {
        this.answer = answer;
    }

    public Integer getLevel() {
        return level;
    }

    public void setLevel(Integer level) {
        this.level = level;
    }

    /**
     *  空参构造器
     */
    public User() {
    }

    /**
     *  用来用户注册
     * @param id
     */
    public User(Integer id) {
        this.id = id;
    }

    /**
     *  初始化除账号外的参数
     * @param userName
     * @param passWord
     * @param question
     * @param answer
     * @param level
     */
    public User(String userName, String passWord, String question, String answer, Integer level) {
        this.userName = userName;
        this.passWord = passWord;
        this.question = question;
        this.answer = answer;
        this.level = level;
    }

    /**
     *  初始化全部参数的构造器
     * @param id
     * @param userName
     * @param passWord
     * @param question
     * @param answer
     * @param level
     */
    public User(Integer id, String userName, String passWord, String question, String answer, Integer level) {
        this.id = id;
        this.userName = userName;
        this.passWord = passWord;
        this.question = question;
        this.answer = answer;
        this.level = level;
    }

    @Override
    public String toString() {
        return "{"  
                "ID:"   id  
                ", 昵称:"   userName  
                ", 密码:"   passWord  
                ", 密保问题:"   question  
                ", 密保答案:"   answer  
                ", 用户权限:"   level  
                "}n";
    }
}

Course 类

课程类

代码语言:javascript复制
package entity;

/**
 * @Author: hyy
 * @Date: 2020/12/18 21:11
 */
public class Course {
    /**
     *  学生id
     */
    private Integer id;
    /**
     *   课程名称
     */
    private String courseName;
    /**
     * 课程编号
     */
    private String courseId;
    /**
     * 上课教师
     */
    private String courseTeacher;
    /**
     * 课程可选总人数
     */
    private Integer number;

    /**
     *  空参构造器
     */
    public Course() {
    }

    /**
     *  初始化除id外所有参数的构造器
     * @param courseName
     * @param courseId
     * @param courseTeacher
     * @param number
     */
    public Course(String courseName, String courseId, String courseTeacher, Integer number) {
        this.courseName = courseName;
        this.courseId = courseId;
        this.courseTeacher = courseTeacher;
        this.number = number;
    }

    /**
     *  初始化所有参数的构造器
     * @param id
     * @param courseName
     * @param courseId
     * @param courseTeacher
     * @param number
     */
    public Course(Integer id, String courseName, String courseId, String courseTeacher, Integer number) {
        this.id = id;
        this.courseName = courseName;
        this.courseId = courseId;
        this.courseTeacher = courseTeacher;
        this.number = number;
    }

    public Integer getId() {
        return id;
    }

    public void setId(Integer id) {
        this.id = id;
    }

    public String getCourseName() {
        return courseName;
    }

    public void setCourseName(String courseName) {
        this.courseName = courseName;
    }

    public String getCourseId() {
        return courseId;
    }

    public void setCourseId(String courseId) {
        this.courseId = courseId;
    }

    public String getCourseTeacher() {
        return courseTeacher;
    }

    public void setCourseTeacher(String courseTeacher) {
        this.courseTeacher = courseTeacher;
    }

    public Integer getNumber() {
        return number;
    }

    public void setNumber(Integer number) {
        this.number = number;
    }

    @Override
    public String toString() {
        return "{"  
                "ID="   id  
                ", 课程名称::"   courseName  
                ", 课程编号:"   courseId  
                ", 任教老师:"   courseTeacher  
                ", 课程总可选人数:"   number  
                "}n";
    }
}

Dao包

UserDao.java

接口

代码语言:javascript复制
package dao;

import entity.User;

import java.util.List;

/**
 * @Author: hyy
 * @Date: 2020/12/18 13:34
 */
public interface UserDao {
    /**
     *  添加用户
     * @param user
     * @return
     */
    boolean insertUser(User user);

    /**
     *  根据id 删除用户
     * @param id
     * @return
     */
    boolean deleteUserById(Integer id);

    /**
     *  根据id 更改用户
     * @param id
     * @param user
     * @return
     */
    boolean updateUserById(Integer id,User user);

    /**
     *  根据id 查询用户
     * @param id
     * @return
     */
    User selectUserById(Integer id);

    /**
     *  根据用户名 查询用户
     * @param userName
     * @return
     */
    User selectUserByUserName(String userName);

    /**
     *  查询所有用户
      * @return
     */
    List<User> selectAllUser();
}

UserDaoImpl.java

实现类

代码语言:javascript复制
package dao.impl;

import dao.UserDao;
import entity.User;

import java.io.*;
import java.util.ArrayList;
import java.util.List;

/**
 * @Author: hyy
 * @Date: 2020/12/18 13:35
 */
public class UserDaoImpl implements UserDao {
    /**
     *  添加用户
     * @param user
     * @return boolean
     */
    @Override
    public boolean insertUser(User user) {
        try{
            BufferedWriter bufferedWriter=new BufferedWriter(new FileWriter("UniversityCourseSelectionSystem\User.txt",true));
            bufferedWriter.write(user.getId() "," user.getUserName() "," user.getPassWord() "," user.getQuestion() "," user.getAnswer() "," user.getLevel());
            bufferedWriter.newLine();
            bufferedWriter.close();
            return true;
        }catch (IOException e){
            System.out.println("io异常,添加失败!");
            return false;
        }
    }

    /**
     *  根据id 删除用户
     * @param id
     * @return boolean
     */
    @Override
    public boolean deleteUserById(Integer id) {
        try {
            BufferedReader bufferedReader=new BufferedReader(new FileReader("UniversityCourseSelectionSystem\User.txt"));
            List<User> userList=new ArrayList<>();
            String string ;
            while ((string=bufferedReader.readLine())!=null){
                String[]dates= string.split(",");
                User user=new User(Integer.parseInt(dates[0]),dates[1],dates[2],dates[3],dates[4],Integer.parseInt(dates[5]));
                userList.add(user);
            }
            for (int i = 0; i < userList.size(); i  ) {
                User user=userList.get(i);
                if ( (user.getId()).equals(id) ){
                    userList.remove(i);
                }
            }
            BufferedWriter bufferedWriter=new BufferedWriter(new FileWriter("UniversityCourseSelectionSystem\User.txt"));
            for (int i = 0; i < userList.size(); i  ) {
                User user=userList.get(i);
                String returnString =String.format("%s,%s,%s,%s,%s,%s",user.getId(),user.getUserName(),user.getPassWord(),user.getQuestion(),user.getAnswer(),user.getLevel());
                bufferedWriter.write(returnString);
                bufferedWriter.newLine();
            }
            bufferedReader.close();
            bufferedWriter.close();
            return true;
        }catch (IOException e){
            System.out.println("IO异常,删除失败!");
            return false;
        }
    }

    /**
     *  根据id 更改用户
     * @param id
     * @param user
     * @return boolean
     */
    @Override
    public boolean updateUserById(Integer id,User user) {
        try{
            BufferedReader bufferedReader=new BufferedReader(new FileReader("UniversityCourseSelectionSystem\User.txt"));
            List<User> userList=new ArrayList<>();
            String string;
            while ((string=bufferedReader.readLine())!=null){
                String[] dates=string.split(",");
                User user1=new User(Integer.parseInt(dates[0]),dates[1],dates[2],dates[3],dates[4],Integer.parseInt(dates[5]));
                userList.add(user1);
            }
            for (int i = 0; i < userList.size(); i  ) {
                User user1= userList.get(i);
                if (user1.getId().equals(id)){
                    userList.set(i,user);
                }
            }
            BufferedWriter bufferedWriter=new BufferedWriter(new FileWriter("UniversityCourseSelectionSystem\User.txt"));
            for (int i = 0; i < userList.size(); i  ) {
                User user1=userList.get(i);
                String returnString=String.format("%s,%s,%s,%s,%s,%s",user1.getId(),user1.getUserName(),user1.getPassWord(),user1.getQuestion(),user1.getAnswer(),user1.getLevel());
                bufferedWriter.write(returnString);
                bufferedWriter.newLine();
            }
            bufferedReader.close();
            bufferedWriter.close();
            return true;
        }catch (IOException e){
            System.out.println("io异常,更改用户信息失败!");
            return false;
        }
    }

    /**
     *  根据id 查询用户
     * @param id
     * @return User
     */
    @Override
    public User selectUserById(Integer id) {
        try {
            BufferedReader bufferedReader=new BufferedReader(new FileReader("UniversityCourseSelectionSystem\User.txt"));
            List<User> userList=new ArrayList<>();
            String string;
            while ((string=bufferedReader.readLine())!=null){
                String [] dates=string.split(",");
                User user=new User(Integer.parseInt(dates[0]),dates[1],dates[2],dates[3],dates[4],Integer.parseInt(dates[5]));
                userList.add(user);
            }
            for (int i = 0; i < userList.size(); i  ) {
                User user=userList.get(i);
                if (user.getId().equals(id)){
                    return user;
                }
            }
            bufferedReader.close();
        }catch (IOException e){
            System.out.println("IO异常,查询失败!");
            return null;
        }
        return null;
    }

    /**
     *  根据userName 查询用户
     * @param userName
     * @return User
     */
    @Override
    public User selectUserByUserName(String userName) {
        try{
            BufferedReader bufferedReader=new BufferedReader(new FileReader("UniversityCourseSelectionSystem\User.txt"));
            List<User> userList=new ArrayList<>();
            String string;
            while ((string=bufferedReader.readLine())!=null){
                String[] dates=string.split(",");
                User user=new User(Integer.parseInt(dates[0]),dates[1],dates[2],dates[3],dates[4],Integer.parseInt(dates[5]));
                userList.add(user);
            }
            for (int i = 0; i < userList.size(); i  ) {
                User user=userList.get(i);
                if (user.getUserName().equals(userName)){
                    return user;
                }
            }
            bufferedReader.close();
        }catch (IOException e) {
            System.out.println("io异常,查询用户信息失败!");
        }
            return null;
    }

    /**
     *  查询所有用户
     * @return List<User>
     */
    @Override
    public List<User> selectAllUser() {
        try {
            BufferedReader bufferedReader=new BufferedReader(new FileReader("UniversityCourseSelectionSystem\User.txt"));
            List<User> userList=new ArrayList<>();
            String string;
            while ((string=bufferedReader.readLine())!=null){
                String[]dates=string.split(",");
                User user=new User(Integer.parseInt(dates[0]),dates[1],dates[2],dates[3],dates[4],Integer.parseInt(dates[5]));
                userList.add(user);
            }
            bufferedReader.close();
            return userList;
        }catch (IOException e){
            System.out.println("io异常,查询用户信息失败!");
        }
        return null;
    }
}

CourseDao.java

接口

代码语言:javascript复制
package dao;

import entity.Course;

import java.util.List;

/**
 * @Author: hyy
 * @Date: 2020/12/18 21:22
 */
public interface CourseDao {
    /**
     *  增加课程
     * @param course
     * @return
     */
    boolean insertCourse(Course course);

    /**
     *  根据id和课程编号 删除对应id里的课程
     * @param id
     * @param courseId
     * @return
     */
    boolean deleteCourseByCourseId(Integer id,String courseId);

    /**
     *  根据id和课程编号 更改对应id里的课程
     * @param id
     * @param courseId
     * @param course
     * @return
     */
    boolean updateCourseById(Integer id,String courseId,Course course);

    /**
     *  根据id 查询该id所选的课程
     * @param id
     * @return
     */
    List<Course> selectCourseById(Integer id);

    /**
     *  查询所有id 所选的课程
     * @return
     */
    List<Course> selectAllCourse();
}

CourseDaoImpl.java

实现类

代码语言:javascript复制
package dao.impl;

import dao.CourseDao;
import entity.Course;

import java.io.*;
import java.util.ArrayList;
import java.util.List;

/**
 * @Author: hyy
 * @Date: 2020/12/18 21:27
 */
public class CourseDaoImpl implements CourseDao {
//    CourseDaoImpl courseDao=new CourseDaoImpl(); 这样调回栈溢出异常

    /**
     *  添加课程
     * @param course
     * @return boolean
     */
    @Override
    public boolean insertCourse(Course course) {
        try {
            BufferedWriter bufferedWriter=new BufferedWriter(new FileWriter("UniversityCourseSelectionSystem\Course.txt",true));
            bufferedWriter.write(course.getId() "," course.getCourseName() "," course.getCourseId() "," course.getCourseTeacher() "," course.getNumber());
            bufferedWriter.newLine();
            bufferedWriter.close();
            return true;
        }catch (IOException e){
            System.out.println("IO异常,添加失败!");
            return false;
        }
    }

    /**
     *  根据用户id和课程编号 删除用户课程里的该课程
     * @param id
     * @param courseId
     * @return boolean
     */
    @Override
    public boolean deleteCourseByCourseId(Integer id,String courseId) {
        try {
//            List<Course> courseList1=courseDao.selectCourseById(id);

            BufferedReader bufferedReader=new BufferedReader(new FileReader("UniversityCourseSelectionSystem\Course.txt"));
            List<Course> courseList=new ArrayList<>();
            String string;
            while ((string=bufferedReader.readLine())!=null){
                String[] dates=string.split(",");
                Course course=new Course(Integer.parseInt(dates[0]),dates[1],dates[2],dates[3],Integer.parseInt(dates[4]));
                courseList.add(course);
            }
            for (int i = 0; i < courseList.size(); i  ) {
                Course course=courseList.get(i);
                if (course.getId().equals(id)){
                    if (course.getCourseId().equals(courseId)){
                        courseList.remove(i);
                    }else {
                        System.out.println("该用户里不存在该课程,删除失败!");
                    }
                }else{
                    System.out.println("查无此id 删除失败!");
                }
            }
            BufferedWriter bufferedWriter=new BufferedWriter(new FileWriter("UniversityCourseSelectionSystem\Course.txt"));
            for (int i = 0; i < courseList.size(); i  ) {
                Course course=courseList.get(i);
                String returnString=String.format("%s,%s,%s,%s,%s,",course.getId(),course.getCourseName(),course.getCourseId(),course.getCourseTeacher(),course.getNumber());
                bufferedWriter.write(returnString);
                bufferedWriter.newLine();
            }
//           byfferedReader.close();
            bufferedWriter.close();
            return true;
        }catch (IOException e){
            System.out.println("IO异常,删除失败!");
            return false;
        }
    }

    /**
     *  根据id和课程编号 将id对应用户里的该课程更改成course
     * @param id
     * @param courseId
     * @param course
     * @return boolean
     */
    @Override
    public boolean updateCourseById(Integer id,String courseId, Course course) {
       try {
//           List<Course> courseList=courseDao.selectCourseById(id);
           BufferedReader bufferedReader=new BufferedReader(new FileReader("UniversityCourseSelectionSystem\Course.txt"));
           List<Course> courseList=new ArrayList<>();
           String string;
           while ((string=bufferedReader.readLine())!=null){
               String[] dates=string.split(",");
               Course course1=new Course(Integer.parseInt(dates[0]),dates[1],dates[2],dates[3],Integer.parseInt(dates[4]));
               courseList.add(course1);
           }
           for (int i = 0; i < courseList.size(); i  ) {
               Course course1=courseList.get(i);
               if (course1.getId().equals(id)){
                   if (course1.getCourseId().equals(courseId)){
                       courseList.set(i,course);
                   }
               }
           }
           BufferedWriter bufferedWriter=new BufferedWriter(new FileWriter("UniversityCourseSelectionSystem\Course.txt"));
           for (int i = 0; i < courseList.size(); i  ) {
               Course course1=courseList.get(i);
               String returnString=String.format("%s,%s,%s,%s,%s,",course1.getId(),course1.getCourseName(),course1.getCourseId(),course1.getCourseTeacher(),course1.getNumber());
               bufferedWriter.write(returnString);
               bufferedWriter.newLine();
           }
//           bufferedReader.close();
           bufferedWriter.close();
           return true;
       }catch (IOException e){
           System.out.println("IO异常,更改失败!");
           return false;
       }
    }

    /**
     *  根据id 查询id对应用户所选的课程
     * @param id
     * @return List<Course>
     */
    @Override
    public List<Course> selectCourseById(Integer id) {
        try {
            BufferedReader bufferedReader = new BufferedReader(new FileReader("UniversityCourseSelectionSystem\Course.txt"));
            List<Course> courseList = new ArrayList<>();
            List<Course> courseList1=new ArrayList<>();
            String string;
            while ((string = bufferedReader.readLine()) != null) {
                String[] dates = string.split(",");
                Course course1 = new Course(Integer.parseInt(dates[0]), dates[1], dates[2], dates[3], Integer.parseInt(dates[4]));
                courseList.add(course1);
            }
            for (int i = 0; i < courseList.size(); i  ) {
                Course course1 = courseList.get(i);
                if (course1.getId().equals(id)) {
                    courseList1.add(course1);
                }
            }
            bufferedReader.close();
            return courseList1;
        }catch (IOException e){
            System.out.println("IO异常,查询失败!");
            return null;
        }
    }

    /**
     *  查询所有用户选的课程
     * @return List<Course>
     */
    @Override
    public List<Course> selectAllCourse() {
        try {
            BufferedReader bufferedReader = new BufferedReader(new FileReader("UniversityCourseSelectionSystem\Course.txt"));
            List<Course> courseList = new ArrayList<>();
            String string;
            while ((string = bufferedReader.readLine()) != null) {
                String[] dates = string.split(",");
                Course course = new Course(Integer.parseInt(dates[0]), dates[1], dates[2], dates[3], Integer.parseInt(dates[4]));
                courseList.add(course);
            }
            bufferedReader.close();
            return courseList;
        }catch (IOException e){
            System.out.println("IO异常,查询失败!");
            return null;
        }
    }
}

Service包

UserService.java

接口

代码语言:javascript复制
package setvice;

import entity.User;

import java.util.List;

/**
 * @Author: hyy
 * @Date: 2020/12/18 14:23
 */
public interface UserService {
    /**
     *  根据id和密码登录普通用户
     * @param id
     * @param passWord
     * @return boolean
     */
    boolean ordinaryUserLogin(Integer id,String passWord);

    /**
     *  根据id和密码登录管理员
     * @param id
     * @param passWord
     * @return boolean
     */
    boolean administratorLogin(Integer id,String passWord);

    /**
     *  注册用户
     *  学生和教师的注册分离在UserView里
     * @param user
     * @return boolean
     */
    boolean registerUser(User user);

    /**
     *  添加用户
     * @param user
     * @return boolean
     */
    boolean insertUser(User user);

    /**
     *  根据id删除用户
     * @param id
     * @return boolean
     */
    boolean deleteUserById(Integer id);

    /**
     *  根据id 更改用户信息
     * @param id
     * @param user
     * @return boolean
     */
    boolean updateUserById(Integer id,User user);

    /**
     *  根据id查询用户
     * @param id
     * @return User
     */
    User selectUserById(Integer id);

    /**
     *  根据用户名查询用户
     * @param userName
     * @return User
     */
    User selectUserByUserName(String userName);

    /**、
     *  查询所有用户
     * @return List<User>
     */
    List<User> selectAllUser();
}

UserServiceImpl.java

实现类

代码语言:javascript复制
package setvice.impl;

import dao.UserDao;
import dao.impl.UserDaoImpl;
import entity.User;
import setvice.UserService;

import java.util.List;

/**
 * @Author: hyy
 * @Date: 2020/12/18 14:23
 */
public class UserServiceImpl implements UserService {
    UserDao UserDao=new UserDaoImpl();

    /**
     *  普通用户登录(老师和学生)
     * @param id
     * @param passWord
     * @return boolean
     */
    @Override
    public boolean ordinaryUserLogin(Integer id, String passWord) {
        User user=UserDao.selectUserById(id);
        if (user!=null&&(user.getLevel()==1||user.getLevel()==2)&&user.getId().equals(id)&&user.getPassWord().equals(passWord)){
            System.out.println("登录成功!");
            return true;
        }
        else {
            System.out.println("登录失败,id不存在或密码输入错误!");
            return false;
        }
    }

    /**
     *  管理员登录
     * @param id
     * @param passWord
     * @return boolean
     */
    @Override
    public boolean administratorLogin(Integer id, String passWord) {
        User user=UserDao.selectUserById(id);
        if (user!=null&&user.getLevel()==3&&user.getId().equals(id)&&user.getPassWord().equals(passWord)){
            System.out.println("登录成功!");
            return true;
        }
        else {
            System.out.println("登录失败,id不存在或密码输入错误!");
            return false;
        }
    }

    /**
     *   注册用户
     * @param user
     * @return boolean
     */
    @Override
    public boolean registerUser(User user) {
        User user1=UserDao.selectUserById(user.getId());
        if (user1==null){
            UserDao.insertUser(user);
            System.out.println("恭喜注册成功!");
            return true;
        } else{
            System.out.println("用户已存在,注册失败!");
            return false;
        }
    }

    /**
     *  添加用户
     * @param user
     * @return
     */
    @Override
    public boolean insertUser(User user) {
            User user1=UserDao.selectUserById(user.getId());
            if (user1==null){
                UserDao.insertUser(user);
                System.out.println("添加成功!");
                return true;
            }else {
                System.out.println("用户已存在,添加失败!");
                return false;
            }
    }

    /**
     *  根据id 删除用户
     * @param id
     * @return
     */
    @Override
    public boolean deleteUserById(Integer id) {
        User user=UserDao.selectUserById(id);
        if (user!=null){
            UserDao.deleteUserById(id);
            System.out.println("删除成功!");
            return true;
        }else {
            System.out.println("用户不存在,删除失败!");
            return false;
        }
    }

    /**
     *  根据id 更改用户
     * @param id
     * @param user
     * @return
     */
    @Override
    public boolean updateUserById(Integer id,User user) {
        User user1=UserDao.selectUserById(id);
        if (user1!=null){
            UserDao.updateUserById(id,user);
            System.out.println("更改成功!");
            return true;
        }else {
            System.out.println("用户不存在,更改失败!");
            return false;
        }
    }

    /**
     *  根据id 查询用户
     * @param id
     * @return
     */
    @Override
    public User selectUserById(Integer id) {
        User user=UserDao.selectUserById(id);
        if (user!=null){
            return user;
        }else {
            System.out.println("用户不存在");
            return null;
        }
    }

    /**
     *  根据用户名查询用户
     * @param userName
     * @return
     */
    @Override
    public User selectUserByUserName(String userName) {
        User user=UserDao.selectUserByUserName(userName);
        if (user!=null){
            return user;
        }else {
            System.out.println("用户不存在,查询失败!");
            return null;
        }
    }

    /**
     *  查询所有用户
     * @return
     */
    @Override
    public List<User> selectAllUser() {
        List<User> userList=UserDao.selectAllUser();
        if (userList!=null){
            return userList;
        }else{
            System.out.println("无用户!");
            return null;
        }
    }
}

CourseService.java

接口

代码语言:javascript复制
package setvice;

import entity.Course;

import java.util.List;

/**
 * @Author: hyy
 * @Date: 2020/12/18 22:06
 */
public interface CourseService {
    /**
     *  添加课程 即 选课
     * @param course
     * @return boolean
     */
    boolean insertCourseService(Course course);

    /**
     *  根据id和课程编号 删除id对应用户里的该课程
     * @param id
     * @param courseId
     * @return boolean
     */
    boolean deleteCourseByCourseIdService(Integer id,String courseId);

    /**
     * 根据id和课程编号 更改id对应用户里的该课程
     * @param id
     * @param courseId
     * @param course
     * @return boolean
     */
    boolean updateCourseByIdService(Integer id,String courseId,Course course);

    /**
     *  根据id 查询id对应用户所选全部课程
     * @param id
     * @return List<Course>
     */
    List<Course> selectCourseByIdService(Integer id);

    /**
     *  查询所有用户对应的课程
     * @return List<Course>
     */
    List<Course> selectAllCourseService();

}

CourseServiceImpl.java

实现类

代码语言:javascript复制
package setvice.impl;

import dao.CourseDao;
import dao.impl.CourseDaoImpl;
import entity.Course;
import entity.User;
import setvice.CourseService;

import java.util.List;

/**
 * @Author: hyy
 * @Date: 2020/12/18 22:24
 */
public class CourseServiceImpl implements CourseService {
    CourseDao courseDao=new CourseDaoImpl();

    /**
     *  添加课程 即 选课
     * @param course
     * @return boolean
     */
    @Override
    public boolean insertCourseService(Course course) {
            courseDao.insertCourse(course);
            System.out.println("添加成功!");
            return true;
    }
    /**
     *  根据id和课程编号 删除id对应用户里的该课程
     * @param id
     * @param courseId
     * @return boolean
     */
    @Override
    public boolean deleteCourseByCourseIdService(Integer id,String courseId) {
        List<Course> courseList=courseDao.selectCourseById(id);
        if (courseList!=null){
            courseDao.deleteCourseByCourseId(id,courseId);
            System.out.println("删除成功!");
            return true;
        }else {
            System.out.println("课程不存在,删除失败!");
            return false;
        }
    }
    /**
     * 根据id和课程编号 更改id对应用户里的该课程
     * @param id
     * @param courseId
     * @param course
     * @return boolean
     */
    @Override
    public boolean updateCourseByIdService(Integer id,String courseId, Course course) {
        List<Course> courseList=courseDao.selectCourseById(id);
//        System.out.println(courseList);
        if (courseList!=null){
            for (int i = 0; i < courseList.size(); i  ) {
                Course course1=courseList.get(i);
//                System.out.println(course1);
                if (course1.getCourseId().equals(courseId)){
                    courseDao.updateCourseById(id,courseId,course);
                    System.out.println("更改成功!");
                }
            }
            return true;
//            courseDao.updateCourseById(id,courseId,course);
//            System.out.println("更改成功!");
//            return true;
        }else {
            System.out.println("课程不存在,更改失败!");
            return false;
        }
//        return false;
    }
    /**
     *  根据id 查询id对应用户所选全部课程
     * @param id
     * @return List<Course>
     */
    @Override
    public List<Course> selectCourseByIdService(Integer id) {
        List<Course> courseList=courseDao.selectCourseById(id);
        if (courseList!=null){
            return courseList;
        }else {
            System.out.println("课程不存在");
            return null;
        }
    }
    /**
     *  查询所有用户对应的课程
     * @return  List<Course>
     */
    @Override
    public List<Course> selectAllCourseService() {
        List<Course> courseList=courseDao.selectAllCourse();
        if (courseList!=null){
            return courseList;
        }else{
            System.out.println("无用户!");
            return null;
        }
    }
}

Contoller包

UserController.java

代码语言:javascript复制
package controller;

import entity.User;
import setvice.UserService;
import setvice.impl.UserServiceImpl;

import java.util.List;

/**
 * @Author: hyy
 * @Date: 2020/12/18 15:23
 */
public class UserController {
    UserService userService=new UserServiceImpl();

    /**
     *  普通用户登录
     * @param id
     * @param passWord
     * @return boolean
     */
    public boolean ordinaryUserLogin(Integer id, String passWord){
        return userService.ordinaryUserLogin(id,passWord);
    }

    /**
     *  管理员登陆
     * @param id
     * @param passWord
     * @return boolean
     */
    public boolean administratorLogin(Integer id, String passWord){
        return userService.administratorLogin(id,passWord);
    }

    /**
     *  注册用户
     * @param user
     * @return boolean
     */
    public boolean registerUser(User user){
        return userService.registerUser(user);
    }

    /**
     *  添加用户
     * @param user
     * @return boolean
     */
    public boolean insertUser(User user){
        return userService.insertUser(user);
    }

    /**
     *  根据id
     * @param id
     * @return boolean
     */
    public boolean deleteUserById(Integer id){
        return userService.deleteUserById(id);
    }

    /**
     *  根据id 更改用户
     * @param id
     * @param user
     * @return boolean
     */
    public boolean updateUserById(Integer id,User user){
        return userService.updateUserById(id,user);
    }

    /**
     *  根据id 查询用户
     * @param id
     * @return User
     */
    public User selectUserById(Integer id){
        return userService.selectUserById(id);
    }

    /**
     *  根据用户名查询用户
     * @param userName
     * @return User
     */
    public User selectUserByUserName(String userName){
        return userService.selectUserByUserName(userName);
    }

    /**
     *  查询所有用户
     * @return List<User>
     */
    public List<User> selectAllUser(){
        return userService.selectAllUser();
    }
}

CourseController.java

代码语言:javascript复制
package controller;

import entity.Course;
import setvice.CourseService;
import setvice.impl.CourseServiceImpl;

import java.util.List;

/**
 * @Author: hyy
 * @Date: 2020/12/18 22:32
 */
public class CourseController {
    CourseService courseService=new CourseServiceImpl();

    /**
     *  添加课程 即 选课
     * @param course
     * @return boolean
     */
    public boolean insertCourseController(Course course){
        return  courseService.insertCourseService(course);
    }
    /**
     *  根据id和课程编号 删除id对应用户里的该课程
     * @param id
     * @param courseId
     * @return boolean
     */
    public boolean deleteCourseByCourseIdController(Integer id,String courseId){
        return courseService.deleteCourseByCourseIdService(id,courseId);
    }
    /**
     * 根据id和课程编号 更改id对应用户里的该课程
     * @param id
     * @param courseId
     * @param course
     * @return boolean
     */
    public boolean updateCourseByIdService(Integer id,String courseId,Course course){
        return courseService.updateCourseByIdService(id,courseId,course);
    }
    /**
     *  根据id 查询id对应用户所选全部课程
     * @param id
     * @return List<Course>
     */
    public List<Course> selectCourseByIdController(Integer id){
        return  courseService.selectCourseByIdService(id);
    }
    /**
     *  查询所有用户对应的课程
     * @return List<Course>
     */
    public List<Course> selectAllCourseService(){
        return courseService.selectAllCourseService();
    }
}

View包

UserView.java

代码语言:javascript复制
package views;

import controller.UserController;
import entity.User;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.List;

/**
 * @Author: hyy
 * @Date: 2020/12/18 15:33
 */
public class UserView {
    BufferedReader bufferedReader=new BufferedReader(new InputStreamReader(System.in));
    protected UserController userController=new UserController();

    /**
     *
     * @return
     */
    public boolean ordinaryStudentLoginView(){
        try {
            System.out.println("欢迎进入学生登录界面");
            System.out.println("请输入id");
            String id=bufferedReader.readLine();
            System.out.println("请输入密码:");
            String password=bufferedReader.readLine();
            return userController.ordinaryUserLogin(Integer.parseInt(id),password);
        }catch (IOException e){
            System.out.println("io异常,登录失败!");
            return false;
        }
    }

    /**
     *
     * @return
     */
    public boolean ordinaryTeacherLoginView(){
        try {
            System.out.println("欢迎进入教师登录界面");
            System.out.println("请输入id");
            String id=bufferedReader.readLine();
            System.out.println("请输入密码:");
            String password=bufferedReader.readLine();
            return userController.ordinaryUserLogin(Integer.parseInt(id),password);
        }catch (IOException e){
            System.out.println("io异常,登录失败!");
            return false;
        }
    }

    /**
     *
     * @return
     */
    public boolean administratorLoginView(){
        try {
            System.out.println("欢迎进入管理员登录界面");
            System.out.println("请输入id");
            String id=bufferedReader.readLine();
            System.out.println("请输入密码:");
            String password=bufferedReader.readLine();
            return userController.administratorLogin(Integer.parseInt(id),password);
        }catch (IOException e){
            System.out.println("io异常,登录失败!");
            return false;
        }
    }

    /**
     * 学生注册页面
     * (权限绑定为1)
     * @return boolean
     */
    public boolean registerStudentView(){
        try {
            System.out.println("欢迎进入注册界面");
            System.out.println("请输入您想要的id:");
            String id=bufferedReader.readLine();
            User user=userController.selectUserById(Integer.parseInt(id));
            if (user==null){
                System.out.println("请输入您的用户名:");
                String userName=bufferedReader.readLine();
                System.out.println("请输入您的密码");
                String passWord=bufferedReader.readLine();
                System.out.println("请输入您的密保问题:");
                String question=bufferedReader.readLine();
                System.out.println("请输入您的密保答案:");
                String answer=bufferedReader.readLine();
                User user1=new User(Integer.parseInt(id),userName,passWord,question,answer,1);
                System.out.println(user1);
                return userController.registerUser(user1);
            }
        }catch (IOException e){
            System.out.println("io异常,登录失败!");
            return false;
        }
        return false;
    }

    /**
     * 教师注册页面
     * 权限绑定为2
     * @return boolean
     */
    public boolean registerTeacherView(){
        try {
            System.out.println("欢迎进入注册界面");
            System.out.println("请输入您想要的id:");
            String id=bufferedReader.readLine();
            User user=userController.selectUserById(Integer.parseInt(id));
            if (user==null){
                System.out.println("请输入您的用户名:");
                String userName=bufferedReader.readLine();
                System.out.println("请输入您的密码");
                String passWord=bufferedReader.readLine();
                System.out.println("请输入您的密保问题:");
                String question=bufferedReader.readLine();
                System.out.println("请输入您的密保答案:");
                String answer=bufferedReader.readLine();
                User user1=new User(Integer.parseInt(id),userName,passWord,question,answer,2);
                System.out.println(user1);
                return userController.registerUser(user1);
            } else{
                System.out.println("该id已存在,注册失败!");
                return false;
            }
        }catch (IOException e){
            System.out.println("io异常,登录失败!");
            return false;
        }
    }

    /**
     * 添加用户页面
     * @return boolean
     */
    public boolean insertUserView(){
        try {
            System.out.println("欢迎进入增加用户界面");
            System.out.println("请输入想要添加的用户id");
            String id=bufferedReader.readLine();
            User user=userController.selectUserById(Integer.parseInt(id));
            if (user==null){
                System.out.println("请输入想要添加的用户名:");
                String userName=bufferedReader.readLine();
                System.out.println("请输入密码:");
                String passWord=bufferedReader.readLine();
                System.out.println("请输入用户权限:");
                String level=bufferedReader.readLine();
                User user1=new User(Integer.parseInt(id),userName,passWord,null,null,Integer.parseInt(level));
                System.out.println(user1);
                return userController.insertUser(user1);
            }
        }catch (IOException e){
            System.out.println("io异常,添加失败!");
            return false;
        }
        return false;
    }

    /**
     * 删除用户页面
     * @return boolean
     */
    public boolean deleteUserView(){
        try {
            System.out.println("欢迎进入删除用户界面");
            System.out.println("请输入想要删除的用户id");
            String id= null;
            id = bufferedReader.readLine();
            return userController.deleteUserById(Integer.parseInt(id));
        } catch (IOException e) {
            System.out.println("io异常,添加失败!");
            return false;
        }
    }

    /**
     * 更改用户页面
     * @return boolean
     */
    public boolean updateUserView(){
        try {
            System.out.println("欢迎进入更改用户信息界面");
            System.out.println("请输入用户id:");
            String id=bufferedReader.readLine();
            System.out.println("请输入密保答案:");
            String answer=bufferedReader.readLine();
            User user=userController.selectUserById(Integer.parseInt(id));
            if (answer.equals(user.getAnswer())){
                System.out.println("请输入想要修改成的用户名");
                String userName=bufferedReader.readLine();
                System.out.println("请输入想要改成的密码:");
                String password=bufferedReader.readLine();
                User user1=new User(user.getId(),userName,password,user.getQuestion(),user.getAnswer(),user.getLevel());
                return userController.updateUserById(Integer.parseInt(id),user1);
            }
        }catch (IOException e){
            System.out.println("io异常,更改失败!");
            return false;
        }
        return false;
    }

    /**
     * 查询用户页面
     * @return void
     */
    public void selectUserViewO(){
        try {
            System.out.println("********欢迎来到查询页面!********");
            System.out.println("请选择你要选择的查询方式:");
            System.out.println(" 1.查所有,2.根据id查询,3.根据用户名查询 ");
            switch (Integer.parseInt(bufferedReader.readLine())){
                case 1:
                    List<User> userList=userController.selectAllUser();
                    System.out.println("查询的结果是:");
                    System.out.println(userList.toString());
                    break;
                case 2:
                    System.out.println("请输入你想要查询的id");
                    String id=bufferedReader.readLine();
                    User user=userController.selectUserById(Integer.parseInt(id));
                    System.out.println("查询的结果是:");
                    System.out.println(user.toString());
                    break;
                case 3:
                    System.out.println("请输入你想查询的用户名");
                    String userName=bufferedReader.readLine();
                    User user1=userController.selectUserByUserName(userName);
                    System.out.println("查询的结果是:");
                    System.out.println(user1.toString());
                    break;
                default:
                    System.out.println("输入指令错误!");
            }
        }catch (IOException e){
            System.out.println("查询失败!");
        }
    }

}

CourseView.java

代码语言:javascript复制
package views;

import controller.CourseController;
import controller.UserController;
import entity.Course;
import entity.User;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;

/**
 * @Author: hyy
 * @Date: 2020/12/18 22:37
 */
public class CourseView {
    CourseController courseController=new CourseController();
    /**
     *  创建一个用户方向的对象 用来去 调用根据id查询用户方法 和getAnswer
     *   去吧user和课程连接到一起、
     */
    UserController userController=new UserController();
    /**
     *  一般流开了需要调close关掉,但是这种不可以关 !!!
     *  new BufferedReader(new InputStreamReader(System.in));
     *  还有try(){}catch{} 这种try括号里是会自动关闭资源的
     *  所以一般流写里面不用再刻意去关闭
     *  也因此上面的这种代码不可以放到那个括号里
     */
    BufferedReader bufferedReader=new BufferedReader(new InputStreamReader(System.in));

    /**
     *  添加课程页面
     * @return boolean
     */
    public boolean insertCourseView(){
        try {
            System.out.println("—————————欢迎进入添加课程界面————————");
            System.out.println("请输入想要添加的课程的用户id");
            String id=bufferedReader.readLine();
            System.out.println("请输入密保答案:");
            String answer=bufferedReader.readLine();
            User user= userController.selectUserById(Integer.parseInt(id));
            if (user.getAnswer().equals(answer)){
                System.out.println("请输入想要添加的课程名称:");
                String courseName=bufferedReader.readLine();
                System.out.println("请输入课程编号:");
                String courseId=bufferedReader.readLine();
                System.out.println("请输入课程任课老师:");
                String courseTeacher=bufferedReader.readLine();
                System.out.println("请输入课程可选人数:");
                String courseNumber=bufferedReader.readLine();
                Course course1=new Course(Integer.parseInt(id),courseName,courseId,courseTeacher,Integer.parseInt(courseNumber));
                System.out.println(course1);
                return courseController.insertCourseController(course1);
            }
        }catch (IOException e){
            System.out.println("io异常,添加失败!");
            return false;
        }
        return false;
    }

    /**
     *  删除课程页面
     * @return boolean
     */
    public boolean deleteCourseView(){
        try {
            System.out.println("—————————欢迎进入删除课程界面————————");
            System.out.println("请输入想要删除的课程的用户id");
            String id=bufferedReader.readLine();
            System.out.println("请输入想要删除的课程的课程编号:");
            String courseId=bufferedReader.readLine();
            System.out.println("请输入密保答案:");
            String answer=bufferedReader.readLine();
            User user= userController.selectUserById(Integer.parseInt(id));
            if (user.getAnswer().equals(answer)){
                return courseController.deleteCourseByCourseIdController(Integer.parseInt(id),courseId);
            }
        }catch (IOException e){
            System.out.println("io异常,删除失败!");
            return false;
        }
        return false;
    }

    /**
     *  更改课程页面
     * @return boolean
     */
    public boolean updateCourseView(){
        try {
            System.out.println("—————————欢迎进入更改课程界面————————");
            System.out.println("请输入想要更改的课程的用户id");
            String id=bufferedReader.readLine();
            System.out.println("请输入想要更改的课程的课程编号");
            String courseId=bufferedReader.readLine();
            System.out.println("请输入密保答案:");
            String answer=bufferedReader.readLine();
            System.out.println("请输入想要添加的课程名称:");
            String courseName=bufferedReader.readLine();
            System.out.println("请输入课程编号:");
            String courseId1=bufferedReader.readLine();
            System.out.println("请输入课程任课老师:");
            String courseTeacher=bufferedReader.readLine();
            System.out.println("请输入课程可选人数:");
            String courseNumber=bufferedReader.readLine();
            Course course=new Course(Integer.parseInt(id),courseName,courseId1,courseTeacher,Integer.parseInt(courseNumber));
            User user= userController.selectUserById(Integer.parseInt(id));
            if (user.getAnswer().equals(answer)){
                return courseController.updateCourseByIdService(Integer.parseInt(id),courseId,course);
            }
        }catch (IOException e){
            System.out.println("io异常,删除失败!");
            return false;
        }
        return false;
    }

    /**
     *  查询课程页面
     */
    public void selectCourseView(){
        try {
            System.out.println("————————欢迎来到课程查询页面!————————");
            System.out.println("请选择你要选择的查询方式:");
            System.out.println(" 1.查所有人的课程,2.根据id查询该用户课程");
            switch (Integer.parseInt(bufferedReader.readLine())){
                case 1:
                    System.out.println("请输入您的id:");
                    String id1=bufferedReader.readLine();
                    User user=userController.selectUserById(Integer.parseInt(id1));
                    if (user.getLevel()==3||user.getLevel()==2) {
                        System.out.println(courseController.selectAllCourseService());
                    }else {
                        System.out.println("抱歉,您没有权限访问!");
                    }
                    break;
                case 2:
                    System.out.println("请输入你想要查询的id:");
                    String id=bufferedReader.readLine();
                    System.out.println("请输入密保答案:");
                    String answer=bufferedReader.readLine();
                    User user1=userController.selectUserById(Integer.parseInt(id));
                    if (user1.getAnswer().equals(answer)){
                        System.out.println(courseController.selectCourseByIdController(Integer.parseInt(id)));
                    }else {
                        System.out.println("答案错误,查询失败!");
                    }
                    break;
                default:
                    System.out.println("输入指令错误!");
                    break;
            }
        }catch (IOException e){
            System.out.println("io异常,查询失败!");
        }
    }
}

Sepration.java

代码语言:javascript复制
package views;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;

/**
 * @Author: hyy
 * @Date: 2020/12/18 20:17
 *  最终页面
 */

public class SeparationView {
    UserView userView=new UserView();
    CourseView courseView=new CourseView();
    BufferedReader bufferedReader=new BufferedReader(new InputStreamReader(System.in));
    int tool=0;
    boolean judgement=false;
    String a=null;
    public void separation(){
        try {
            System.out.println("————————是否进入学生选课系统?——————————");
            System.out.println("——————————如果进入系统请输入yes,如果要不进入系统输入其他即可————————");
            a=bufferedReader.readLine();
            while (a.equals("yes")){
                System.out.println("欢迎来到学生选课系统!!!");
                System.out.println("——————————请输入你要进入的界面——————————");
                System.out.println("1.学生登录,2.教师登录,3.管理员登录,4.学生注册,5,教师注册");
                try {
                    switch (Integer.parseInt(bufferedReader.readLine())){
                        case 1:
                            judgement=userView.ordinaryStudentLoginView();
                            tool=1;
                            break;
                        case 2:
                            judgement=userView.ordinaryTeacherLoginView();
                            tool=2;
                            break;
                        case 3:
                            judgement=userView.administratorLoginView();
                            tool=3;
                            break;
                        case 4:
                            userView.registerStudentView();
                            judgement=userView.ordinaryStudentLoginView();
                            tool=1;
                            break;
                        case 5:
                            userView.registerTeacherView();
                            judgement=userView.ordinaryTeacherLoginView();
                            tool=2;
                            break;
                        default:
                            System.out.println("输入指令异常!");
                            break;
                    }
                    if (judgement&&tool==3){
                        System.out.println("-------请输入你要进入的界面----------");
                        System.out.println("1.增加用户 2.删除用户 3.更改用户 4.查询用户 5.添加课程 6.删除课程 7.更改课程 8.查询课程");
                        switch (Integer.parseInt(bufferedReader.readLine())){
                            case 1:
                                userView.insertUserView();
                                break;
                            case 2:
                                userView.deleteUserView();
                                break;
                            case 3:
                                userView.updateUserView();
                                break;
                            case 4:
                                userView.selectUserViewO();
                                break;
                            case 5:
                                courseView.insertCourseView();
                                break;
                            case 6:
                                courseView.deleteCourseView();
                                break;
                            case 7:
                                courseView.updateCourseView();
                                break;
                            case 8:
                                courseView.selectCourseView();
                                break;
                            default:
                                System.out.println("输入指令错误!");
                                break;
                        }
                    }else if (judgement&&tool==2){
                        System.out.println("-------请输入你要进入的界面----------");
                        System.out.println("1.更改个人信息 2.查询课程 ");
                        switch (Integer.parseInt(bufferedReader.readLine())){
                            case 1:
                                userView.updateUserView();
                                break;
                            case 2:
                                courseView.selectCourseView();
                                break;
                            default:
                                System.out.println("输入指令错误!");
                                break;
                        }
                    }else if (judgement&&tool==1){
                        System.out.println("-------请输入你要进入的界面----------");
                        System.out.println(" 1.更改个人信息 2.选课 3.退课 4.改课 5.查课");
                        switch (Integer.parseInt(bufferedReader.readLine())){
                            case 1:
                                userView.updateUserView();
                                break;
                            case 2:
                                courseView.insertCourseView();
                                break;
                            case 3:
                                courseView.deleteCourseView();
                                break;
                            case 4:
                                courseView.updateCourseView();
                                break;
                            case 5:
                                courseView.selectCourseView();
                                break;
                            default:
                                System.out.println("输入指令错误!");
                                break;
                        }
                    }
                }catch (IOException e){
                    System.out.println("io异常,进入失败!");
                }
                System.out.println("是否重新进入系统?");
                System.out.println("如果重新进入请输入yes,如果要退出系统输入其他即可");
                a=bufferedReader.readLine();
            }
            System.out.println("欢迎下次进入!");
        }catch (IOException e){
            System.out.println("io异常,进入失败!");
        }
    }
}

Test 测试类

代码语言:javascript复制
import views.SeparationView;

/**
 * @Author: hyy
 * @Date: 2020/12/18 13:07
 */
public class UniversitySelectionCourseSystemTest {
    public static void main(String[] args) {
        SeparationView separationView=new SeparationView();
        separationView.separation();
    }
}

uril包

Create.java

建文件夹及文件

代码语言:javascript复制
package util;

/**
 * @Author: hyy
 * @Date: 2020/12/18 13:08
 */
public class Create {
    public static void main(String[] args) {
        CreateFileName createFileName=new CreateFileName();
        CreatePathName createPathName=new CreatePathName();
//        创建目录
        createPathName.create("UniversityCourseSelectionSystem");
//        创建文件
        createFileName.create("UniversityCourseSelectionSystem\User.txt");
        createFileName.create("UniversityCourseSelectionSystem\Order.txt");
        createFileName.create("UniversityCourseSelectionSystem\Course.txt");
    }
}

CreateFileName

创建文件

代码语言:javascript复制
package util;

import java.io.File;
import java.io.IOException;

/**
 * @Author: hyy
 * @Date: 2020/12/18 13:09
 */
public class CreateFileName {
    /**
     *  创建文件
     * @param Filename
     * @return boolean
     */
    public boolean create(String Filename){
        try {
            File file=new File(Filename);
            file.createNewFile();
            if (file.exists()){
                return false;
            }
            if (file.createNewFile()){
                return true;
            }
            else {
                return false;
            }
        }catch (IOException e){
            return false;
        }
    }

}

CreatePathName.java 创建目录

代码语言:javascript复制
package util;

import java.io.File;

/**
 * @Author: hyy
 * @Date: 2020/12/18 13:08
 */
public class CreatePathName {
    /**
     *  创建目录
     * @param Filename
     * @return
     */
    public boolean create(String Filename){
        File file=new File(Filename);
        if (file.exists()){
            return false;
        }
        if (!Filename.endsWith(File.separator)) {
            Filename = Filename   File.separator;
        }
        if (file.mkdir()) {
            return true;
        } else {
            return false;
        }
    }
}

0 人点赞