[ python ] 面向對象 - 選課系統

根據源程序進行改寫:
    原程序地址:http://www.cnblogs.com/lianzhilei/p/5985333.html  若有侵權當即刪除。
    感謝原做者將完整的代碼提供參考。
    原做者的代碼是經過 shelve 模塊實現的,很是完美。具體參考上面的連接地址;如下我改寫代碼是經過 pickle 模塊來實現。主要作爲練習:html

 

READMEpython

 

class_system-pickle		選課系統根目錄
├── bin	
│   ├── __init__.py
│   └── start.py	程序入口
├── conf
│   ├── __init__.py
│   └── settings.py	配置文件 - 主要存儲數據庫文件目錄
├── core
│   ├── __init__.py
│   ├── manage_center.py	主菜單入口
│   ├── school_center.py	學校中心功能點實現
│   ├── student_center.py	學生中心功能點實現
│   └── teacher_center.py	講師中心功能點實現
├── db
│   ├── __init__.py
│   └── school_dict	數據庫文件(運行程序自動初始化生成)
└── modules
    ├── course.py	課程類
    ├── grade.py	班級類
    ├── __init__.py
    ├── school.py	學校類
    ├── student.py	學生類
    ├── teacher.py	講師類
    └── tools.py	工具包 - 對數據庫文件讀寫操做

5 directories, 18 files


代碼實現邏輯請閱讀原做者連接。

 

 基於 pickle 序列化的代碼以下:數據庫

 

binapp

#!/usr/bin/python3
# -*- coding: utf-8 -*-
# Author: hkey
from conf.settings import school_db_file
from core.manage_center import Manage_center

if __name__ == '__main__':
    obj = Manage_center()
    obj.run()
start.py

 

 

confide

#!/usr/bin/python3
# -*- coding: utf-8 -*-
# Author: hkey
import os

BASE_DIR = os.path.dirname(os.path.dirname(os.path.abspath(__file__)))

school_db_file = os.path.join(BASE_DIR, 'db', 'school_dict')
teacher_db_file = os.path.join(BASE_DIR, 'db', 'school_dict')
settings.py

 

 

db工具

(不須要手動建立文件,啓動程序自動生成數據文件)spa

 

core3d

#!/usr/bin/python3
# -*- coding: utf-8 -*-
# Author: hkey
from core.school_center import School_center
from core.student_center import Student_center
from core.teacher_center import Teacher_center


class Manage_center(object):
    def __init__(self):
        pass

    def run(self):
        while True:
            print('1. 學校中心\n'
                  '2. 講師中心\n'
                  '3. 學生中心\n'
                  '4. 退出')
            choice = input('>>>').strip()
            if choice == '1':
                School_center()
            elif choice == '2':
                Teacher_center()
            elif choice == '3':
                Student_center()
            elif choice == '4':
                break
            else:
                print('\033[31;1m輸入錯誤,請從新輸入.\033[0m')
manage_center.py
#!/usr/bin/python3
# -*- coding: utf-8 -*-
# Author: hkey
import os, sys, pickle
from conf.settings import school_db_file
from modules.tools import file_oper
from modules.school import School


class School_center(object):
    def __init__(self):
        if not os.path.exists(school_db_file):
            self.init_school()
            self.school_db = file_oper(school_db_file, 'rb')
            self.manage_run()
        else:
            self.school_db = file_oper(school_db_file, 'rb')
            self.manage_run()

    def init_school(self):
        sh = School('上海', '上海市')
        bj = School('北京', '北京市')
        data = {'上海': sh, '北京': bj}
        file_oper(school_db_file, 'wb', data)

    def manage_run(self):
        for key in self.school_db:
            print('學校名:', key)

        choice_school = input('>>>').strip()
        if choice_school in self.school_db:
            self.choice_school = choice_school
            self.school_obj = self.school_db[choice_school]
            while True:
                print('\n歡迎進入【%s】校區\n'
                      '建立課程 add_course\n'
                      '查看課程 check_course\n'
                      '建立班級 add_grade\n'
                      '查看班級 check_grade\n'
                      '建立講師 add_teacher\n'
                      '查看講師 check_teacher\n'
                      '退出系統 exit' % choice_school)

                user_func = input('>>>').strip()
                if hasattr(self, user_func):
                    getattr(self, user_func)()

    def add_course(self):
        course_name = input('\033[34;1m輸入課程名:\033[0m')
        course_price = input('\033[34;1m輸入課程價格:\033[0m')
        course_time = input('\033[34;1m輸入課程週期:\033[0m')
        if course_name not in self.school_obj.school_course:
            self.school_obj.create_course(course_name, course_price, course_time)
            self.school_db[self.choice_school] = self.school_obj
            file_oper(school_db_file, 'wb', self.school_db)
            print('\033[32;1m課程新增成功。\033[0m')
        else:
            print('\033[31;1m課程信息已存在.\033[0m')
            self.school_obj.create_course(course_name, course_price, course_time)
            print('\033[32;1m課程更新成功。\033[0m')

    def check_course(self):
        self.school_obj.show_course()

    def add_grade(self):
        grade_name = input('\033[34;1m輸入班級名:\033[0m')
        course_name = input('\033[34;1m輸入班級要上的課程名:\033[0m')
        if course_name in self.school_obj.school_course:
            course_obj = self.school_obj.school_course[course_name]
            if grade_name not in self.school_obj.school_grade:
                self.school_obj.create_grade(grade_name, course_obj)
                self.school_db[self.choice_school] = self.school_obj
                file_oper(school_db_file, 'wb', self.school_db)

            else:
                print('\033[31;1m班級信息已存在.\033[0m')
        else:
            print('\033[31;1m課程信息不存在.\033[0m')

    def check_grade(self):
        self.school_obj.show_grade()

    def add_teacher(self):
        teacher_name = input('\033[34;1m輸入講師名:\033[0m')
        teacher_salary = input('\033[34;1m輸入講師薪資:\033[0m')
        grade_name = input('\033[34;1m輸入講師關聯的班級:\033[0m')
        if grade_name in self.school_obj.school_grade:
            grade_obj = self.school_obj.school_grade[grade_name]
            if teacher_name not in self.school_obj.school_teacher:
                self.school_obj.create_teacher(teacher_name, teacher_salary, grade_name, grade_obj)
                print('\033[32;1m講師新增成功.\033[0m')
            else:
                print('\033[31;1m講師信息已存在.\033[0m')
                self.school_obj.create_teacher(teacher_name, teacher_salary, grade_name, grade_obj)
                print('\033[32;1m講師更新成功.\033[0m')
            self.school_db[self.choice_school] = self.school_obj
            file_oper(school_db_file, 'wb', self.school_db)
        else:
            print('\033[31;1m班級信息不存在,請先建立班級.\033[0m')

    def check_teacher(self):
        self.school_obj.show_teacher()

    def exit(self, *args):
        sys.exit('歡迎下次使用選課系統.')
school_center.py
#!/usr/bin/python3
# -*- coding: utf-8 -*-
# Author: hkey
import os, sys, pickle
from conf.settings import school_db_file
from modules.tools import file_oper
from modules.school import School


class Student_center(object):
    def __init__(self):
        if os.path.exists(school_db_file):
            self.school_db = file_oper(school_db_file, 'rb')
            self.manage_run()
        else:
            print('\033[31;1m數據庫文件不存在,請先初始化.\033[0m')

    def manage_run(self):
        for key in self.school_db:
            print('學校名:', key)
        choice_school = input('>>>').strip()
        if choice_school in self.school_db:
            self.choice_school = choice_school
            self.school_obj = self.school_db[choice_school]
            student_name = input('\033[34;1m輸入學生名:\033[0m').strip()
            student_age = input('\033[34;1m輸入學生年齡:\033[0m').strip()
            self.school_obj.show_grade_course()
            grade_name = input('\033[34;1m輸入要選擇的班級名:\033[0m')
            if grade_name in self.school_obj.school_grade:
                self.school_obj.create_student(student_name, student_age, grade_name)
                self.school_db[self.choice_school] = self.school_obj
                file_oper(school_db_file, 'wb', self.school_db)
                print('\033[32;1m選課成功.\033[0m')

            else:
                print('\033[31;1m班級信息不存在.\033[0m')
student_center.py
#!/usr/bin/python3
# -*- coding: utf-8 -*-
# Author: hkey
import os, sys
from modules.tools import file_oper
from conf.settings import school_db_file, teacher_db_file


class Teacher_center(object):
    def __init__(self):
        if os.path.exists(school_db_file):
            self.school_db = file_oper(school_db_file, 'rb')
            self.manage_run()
        else:
            print('\033[31;1m數據庫不存在,請初始化數據庫.')
            exit(1)
        # if not os.path.exists(teacher_db_file):
        #     self.init_teacher()
        #     self.manage_run()
        # else:
        #     self.teacher_db = file_oper(teacher_db_file, 'rb')
        #     self.manage_run()

    def init_teacher(self):
        data = {}
        file_oper(teacher_db_file, 'wb', data)

    def manage_run(self):
        for key in self.school_db:
            print('學校名:', key)
        choice_school = input('\033[34;1m輸入學校名:\033[0m')
        if choice_school in self.school_db:
            self.choice_school = choice_school
            self.school_obj = self.school_db[choice_school]
            teacher_name = input('\033[34;1m輸入講師名:\033[0m')
            if teacher_name in self.school_obj.school_teacher:
                while True:
                    print('\n歡迎進入講師中心\n'
                          '查看班級信息 check_grade\n'
                          '退出 exit')

                    user_func = input('>>>').strip()
                    if hasattr(self, user_func):
                        getattr(self, user_func)(teacher_name)

    def check_grade(self, teacher_name):
        self.school_obj.show_teacher_grade(teacher_name)

    def exit(self, *args):
        sys.exit('歡迎下次使用選課系統.')
teacher_center.py

 

 

 

modulescode

#!/usr/bin/python3
# -*- coding: utf-8 -*-
# Author: hkey


class Course(object):
    def __init__(self, course_name, course_price, course_time):
        self.course_name = course_name
        self.course_price = course_price
        self.course_time = course_time
course.py
#!/usr/bin/python3
# -*- coding: utf-8 -*-
# Author: hkey


class Grade(object):
    def __init__(self, grade_name, course_obj):
        self.grade_name = grade_name
        self.course_obj = course_obj
        self.grade_student = {}
grade.py
#!/usr/bin/python3
# -*- coding: utf-8 -*-
# Author: hkey
import pickle
from modules.course import Course
from modules.grade import Grade
from modules.teacher import Teacher
from modules.student import Student


class School(object):
    def __init__(self, school_name, school_addr):
        self.school_name = school_name
        self.school_addr = school_addr
        self.school_course = {}
        self.school_grade = {}
        self.school_teacher = {}

    def create_course(self, course_name, course_price, course_time):
        course_obj = Course(course_name, course_price, course_time)
        self.school_course[course_name] = course_obj

    def show_course(self):
        for key in self.school_course:
            course_obj = self.school_course[key]
            print('\033[32;1m課程【%s】\t價格【%s元】\t週期【%s個月】\033[0m' % (
                course_obj.course_name, course_obj.course_price, course_obj.course_time))

    def create_grade(self, grade_name, course_obj):
        grade_obj = Grade(grade_name, course_obj)
        self.school_grade[grade_name] = grade_obj

    def show_grade(self):
        for key in self.school_grade:
            grade_obj = self.school_grade[key]
            print('\033[32;1m班級名【%s】\t課程【%s】\033[0m' % (grade_obj.grade_name, grade_obj.course_obj.course_name))

    def create_teacher(self, teacher_name, teacher_salary, grade_name, grade_obj):
        teacher_obj = Teacher(teacher_name, teacher_salary)
        teacher_obj.teacher_add_grade(grade_name, grade_obj)
        self.school_teacher[teacher_name] = teacher_obj

    def show_teacher(self):
        for key in self.school_teacher:
            teacher_obj = self.school_teacher[key]
            grade_list = []
            for key in teacher_obj.teacher_grade:
                grade_list.append(key)
            print('\033[32;1m講師【%s】\t薪資【%s】\t班級列表【%s】\033[0m' % (
                teacher_obj.teacher_name, teacher_obj.teacher_salary, grade_list))

    def show_grade_course(self):
        for key in self.school_grade:
            grade_obj = self.school_grade[key]
            course_obj = grade_obj.course_obj
            print('\033[32;1m班級【%s】\t課程【%s】\t價格【%s元】\t週期【%s個月】\033[0m' % (
                grade_obj.grade_name, course_obj.course_name, course_obj.course_price, course_obj.course_time))

    def create_student(self, student_name, student_age, grade_name):
        student_obj = Student(student_name, student_age)
        grade_obj = self.school_grade[grade_name]
        grade_obj.grade_student[student_name] = student_obj

    def show_teacher_grade(self, teacher_name):
        teacher_obj = self.school_teacher[teacher_name]
        for key in teacher_obj.teacher_grade:
            grade_obj = teacher_obj.teacher_grade[key]
            student_list = []
            for st in grade_obj.grade_student:
                student_list.append(st)
            print('\033[32;1m班級【%s】課程【%s】學生列表【%s】\033[0m' % (
                grade_obj.grade_name, grade_obj.course_obj.course_name, student_list))
school.py
#!/usr/bin/python3
# -*- coding: utf-8 -*-
# Author: hkey


class Student(object):
    def __init__(self, student_name, student_age):
        self.student_name = student_name
        self.student_age = student_age
student.py
#!/usr/bin/python3
# -*- coding: utf-8 -*-
# Author: hkey


class Teacher(object):
    def __init__(self, teacher_name, teacher_salary):
        self.teacher_name = teacher_name
        self.teacher_salary = teacher_salary
        self.teacher_grade = {}

    def teacher_add_grade(self, grade_name, grade_obj):
        self.teacher_grade[grade_name] = grade_obj
teacher.py
#!/usr/bin/python3
# -*- coding: utf-8 -*-
# Author: hkey
import pickle


def file_oper(file, mode, *args):
    if mode == 'wb':
        data = args[0]
        with open(file, mode) as f:
            pickle.dump(data, f)
    elif mode == 'rb':
        with open(file, mode) as f:
            data = pickle.load(f)
            return data
tools.py
相關文章
相關標籤/搜索