利用面向对象实现简单的控制台课程管理系统

  1. 本程序主要分为三部分,数据层,控制层和显示层
  2. 程序中需要用到的东西:数组,Scanner类,java中的多态等等
  3. 程序包括6个java文件,Clinte,Course,Dao,Menu,Person,Student,Teacher
  4. Clinte是情景类,Course是课程类,Dao是数据库,用于保存数据,Menu是控制显示类,Student是学生类,Teacher是教师类
package com.pf.student2;

import java.util.Scanner;

public class Clinte {

    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);
        Teacher teacher=new Teacher();//创建教师对象
        Student student=new Student();//创建学生对象
        Dao dao=new Dao();//创建一个数据库
        Menu menu=new Menu();//初始化菜单
        while (true) {
            System.out.println("-------------------------------------------");
            System.out.println(" 欢迎来到学生管理系统");
            System.out.println("请选择角色:");
            System.out.println("1、老师");
            System.out.println("2、学生");
            System.out.println("3、退出");
            switch (scanner.nextInt()) {
            case 1:
                teacher.addDao(dao);
                menu.menu(teacher);
                break;
            case 2:
                student.addDao(dao);
                menu.menu(student);
                break;
            case 3:
                return;
            default:
                System.out.println("输入菜单错误!,请重新输入菜单号。");
                break;
            }
        }
    }
}
package com.pf.student2;

//课程类
public class Course {
    public String name;
    public int id;
    public int time;
    public Course(String name, int id, int time) {
        super();
        this.name = name;
        this.id = id;
        this.time = time;
    }
    public String getName() {
        return name;
    }
    public void setName(String name) {
        this.name = name;
    }
    public int getId() {
        return id;
    }
    public void setId(int id) {
        this.id = id;
    }
    public int getTime() {
        return time;
    }
    public void setTime(int time) {
        this.time = time;
    }
}
package com.pf.student2;

public class Dao {
    public Course storeCourse[]=new Course[100];//创建一个教师书库
    public Course studentCourse[]=new Course[10];//创建一个学生书库
    public int courseNumber=0;//记录学生选了多少个课程
    public int storeNumber=0;//记录书库了的图书的数量
}
package com.pf.student2;

public class Menu {
    public Person person;
    public void menu(Person person){
        person.menu();
    }
}
package com.pf.student2;

public class Person {

    public void add(Course course){
        System.out.println("person的add方法");
    }

    public void delect(){
        System.out.println("person的delect方法");
    }

    public void query(){
        System.out.println("person的query方法");
    }

    public void menu(){
        System.out.println("person菜单");
    }

    public void addDao(Dao dao){

    } 
}
package com.pf.student2;

import java.util.Scanner;

public class Student extends Person {
    Scanner scanner = new Scanner(System.in);
    Dao dao;

    @Override
    public void add(Course course) {
        System.out.println("student的add方法");
    }

    @Override
    public void delect() {
        System.out.println("student的delect方法");
    }

    @Override
    public void query() {
        if (dao.storeNumber != 0) {
            System.out.println("-------------------------------------------");
            System.out.println("课程编号\t\t课程名称\t\t课程课时");
            for (int i = 0; i < dao.storeNumber; i++) {
                System.out.print(dao.storeCourse[i].getId() + "\t\t");
                System.out.print(dao.storeCourse[i].getName() + "\t\t");
                System.out.print(dao.storeCourse[i].getTime() + "\t\t");
                System.out.println();
            }
        }else{
            System.out.println("-------------------------------------------");
            System.out.println("数据为空!");
        }
    }

    public void queryPerson(){
        if (dao.courseNumber != 0) {
            System.out.println("-------------------------------------------");
            System.out.println("课程编号\t\t课程名称\t\t课程课时");
            for (int i = 0; i < dao.courseNumber; i++) {
                System.out.print(dao.studentCourse[i].getId() + "\t\t");
                System.out.print(dao.studentCourse[i].getName() + "\t\t");
                System.out.print(dao.studentCourse[i].getTime() + "\t\t");
                System.out.println();
            }
        }else{
            System.out.println("-------------------------------------------");
            System.out.println("数据为空!");
        }
    }

    @Override
    public void menu() {
        while (true) {
            System.out.println("-------------------------------------------");
            System.out.println("你选择的是老师的角色 请选择菜单:");
            System.out.println("1、查询所有课程");
            System.out.println("2、选择相应课程");
            System.out.println("3、我的选课");
            System.out.println("4、退出角色");
            switch (scanner.nextInt()) {
            case 1:
                query();
                break;
            case 2:
                query();
                System.out.println("请输入相应课程的编号:");
                int id=scanner.nextInt();
                for(int i=0;i
                    if(dao.storeCourse[i].getId()==id){
                        dao.studentCourse[dao.courseNumber]=dao.storeCourse[i];
                        dao.courseNumber++;
                        System.out.println("选课成功!");
                        break;
                    }
                }

                break;
            case 3:
                queryPerson();
                break;
            case 4:
                return;
            default:
                System.out.println("输入菜单错误!,请重新输入菜单号。");
                break;
            }
        }
    }

    // 添加书库
    @Override
    public void addDao(Dao dao) {
        this.dao = dao;
    }
}
package com.pf.student2;

import java.util.Scanner;

public class Teacher extends Person {
    Scanner scanner = new Scanner(System.in);
    Dao dao;

    @Override
    public void add(Course course) {
        dao.storeCourse[dao.storeNumber] = course;
        dao.storeNumber++;
    }

    @Override
    public void delect() {
        query();
        System.out.println("请输入课程的编号:");
        int id=scanner.nextInt();
        int index=0;
        for(int i=0;iif(dao.storeCourse[i].getId()==id){
                index=i;
                for(int j=index+1;j1]=dao.storeCourse[j];
                }
                dao.storeNumber--;
                break;
            }
        }
    }

    @Override
    public void query() {
        if (dao.storeNumber != 0) {
            System.out.println("-------------------------------------------");
            System.out.println("课程编号\t\t课程名称\t\t课程课时");
            for (int i = 0; i < dao.storeNumber; i++) {
                System.out.print(dao.storeCourse[i].getId() + "\t\t");
                System.out.print(dao.storeCourse[i].getName() + "\t\t");
                System.out.print(dao.storeCourse[i].getTime() + "\t\t");
                System.out.println();
            }
        }else{
            System.out.println("-------------------------------------------");
            System.out.println("数据为空!");
        }
    }

    @Override
    public void menu() {
        while (true) {
            System.out.println("-------------------------------------------");
            System.out.println("你选择的老师的角色 选择菜单:");
            System.out.println("1、添加课程");
            System.out.println("2、查询所有课程");
            System.out.println("3、删除课程");
            System.out.println("4、退出角色");
            switch (scanner.nextInt()) {
            case 1:
                System.out.println("请输入课程名称:");
                String name = scanner.next();
                System.out.println("请输入课程的课时:");
                int time = scanner.nextInt();
                Course course = new Course(name, dao.storeNumber + 1, time);
                add(course);
                System.out.println("课程添加完毕");
                break;
            case 2:
                query();
                break;
            case 3:
                delect();
                System.out.println("课程删除完毕");
                break;
            case 4:
                return;
            default:
                System.out.println("输入菜单错误!,请重新输入菜单号。");
                break;
            }
        }
    }

    // 添加书库
    @Override
    public void addDao(Dao dao) {
        this.dao = dao;
    }
}

利用面向对象实现简单的控制台课程管理系统_第1张图片

你可能感兴趣的:(寻知音)