不多bb了,直接上代码吧。
from pprint import pprint
class Course:
total_course = []
def __init__(self,name,id):
self.name = name
self.id = id
self.is_select = False
Course.total_course.append(self)
def __repr__(self):
return (
f"{__class__.__name__}("
f"学科:{self.name!r},学科代码:{self.id!r})"
)
class Students:
total_students = []
def __init__(self):
self.selected_courses = []
# Students.total_students.pop()
def register(self,name,id,institution,classes,password):
self.name = name
self.id = id
self.institution = institution
self.classes = classes
self.password = password
Students.total_students.append(self)
def log_in(self,student_id,student_password):
for i in Students.total_students:
if student_id == i.id and student_password == i.password:
return True
return False
def select_course(self,course):
for i in Course.total_course:
if i.name == course:
i.is_select = True
self.selected_courses.append((i.name,i.id))
return True
return False
def withdraw_a_course(self,course):
for i in range(len(self.selected_courses)):
if self.selected_courses[i][0] == course:
self.selected_courses.pop(0)
return True
return False
def get_name_through_id(self,id):
for i in Students.total_students:
if i.id == id:
return i.name
def get_info_through_name(self,name):
for i in Students.total_students:
if i == None:
continue
elif i.name == name:
return i
def __repr__(self):
return (
f"{__class__.__name__}("
f"{self.name!r},{self.id!r},{self.institution!r},{self.classes!r},{self.password!r})"
)
Course.total_course.extend([
Course("Python","25234"),
Course("高等数学","23123"),
Course("线性代数","23145"),
Course("数字逻辑","89080"),
Course("算法设计与分析","237234"),
Course("物理","2358239"),
Course("生物","43598734"),
Course("化学","233521"),
Course("计算机网络","45234324")
])
Students.total_students.extend([
Students().register("Jace","2432","马克思主义学院","12","1234125"),
Students().register("Curry","2352525","计算机学院","13","124125")
])
# info = list(input("请输入所有相关个人信息(姓名,学号,学院,班级,密码):").split())
# Students().register(*info)
# print("您已注册成功并成功登录!")
# # course = input("请输入选课名称:").strip()
# # info = Students().get_info_through_name("Jace")
# # if info.select_course(course):
# # print("选课成功!")
# pprint(Students.total_students)
student_name = ""
info = ""
is_log_in = False
while True:
if is_log_in == False:
print("欢迎使用选课系统!")
print("1. 注册学生")
print("2. 登录")
print("3. 选课")
print("4. 退课")
print("5. 查看已选课程")
print("6. 查看所有课程")
print("7. 退出系统")
print("8. 终止程序")
print("如果您未注册,请输入1注册")# 假定学生操作无误
print("如果您已注册,请输入2登录")
print("如果已经登陆,请输入对应序号来进行其他操作")
x = int(input())
if x == 1:# 假定输入无误
info = list(input("请输入所有相关个人信息(姓名,学号,学院,班级,密码):").split())
Students().register(*info)
print("您已注册成功并成功登录!")
is_log_in = True
student_name = info[0]
elif x == 2:
info = list(input("请输入学号和密码.").split())
if Students().log_in(*info):
print("成功登录")
is_log_in = True
student_name = Students().get_name_through_id(info[0])
else:
print("学号或密码不正确,请再尝试一下.")
elif x == 3:
course = input("请输入选课名称:").strip()
info = Students().get_info_through_name(student_name)
if info.select_course(course):
print("选课成功!")
else:
print("没有该课程.")
elif x == 4:
course = input("请输入退课名称:").strip()
info = Students().get_info_through_name(student_name)
if info.withdraw_a_course(course):
print("退课成功")
else:
print("没有该退课课程")
elif x == 5:
info = Students().get_info_through_name(student_name)
if info.selected_courses == []:
print("您未选课")
else:
print(f"您学课程如下:{info.selected_courses}")
elif x == 6:
print("所有科目显示如下:")
pprint(Course.total_course)
elif x == 7:
print("您已退出")
student_name = ""
info = ""
is_log_in = False
continue
elif x == 8:
break
一个小题目,本来使用列表+字典实现的,我看了想了想,还是通过面向对象来做做吧,提高一下能力。整个代码测试下来的时候就是在Students中的类变量列表total_students,在我一开始通过两个register初始化的时候,总会出现多余的两个None,不知道为啥,以及为何出现,并且在选课模块出现了问题,我自己看了看,改了.get_info_through_name的内部逻辑,如果为None的话直接continue,这样能解决问题,但是并没有根本解决问题。
最近看了manim大佬的源代码,确实能学到很多东西,对面向对象又够了更深层次的认识。如果缺少这一认识,这个题目虽然也能用面向对象思想来做,然理解程度肯定不够。当然,现在肯定也是不够的,还需要多多摸索。manim中的类、方法,肯定是因为为了解决实际情况而产生的,对于制作它们的作者来说,理解起来很容易,但是开源让别人学的话,不明白其切实含义,缺少实践,就是很难理解。比如在我的Students类中有这两个方法:
def get_name_through_id(self,id):
for i in Students.total_students:
if i.id == id:
return i.name
def get_info_through_name(self,name):
for i in Students.total_students:
if i == None:
continue
elif i.name == name:
return i
别人来看肯定不理解什么意思以及为什么有这两个方法,但是确实是我自己在解决问题时必要的,第一个方法是通过id找到name,即由学号找到姓名。登录时是输入学号+密码登录,通过学号找到姓名,进而在进行一些列的操作。第二个方法是通过姓名找到所有个人信息,看名字就知道啥意思了,我也不多说了。这都是有其存在的意义的。
还是要感谢那个manim大佬,让我对抽象类有了额外的理解。无法用言语说明,只能自个去悟。