python一个选课的小功能竟然捣鼓半天,有瑕疵,日后精简修复

尝试用python写的选课系统小功能,捣鼓了半天竟然,有瑕疵,日后留着精进修复,利用了反射,代码冗余,后期后期……

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
import random
lessons = ['JAVA语言开发与设计', 'C语言开发与设计', 'C++语言开发与设计', 'C#语言开发与设计', 'PHP语言开发与设计', 'MySql语言开发与设计', 'PYTHON语言开发与设计', 'JAVASCRIPT语言开发与设计', 'GO语言开发与设计', 'LINUX语言开发与设计']
citys = ['北京', '上海', '深圳', '广州', '天津', '重庆', '成都', '武汉', '郑州', '杭州']
class Common:
# 学生列表
stu_list = []
# 所有课程
course_list = []
# 已选课程-学生
selected_course_list = {}
def show_courses(self):
'''
查看所有课程
:return:
'''
print('{} {:20} {:11} {:4}'.format('城市', '课程', '价格', '学期'))
for i in self.course_list:
print('{} {:21} {:12} {:4}'.format(i.course_city, i.course_name, i.course_price, i.course_time))


class Student(Common):
menu_list = [{'title': '查看所有课程', 'name': 'show_courses'},
{'title': '选择课程', 'name': 'elective_course'},
{'title': '查看所选课程', 'name': 'show_coursed'},
{'title': '删除已选课程', 'name': 'del_course'}
]
def __init__(self, u_name=None, u_account=None, u_pwd=None):
self.u_name = u_name
self.u_account = u_account
self.u_pwd = u_pwd

def login(self, u_account, u_pwd):
'''
登录
:param u_account:账号
:param u_pwd:密码
:return:1成功,0失败
'''
for stu in self.stu_list:
if stu.u_account == u_account and stu.u_pwd == u_pwd:
self.u_name = stu.u_name
self.u_account = u_account
self.u_pwd = u_pwd
return 1
else:
return -1

def show_coursed(self):
'''
查看已选课程
:return:
'''
selected_courses_data = []


for stu, course in self.selected_course_list.items():
if self.u_account == stu.u_account:
selected_courses_data.append(course)
print('{} {:21} {:12} {:4}'.format(course.course_city, course.course_name, course.course_price, course.course_time))
if len(selected_courses_data) != 0:
return selected_courses_data
else:
print('抱歉,您还没有选修的课')
return -1
def elective_course(self):
"""
选择课程
:param course:
:return:
"""
print('{:4} {} {:20} {:11} {:4}'.format('编号', '城市', '课程 ', '价格', '学期'))
for index,course in enumerate(self.course_list, 1):
print('{:4} {} {:21} {:12} {:4}'.format(index, course.course_city, course.course_name, course.course_price, course.course_time))
course_id = int(input('请选择您要学习的课程编号:'))
self.selected_course_list[self] = self.course_list[course_id-1]

print('【%s】成功选修【%s】成功' % (self.u_name, self.course_list[course_id-1].course_name))
def del_course(self):
'''
删除已选课程
:param course:
:return:
'''
selected_course_data = self.show_coursed()
if selected_course_data == -1:
print('你还没有选择课程呢')
return -1
print('{:4} {} {:20} {:11} {:4}'.format('编号', '城市', '课程 ', '价格', '学期'))
for index,course in enumerate(selected_course_data, 1):
print('{:4} {} {:21} {:12} {:4}'.format(index, course.course_city, course.course_name, course.course_price,
course.course_time))
course_id = int(input('请选择您要放弃的课程:'))
course_active = selected_course_data[course_id-1]
for stu, course in self.selected_course_list.items():
if stu.u_account == self.u_account and course == course_active:
del self.selected_course_list[stu]
class Administrator(Common):
menu_list = [{'title':'创建课程', 'name':'create_course'},
{'title': '创建学生账号', 'name': 'create_account'},
{'title': '查看所有课程', 'name': 'show_courses'},
{'title': '查看所有学生', 'name': 'show_stu'},
{'title': '查看所有学生的选课情况', 'name': 'show_stu_course'}
]
def login(self, u_account, u_pwd):
'''
登录
:param u_account:账号
:param u_pwd:密码
:return:1成功,0失败
'''
if 'admin' == u_account and '123' == u_pwd:
return 1
else:
return -1
def create_course(self):
'''
创建课程
:param course:
:return:
'''
course_name = random.sample(lessons, 1)[0]
course_city = random.sample(citys, 1)[0]
course_price = '¥' + format(random.randrange(10000, 30000), ',.2f')
course_time = '第' + str(random.randrange(1, 5)) + '期'
self.course_list.append(Course(course_name, course_city, course_price, course_time))
print('课程【%s】创建成功' % course_name)
def create_account(self):
'''
创建学生账户
:param student:
:return:
'''
u_name = input('请输入学员姓名:')
u_account = input('请输入学员账户:')
u_pwd = input('请输入学员密码:')
self.stu_list.append(Student(u_name, u_account, u_pwd))
print('学员【'+u_name+'】创建成功')
def show_stu(self):
'''
查看所有学员
:return:
'''
print('{:5} {:15} {:6}'.format('姓名', '账户', '密码'))
for i in self.stu_list:
print('{:5} {:15} {:6}'.format(i.u_name, i.u_account, i.u_pwd))
def show_stu_course(self):
'''
查看所有学员选课情况
:return:
'''
if len(self.selected_course_list) == 0:
print('你还没有选修任何课程')
return -1
for stu,course in self.selected_course_list.items():
print('{:4} {} {:21} {:12} {:4}'.format(stu.u_name, course.course_city, course.course_name, course.course_price, course.course_time))

class Course(object):
def __init__(self, course_name, course_city, course_price, course_time):
self.course_name = course_name
self.course_city = course_city
self.course_price = course_price
self.course_time = course_time

separator = '-'*80
def run():
print('欢迎来到选课系统')
print(separator)
while 1:
print('''1 学员登录
2 管理员登录''')
print(separator)
login_type = input('请选择登录类型编号:')
if login_type == '1':
u_account = input('请输入您的用户名:')
u_pwd = input('请输入您的密码:')
stu = Student()
if stu.login(u_account, u_pwd) > 0:
print(separator)
for index, menu in enumerate(stu.menu_list, 1):
print(index, menu['title'])
print(separator)
menu_type = int(input('请选择您要操作的功能编号:'))
menu_method = getattr(stu, stu.menu_list[menu_type - 1]['name'])
print(separator)
menu_method()
print(separator)
else:
print('账户密码有误,请重新选择')
continue
elif login_type == '2':
u_account = input('请输入您的用户名:')
u_pwd = input('请输入您的密码:')
admin = Administrator()
if admin.login(u_account, u_pwd) > 0:
print(separator)
for index,menu in enumerate(admin.menu_list, 1):
print(index, menu['title'])
print(separator)
menu_type = int(input('请选择您要操作的功能编号:'))
menu_method = getattr(admin, admin.menu_list[menu_type-1]['name'])
print(separator)
menu_method()
print(separator)
else:
print('账户密码有误,请重新选择')
continue
else:
break
run()