Eva_J 老师博客链接: https://www.cnblogs.com/Eva-J/articles/11370946.html day06-面向对象
开发环境:
Python3.7
Windows
各文件夹说明:
bin:
start.py #启动目录
conf:
settings.py #配置文件,用于保存项目目录、课程、选课、以及用户等文件目录
core: #主要代码块
main.py # 主程序目录
authentication.py #登录认证
student.py #学生类
manager.py #管理员类
course.py #课程类和公共类
db: #数据存储
course # 课程数据,存储类型为pickle
select_course #学生已选课程信息,存储类型为pickle
user # 用户表,明文存储
bin 目录下:

conf 目录下:


#!/usr/bin/python
# -*- coding:utf-8 -*-
# Author :wangliujun
import os
path = r'E:\老男孩Python\Class_27\Class_27\CourseSelectionSystem' # 项目目录
course_file = os.path.join(path, 'db\course') # 存储课程
user_file = os.path.join(path, r'db\user') # 存储用户
select_course = os.path.join(path, 'db\select_course') # 已选课程
core 目录下:


1 #!/usr/bin/python
2 # -*- coding:utf-8 -*-
3 # Author :wangliujun
4
5 import sys
6 from core import authentication as au
7 from core import manager as ma
8 from core import student as stu
9
10
11 def main():
12 ret = au.login()
13 if ret:
14 while True:
15 usr, role = ret[0], ret[2]
16 if role == 'Student':
17 cls = getattr(stu, role)
18 elif role == 'Manager':
19 cls = getattr(ma, role)
20 obj = cls.init(usr)
21 print('-' * 40)
22 for index, i in enumerate(cls.menu_list, 1): # 遍历列表中菜单
23 print(index, i[0])
24 print('-' * 40)
25 choice = int(input('请选择序号:').strip())
26 func = obj.menu_list[choice - 1][1] # 取出选择序号对应的方法名,字符串类型
27 getattr(obj, func)() # 执行对象中的方法
28 # cls = getattr(sys.modules[__name__], role)
29 # obj = cls.init(usr)
30 # print('-' * 40)
31 # for index, i in enumerate(cls.menu_list, 1): # 遍历列表中菜单
32 # print(index, i[0])
33 # print('-' * 40)
34 # choice = int(input('请选择序号:').strip())
35 # func = obj.menu_list[choice - 1][1] # 取出选择序号对应的方法名,字符串类型
36 # getattr(obj, func)() # 执行对象中的方法


1 #!/usr/bin/python
2 # -*- coding:utf-8 -*-
3 # Author :wangliujun
4
5 import os
6 import sys
7 from conf import settings as cof
8
9
10 def login():
11 input_usr = input('请输入用户名:').strip()
12 input_pwd = input('请输入密码:').strip()
13 with open(cof.user_file, mode='r', encoding='utf-8') as f:
14 for line in f:
15 if line:
16 usr, pwd, role = line.strip().split('|')
17 if usr == input_usr and pwd == input_pwd:
18 print('%s登录成功,欢迎您%s' % (role, usr))
19 return usr, pwd, role
20 else:
21 print('账号密码错误')
22 return None


1 #!/usr/bin/python
2 # -*- coding:utf-8 -*-
3 # Author :wangliujun
4
5 import pickle
6 from conf import settings as cof
7 from core import course as co
8
9 class Course(): # 课程类
10 def __init__(self, name, price, cycle, teacher):
11 self.name = name
12 self.price = price
13 self.cycle = cycle
14 self.teacher = teacher
15
16
17 class Public(object): # 公共类
18 def show_courses(self): # 查看所有课程
19 with open(cof.course_file, mode='rb') as f:
20 print('序号:\t课程名\t课程价格\t课程周期\t老师')
21 n = 1
22 while True:
23 try:
24 course = pickle.load(f)
25 tplt = '{0:<3}{1:<15}{2:<10}{3:<10}{4:<10}'
26 print(tplt.format(n, course.name, course.price, course.cycle, course.teacher))
27 n += 1
28 except EOFError:
29 break
30
31 def exit(self): # 退出
32 exit('bye')


1 #!/usr/bin/python
2 # -*- coding:utf-8 -*-
3 # Author :wangliujun
4
5 import sys
6 import pickle
7 from core import student as stu
8 from core import course as co
9 from conf import settings as cof
10
11
12 class Manager(co.Public): # 管理员
13 menu_list = [
14 ('创建课程', 'create_course'),
15 ('查看所有课程', 'show_courses'),
16 ('创建学生账号', 'create_stu'),
17 ('查看所有学生', 'show_students'),
18 ('查看所有选课情况', 'show_sit'),
19 ('退出程序', 'exit')] # 管理员菜单
20
21 def __init__(self, name):
22 self.name = name
23
24 @classmethod
25 def init(cls, username):
26 return cls(username) # 返回实例化对象
27
28 def create_course(self):
29 name = input('课程名:')
30 price = input('课程价格:')
31 cycle = input('课程周期:')
32 teacher = input('老师:')
33 course = co.Course(name, price, cycle, teacher) # 创建新的课程对象
34 with open(cof.course_file, 'ab') as f:
35 pickle.dump(course, f)
36 print('课程创建成功') # 创建课程
37
38 def create_stu(self): # 创建学生账号
39 user = input('请输入学生用户名:').strip()
40 pwd = input('请输入密码:').strip()
41 cof_pwd = input('确认密码:').strip()
42 if pwd == cof_pwd and user and pwd:
43 with open(cof.user_file, 'a', encoding='utf-8') as f:
44 f.write('\n%s|%s|%s' % (user, pwd, 'Student'))
45 print('用户%s创建成功' % user)
46 stu_obj = stu.Student(user)
47 with open(cof.select_course, mode='ab') as f:
48 pickle.dump(stu_obj, f)
49 else:
50 print('帐号密码格式不符合')
51
52 def show_students(self): # 查看所有学生
53 with open(cof.select_course, mode='rb') as f:
54 n = 1
55 while True:
56 try:
57 obj = pickle.load(f)
58 print('%s %s' % (n, obj.name))
59 n += 1
60 except EOFError:
61 print('-' * 50)
62 break
63
64 def show_sit(self): # 查看所有选课情况
65 with open(cof.select_course, 'rb') as f:
66 n = 1
67 while True:
68 try:
69 stu_obj = pickle.load(f)
70 print('%s 用户:%s\t' % (n, stu_obj.name), end='所选课程:')
71 for i in stu_obj.course_list: print(i.name, end=' ')
72 n += 1
73 print()
74 except EOFError:
75 break


1 #!/usr/bin/python
2 # -*- coding:utf-8 -*-
3 # Author :wangliujun
4 import pickle
5 import os
6 from conf import settings as cof
7 from core import course as co
8
9 class Student(co.Public): # 学生类
10 # 存储学生菜单
11 menu_list = [
12 ('查看可选课程', 'opt_course'),
13 ('选择课程', 'select_course'),
14 ('查看所选课程', 'show_selected_course'),
15 ('退出', 'exit')
16 ]
17
18 def __init__(self, name):
19 self.name = name
20 self.course_list = []
21
22 @classmethod
23 def init(cls,username):
24 with open(cof.select_course, mode='rb') as f:
25 while True:
26 try:
27 stu_obj = pickle.load(f)
28 if stu_obj.name == username:
29 return stu_obj
30 except EOFError:
31 break
32
33 def opt_course(self): # 查看可选课程
34 co.Public.show_courses(self)
35
36 def select_course(self): # 选择课程
37 co.Public.show_courses(self)
38 choice = int(input('请输入要选择的序号:').strip())
39 n = 1
40 with open(cof.course_file, 'rb') as f:
41 while True:
42 try:
43 obj = pickle.load(f)
44 if choice == n:
45 self.course_list.append(obj)
46 print('用户%s,添加课程%s成功' % (self.name, obj.name))
47 break
48 n += 1
49 except EOFError:
50 print('没有要选的课程')
51 tmp = os.path.dirname(cof.select_course) + '/tmp'
52 with open(cof.select_course, mode='rb') as f1, open(tmp, mode='wb') as f2:
53 # with open(select_course, mode='rb'):
54 while True:
55 try:
56 obj = pickle.load(f1)
57 if obj.name == self.name:
58 obj = self
59 pickle.dump(obj, f2)
60 except EOFError:
61 break
62 os.remove(cof.select_course)
63 os.rename(tmp, cof.select_course)
64
65 def show_selected_course(self): # 查看已选课程
66 with open(cof.select_course, 'rb') as f:
67 while True:
68 try:
69 obj = pickle.load(f)
70 if self.name == obj.name:
71 for i in obj.course_list:
72 print(i.name)
73 except EOFError:
74 break
db 目录下:
存储格式: 用户名|密码|角色 例: alex|alex3714|Manager
来源:oschina
链接:https://my.oschina.net/u/4408924/blog/3409075