python之路34面向对象作业之学生选课系统

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了python之路34面向对象作业之学生选课系统相关的知识,希望对你有一定的参考价值。

一、需求:

1、可以注册管理员账号,管理员账号可以创建老师和课程

2、学生可以注册和登陆,学生可以从课程列表选课,可以进行上课登记查看

二、代码

1、文件目录

bin 存放可执行文件

config 存放可配置文件

db 存放数据文件

lib 存放实现类

目录结构如图:

技术分享

技术分享

2、admin.py 中的代码

技术分享
#!usr/bin/env python
# -*- coding:utf-8 -*-
"""admin注册登录,以及用admin账号创建老师和课程"""
import pickle
import os
import sys
sys.path.append(os.path.dirname(os.path.dirname(__file__)))
#import time
from config import setting
from lib.models import *  #导入所有类

def login():
    """用户登陆"""
    username = input("请输入用户名:")
    pwd = input(请输入密码:)
    if os.path.exists(os.path.join(setting.ADMIN_PATH ,username)):
        with open(os.path.join(setting.ADMIN_PATH ,username), rb) as f:
            admin_obj = pickle.load(f)
            if admin_obj.login(username, pwd):
                print(登陆成功!)
                return admin_obj
            else:
                print(密码错误!)
    else:
        print(账号不存在!)
        exit()
def regester():
    """用户注册"""
    username = input("请输入用户名:")
    pwd = input(请输入密码:)
    adm = Admin()
    adm.regester(username, pwd)
def creat_teacher(admin_obj):
    li_teacher = []
    while True:
        name = input(请输入要创建老师的名字,Q退出:)
        if name == "Q":
            break
        age = input(请输入要创建老师的年龄:)
        obj = Teacher(name,age,admin_obj)
        li_teacher.append(obj)
    if os.path.exists(setting.TEACHER_PATH):
        read_teacher = pickle.load(open(setting.TEACHER_PATH,rb))
        li_teacher.extend(read_teacher)
    pickle.dump(li_teacher,open(setting.TEACHER_PATH,wb))
    #print(li_teacher)
def creat_course(admin_obj):
    print(老师信息:)
    teacher_list = pickle.load(open(setting.TEACHER_PATH,rb))
    for num,item in enumerate(teacher_list,1):
        print(num, item.name, item.age, item.creat_time, item.admin_obj.username)
    course_li = []
    while True:
        course_name = input(请输入课程名(Q退出):)
        if course_name == "Q":
            break
        cost = input(请输入课时费:)
        num = input(请选择老师的序号:)
        course_obj = Course(course_name, cost, teacher_list[int(num)-1], admin_obj)
        course_li.append(course_obj)
    if os.path.exists(setting.COURSE_PATH):
        read_course = pickle.load(open(setting.COURSE_PATH,rb))
        course_li.extend(read_course)
    pickle.dump(course_li, open(setting.COURSE_PATH,wb))

def main():
    isregsterOrlogin = input(登陆请按1,注册请按2\\n)
    if isregsterOrlogin == 1:
        admin_obj = login()  #登陆
        creat_teacher(admin_obj)  #创建老师,写进文件teacher/teacher
        creat_course(admin_obj)  #创建课程
    elif isregsterOrlogin == 2:
        regester()  #注册


if __name__ == __main__:
    main()
admin

3、student.py 中的代码

技术分享
#!usr/bin/env python
# -*- coding:utf-8 -*-
import sys
import os
sys.path.append(os.path.dirname(os.path.dirname(__file__)))

from config import setting
import pickle
from lib.models import *

def login():
    username = input(请输入用户名:)
    pwd = input(请输入密码:)
    if os.path.exists(os.path.join(setting.STUDENT_PATH, username)):
        student_obj = pickle.load(open(os.path.join(setting.STUDENT_PATH, username),rb))
        if student_obj.login(username, pwd):
            print(登陆成功!)
            return student_obj
        else:
            print(密码错误)
    else:
        print(用户名不存在!)

def register():
    username = input(请输入用户名:)
    pwd = input(请输入密码:)
    student_obj = Student()
    student_obj.register(username, pwd)

def choose_course(student_obj):
    """选择课程"""
    course_obj_list = pickle.load(open(setting.COURSE_PATH, rb))
    for num,item in enumerate(course_obj_list, 1):
        print(num, item.course_name, item.cost, item.teacher_obj.name)
    while True:
        num = input(请选择课程序号(Q退出):)
        if num == "Q":
            break
        if course_obj_list[int(num) - 1] not in student_obj.choose_course_list:
            student_obj.choose_course_list.append(course_obj_list[int(num) - 1])
    pickle.dump(student_obj, open(os.path.join(setting.STUDENT_PATH, student_obj.username), wb))

def show_choose_course(student_obj):
    for num,item in enumerate(student_obj.choose_course_list, 1):
        print(num, item.course_name, item.cost, item.teacher_obj.name)

def attend_course(student_obj):
    print(选择的课程有:)
    show_choose_course(student_obj)
    while True:
        num = input(请输入您参加课程的序号(Q退出):)
        if num == "Q":
            break
        student_obj.attend_course_list.append(student_obj.choose_course_list[int(num) - 1])
    pickle.dump(student_obj, open(os.path.join(setting.STUDENT_PATH, student_obj.username), wb))

def show_attend_course(student_obj):
    for num,item in enumerate(student_obj.attend_course_list, 1):
        print(num, item.course_name, item.cost, item.teacher_obj.name)

def main():
    inp = input(登陆请按1,注册请按2\\n>>>>:)
    if inp == 1:
        student_obj = login()
        temp = input(选课请按1,显示已选课程请按2,上课请按3,显示上课请按4 :)
        if temp == 1:
            choose_course(student_obj)
        elif temp == 2:
            show_choose_course(student_obj)
        elif temp == 3:
            attend_course(student_obj)
        elif temp == 4:
            show_attend_course(student_obj)
    elif inp == 2:
        register()

if __name__ == __main__:
    main()
student.py

4、setting.py 中的代码

技术分享
#!usr/bin/env python
# -*- coding:utf-8 -*-

"""路径配置, py文件路径变了,或db下目录路径变了,需修改下面的路径"""
import os

TEACHER_PATH = os.path.join(os.path.dirname(os.path.dirname(__file__)),db,teacher,teachers)
COURSE_PATH = os.path.join(os.path.dirname(os.path.dirname(__file__)),db,course,courses)
ADMIN_PATH = os.path.join(os.path.dirname(os.path.dirname(__file__)),db,admin)
STUDENT_PATH = os.path.join(os.path.dirname(os.path.dirname(__file__)),db,student)
setting.py

5、models.py 中的代码

技术分享
#!usr/bin/env python
# -*- coding:utf-8 -*-

""""""

import time
from config import setting
import os
import pickle

class Admin:

    def __init__(self):
        self.username = None
        self.psw = None

    def login(self,username,psw):
        """管理员登陆"""
        if self.username == username and self.psw == psw:
            return True
        else:
            return False

    def regester(self,username , pwd):
        """管理员用户注册"""
        self.username = username
        self.psw = pwd
        with open(os.path.join(setting.ADMIN_PATH,self.username),wb) as f:
            pickle.dump(self,f)

class Teacher:
    """老师类"""
    def __init__(self, name, age, admin_obj):
        self.name = name
        self.age = age
        self.creat_time = time.strftime(%Y-%m-%d %H:%M:%S)
        self.admin_obj = admin_obj

class Course:
    def __init__(self,course_name,cost,teacher_obj,admin_obj):
        self.course_name = course_name
        self.cost = cost
        self.creat_time = time.strftime(%Y-%m-%d %H:%M:%S)
        self.teacher_obj = teacher_obj
        self.admin_obj = admin_obj

class Student:

    def __init__(self):
        self.username = None
        self.psw = None
        self.choose_course_list = []
        self.attend_course_list = []

    def login(self,username,psw):
        if self.username == username and self.psw == psw:
            return True
        else:
            return False

    def register(self, username, psw):
        self.username = username
        self.psw = psw
        with open(os.path.join(setting.STUDENT_PATH, self.username), wb) as f:
            pickle.dump(self,f)
models.py

 

以上是关于python之路34面向对象作业之学生选课系统的主要内容,如果未能解决你的问题,请参考以下文章

python面向对象(选课系统)

面向对象的应用:学生选课系统

Python之路_Day7

面向对象实现简单的学生课程选择

python之选课系统详解[功能未完善]

Day7作业:选课系统