想要完整代码来这里哦
选课系统(面向对象)(演示视频与完整代码)
制作一个在终端运行的选课系统
属性 | 方法 | |
---|---|---|
管理员 | 用户名,id,密码 | 创建学校、创建老师(账户)、创建课程 |
老师 | 用户名,id,密码 ,课程列表 | 选择(要教授的)课程、查看课程、查看学生、修改学生分数 |
学生 | 用户名,id,密码 ,所属学区,课程列表,分数 | 选择课程、查看课程、查看分数 |
属性 | |
---|---|
学区 | 名称 |
课程 | 名称、价格、周期、所属学区、(教授的)老师、学生列表 |
学生可选一个学区、多门课程,每门课程一个分数
一门课一个老师教,一个老师多门课
一个学区多门课
(1)老师教授课程,所以老师有自己的课程(列表0
(2)学生有所属学区(属性),所学课程(列表)和每门课的分数(字典)
(3)学区存储旗下的课程(列表)
(4)课程有所属学区(属性)、教授老师(属性)和学生(列表)
用字典存储ID与密码,用列表存储字典(不同用户的)
用字典存储对象,用列表存储字典(不同用户的)
注册 | 管理员、学生 |
---|---|
登录 | 管理员、老师、学生 |
初始化的作用在于创建/清空存储信息的文件,避免加载信息时出现错误
先抽象出一个父类
class Role:
def __init__(self, usr_name, number,
password=None):
self.usr_name = usr_name
self.number = number
self.password = password
再分别定义三个子类并初始化他们的属性
class Administrator(Role):
pass
class Student(Role):
def __init__(self, usr_name, number, password, school):
Role.__init__(self, usr_name, number, password)
self.school = school
self.subject_lst = [] # subject_lst: list[obj]
self.score_dic = {
}
# score_dic: dict{subject_name: score}
class Teacher(Role):
def __init__(self, usr_name, number, password):
Role.__init__(self, usr_name, number, password)
self.subject_lst = [] # subject_lst: list[obj]
class School:
def __init__(self, name):
self.name = name
self.subject_lst = [] # subject_lst: list[str]
class Subject:
def __init__(self, name, price, cycle,
school: object):
self.name = name
self.teacher = None # teacher: str
self.information = [name, price, cycle, school.name, self.teacher] #定义information描述课程
self.school = school
self.school.subject_lst.append(self.name)
self.student_lst = [] # student_lst: list[str]
# Set variable
admin_usr_password_dic = {
}
teacher_usr_password_dic = {
}
student_usr_password_dic = {
}
admin_dic = {
}
teacher_dic = {
}
student_dic = {
}
school_dic = {
}
subject_dic = {
}
usr_password_data_lst = [admin_usr_password_dic, teacher_usr_password_dic,student_usr_password_dic]
obj_information_lst = [admin_dic, teacher_dic, student_dic, school_dic, subject_dic]
用JSON存储基本数据类型的数据
用PICKLE存储带有类数据类型的数据
# save data
def save_usr_password():
with open('UserData.json', 'w', encoding='utf-8') as fp:
#打开文件
json.dump(usr_password_data_lst, fp)
#存储
fp.close()
#关闭文件
def save_obj_information(): #同上
with open('UserData.pkl', 'wb') as fp:
pickle.dump(obj_information_lst, fp)
fp.close()
实例化学区并存储
def create_a_school():
name_str = input('请输入学区名称:')
name = School(name_str) #name: obj 实例化学区
obj_information_lst[3][name_str] = name
save_obj_information() #存储
print('学区创建完毕')
实例化老师(账户)并存储
def create_a_teacher():
usr_name_str = input('请输入教师姓名')
number = input('请输入教师id')
password = input('请输入密码')
usr_name = Teacher(usr_name_str, number, password) # usr_name: obj #usr_name: obj 实例化老师(账户)
obj_information_lst[1][usr_name_str] = usr_name
usr_password_data_lst[1][number] = password
save_obj_information()
save_usr_password() #存储
print('教师创建完毕')
实例化课程并存储
def create_a_subject():
name_str = input('请输入课程名称')
price = input('请输入课程价格')
cycle = input('请输入课程周期')
school = choose_school() # school: obj
name = Subject(name_str, price, cycle, school)
obj_information_lst[4][name_str] = name
save_obj_information()
print('课程创建完毕')
choose_school()
函数暂未定义,但我们知道它的作用是选择一个学区并返回它的实例对象,后来,我们把这个函数定义在学生下(其实定义在哪都无所谓) def choose_subject(self, subject_name: str):
self.subject_lst.append(obj_information_lst[4][subject_name].name)
obj_information_lst[4][subject_name].teacher = self.usr_name
def check_my_subject(self):
print(self.subject_lst)
# 2.teacher func
def check_all_subject(): # school_dic can be empty!!!
for schoolname, schoolobj in obj_information_lst[3].items():
print(schoolname, schoolobj.subject_lst)
def further_check(usr_name):
choise = input('1.further check\n2.choose subject\n3.end\n')
if choise == '1':
subject_name = input('subject name')
print(obj_information_lst[4][subject_name].information)
further_check(usr_name)
elif choise == '2':
subject_name = input('subject name')
usr_name.choose_subject(subject_name)
print('选课成功')
further_check(usr_name)
elif choise == '3':
pass
else:
further_check(usr_name)
# 3.student func
def choose_school():
print(obj_information_lst[3].keys())
school_str = input('请输入校区')
school = obj_information_lst[3][school_str]
return school
我们将以上代码写入文件class_information.py
下,至此准备工作便做的差不多了
admin_inter.py
import class_information
def admin_inter(usr_name: object):
print('1.create a school\n2.create a teacher\n3.create a subject\n4.log out')
tem = input('make a choice')
if tem == '1':
class_information.create_a_school()
admin_inter(usr_name)
elif tem == '2':
class_information.create_a_teacher()
admin_inter(usr_name)
elif tem == '3':
class_information.create_a_subject()
admin_inter(usr_name)
elif tem == '4':
print('log out successfully')
pass
else:
print('请在目录里面选择')
admin_inter(usr_name)
调用
import pickle
import json
import class_information
import admin_inter
import student_inter
import teacher_inter
代码顺序及流程图循序
# user-interface
def weather_initial():
choice = input()
if choice == '1':
initial()
else:
pass
load_information()
def initial():
with open('UserData.pkl', 'wb') as fp:
pickle.dump(class_information.obj_information_lst, fp)
fp.close()
with open('UserData.json', 'w', encoding='utf-8') as fp:
json.dump(class_information.usr_password_data_lst, fp)
fp.close()
def load_information():
with open('Userdata.pkl', 'rb') as fp:
class_information.obj_information_lst = pickle.load(fp)
fp.close()
with open('UserData.json', 'r', encoding='utf-8') as fp:
class_information.usr_password_data_lst = json.load(fp)
fp.close()
choose_status()
def choose_status():
print('目录')
print('1.Administrator')
print('2.Teacher')
print('3.Student')
print('4.end')
status = input('请选择身份:\n')
if status == '1':
log_or_register(status)
elif status == '2':
log_in(status)
elif status == '3':
log_or_register(status)
elif status == '4':
pass
else:
print('请在目录里面选择')
choose_status()
def log_or_register(status):
choice = input("目录\n1.注册\n2.登录\n请输入你的选择:\n")
if choice == '1':
register(status)
elif choice == '2':
log_in(status)
else:
print('请在目录里面选择')
log_or_register(status)
def register(status):
if status == '1':
admin_inter.admin_register()
else:
student_inter.stu_register()
class_information.save_usr_password()
class_information.save_obj_information()
choose_status()
def log_in(status):
if status == '1':
tem = 0
elif status == '2':
tem = 1
else:
tem = 2
usr_name = input('请输入用户名')
number = input('请输入id')
password = input('请输入密码')
if id in class_information.usr_password_data_lst[tem]:
if password == class_information.usr_password_data_lst[tem][number]:
print('log in successfully')
enter(status, class_information.obj_information_lst[tem][usr_name])
else:
print('password is not match id')
log_in(status)
else:
print('no usr')
log_in(status)
def enter(status, usr: object):
if status == '1':
admin_inter.admin_inter(usr)
elif status == '2':
teacher_inter.teacher_inter(usr)
elif status == '3':
student_inter.student_inter(usr)
class_information.save_usr_password()
class_information.save_obj_information()
choose_status()
pass
def main():
weather_initial()
我们注意到在register()
中出现两个未定义的函数admin_register()
和stu_register()
我们分别把他写入自己的文件中并调用
例
def admin_register():
usr_name_str = input('请输入用户名')
number = input('请输入id')
password = input('请输入密码')
usr_name = class_information.Administrator(usr_name=usr_name_str, number=number, password=password)
class_information.obj_information_lst[0][usr_name_str] = usr_name
class_information.usr_password_data_lst[0][number] = password
print('over')