python基础-10 程序目录结构 学生选课系统面向对象练习
一 程序目录结构
1
bin文件夹
二进制文件、代码程序
2
conf 配置文件
3
帮助文档
4
头文件库文件等
二 学生选课系统部分代码 未完待续
1 包内的__init__.py文件 在包下的脚本内 导入包内 的其他包下的文件也叫模块,在2.7下导入没有__init__.py 会报错。在3.5有没有这个文件以后没有影响了。 讲解: 1 老师类 : 需要有添加时间 加工资 减工资 2 管理员类: 封装管理员用户名密码
bin 目录
1 #!/usr/bin/env python 2 # -*- coding:utf-8 -*- 3 import os, sys, pickle 4 5 sys.path.append(os.path.dirname(os.path.dirname(__file__))) 6 from lib.models import * 7 from conf import settings 8 9 10 def create_curriculum(admin_obj): 11 curriculum_list = [] 12 if os.path.exists(settings.Teacher_dir): 13 teacher_show = pickle.load(open(settings.Teacher_dir, "rb")) 14 for i, item in enumerate(teacher_show, 1): # 从1开始 15 print(i, item.teacher_name) 16 while True: 17 curriculum_name = input("请输入课程名(q退出):") 18 if curriculum_name == "q": 19 break 20 curriculum_cost = input("请输入费用:") 21 curriculum_teacher = input("请输入老师编号:") 22 curriculum_obj = Curriculum(curriculum_name, curriculum_cost, 23 teacher_show[int(curriculum_teacher) - 1].teacher_name, admin_obj) 24 curriculum_list.append(curriculum_obj) 25 if os.path.exists(settings.Curriculum_dir): 26 exist_curriculum = pickle.load(open(settings.Curriculum_dir, "rb")) 27 curriculum_list.append(exist_curriculum) 28 pickle.dump(curriculum_list, open(settings.Curriculum_dir, "wb")) 29 else: 30 print("还没有老师请先创建老师") 31 32 33 def create_teacher(admin_obj): 34 teacher_list = [] 35 while True: 36 name = input("教师名字:(q退出)") 37 if name == "q": 38 break 39 age = input("年龄") 40 teacher_obj = Teacher(name, age, admin_obj) # 创建老师 41 teacher_list.append(teacher_obj) 42 if os.path.exists(settings.Teacher_dir): 43 exists_teacher = pickle.load(open(settings.Teacher_dir, "rb")) 44 teacher_list.extend(exists_teacher) 45 pickle.dump(teacher_list, open(settings.Teacher_dir, "wb")) 46 47 48 def login(user, pwd): 49 path = os.path.join(settings.Base_admin_dir, user) # 用户名创建一个路径 50 if os.path.exists(path): # 判断路径是否存在,存在就是说明用户正确。 51 admin_obj = pickle.load(open(path, "rb")) # 获取用户名对应的对象。 52 if admin_obj.login(user, pwd): # 执行这个对象的登陆方法 53 print("登录成功") 54 while True: 55 inp = input("1、创建课程。2、创建老师(其他退出)\n>>>") 56 if inp == "1": 57 create_curriculum(admin_obj) 58 continue 59 elif inp == "2": 60 create_teacher(admin_obj) 61 continue 62 else: 63 break 64 else: 65 print("密码错误") 66 else: 67 print("账号不存在") 68 69 70 def register(user, pwd): 71 admin_obj = Admin() 72 if admin_obj.register(user, pwd): 73 print("管理员注册成功") 74 else: 75 print("管理员已存在") 76 77 78 def main(): 79 inp = input("管理员1、登陆。2、注册。(其他退出)\n>>>") 80 user = input("账号:") 81 pwd = input("密码:") 82 if inp == "1": 83 login(user, pwd) 84 elif inp == "2": 85 register(user, pwd) 86 else: 87 pass 88 89 90 if __name__ == "__main__": 91 main()
1 #!/usr/bin/env python 2 # _*_ coding:utf-8 _*_ 3 import pickle, os, sys 4 5 sys.path.append(os.path.dirname(os.path.dirname(__file__))) 6 from lib.models import * 7 from conf import settings 8 9 10 def login(user, pwd): 11 student_path = os.path.join(settings.Student_dir, user) 12 if os.path.exists(student_path): 13 student_obj = pickle.load(open(student_path, "rb")) 14 if student_obj.login(user, pwd): 15 print("登录成功") 16 while True: 17 inp = input("1、上课。2、选课。3、查看课表。(其他退出)") 18 if inp == "1": 19 study_curriculum(student_obj) 20 continue 21 elif inp == "2": 22 select_curriculum(student_obj) 23 continue 24 elif inp == "3": 25 curriculum_info(student_obj) 26 continue 27 else: 28 break 29 else: 30 print("登录失败") 31 else: 32 print("学生账号不存在") 33 34 35 def register(user, pwd): 36 student_obj = Student() 37 38 if student_obj.register(user, pwd): 39 print("学生注册成功") 40 else: 41 print("学生已存在") 42 43 44 def curriculum_info(student_obj): 45 print("已选课程信息:") 46 if student_obj.curriculum: 47 for i in student_obj.curriculum: 48 print(i.curriculum_name, i.curriculum_teacher, i.curriculum_cost) 49 else: 50 print("课表为空") 51 print("已上课表信息:") 52 if student_obj.study: 53 for i in student_obj.study: 54 print(i.curriculum_name, str(student_obj.study[i]) + "次") 55 pass 56 else: 57 print("已上课表为空") 58 59 60 def study_curriculum(student_obj): 61 62 for i, item in enumerate(student_obj.curriculum, 1): 63 print(i, item.curriculum_name, item.curriculum_teacher, item.curriculum_cost) 64 while True: 65 inp = input("输入上课序号:(q退出)") 66 if inp == "q": 67 break 68 else: 69 if student_obj.curriculum_study(student_obj.curriculum[int(inp) - 1]): 70 print("上课成功") 71 continue 72 else: 73 print("还没选课") 74 continue 75 pickle.dump(student_obj, open(os.path.join(settings.Student_dir,student_obj.username), "wb")) 76 77 def select_curriculum(student_obj): 78 print("所有课程") 79 curriculum_obj = pickle.load(open(settings.Curriculum_dir, "rb")) 80 for i, item in enumerate(curriculum_obj, 1): 81 print(str(i) + ":", item.curriculum_name, item.curriculum_cost, item.curriculum_teacher) 82 while True: 83 inp = input("请输入选课序号:(q退出)") 84 if inp == "q": 85 break 86 else: 87 select_curriculum_obj = curriculum_obj[int(inp) - 1] 88 if student_obj.select_curriculum(select_curriculum_obj): 89 print("选课成功") 90 continue 91 else: 92 print("已有课程,不用在选") 93 pickle.dump(student_obj, open(os.path.join(settings.Student_dir,student_obj.username), "wb")) 94 95 96 def main(): 97 inp = input("学生 1、登录。2、注册(任意推出)\n>>>") 98 user = input("账号:") 99 pwd = input("密码:") 100 if inp == "1": 101 login(user, pwd) 102 elif inp == "2": 103 register(user, pwd) 104 else: 105 pass 106 107 108 if __name__ == "__main__": 109 main()
conf目录
1 import os 2 3 Base_dir = os.path.dirname(os.path.dirname(__file__)) 4 Base_admin_dir = os.path.join(Base_dir, "db", "admin") 5 Teacher_dir = os.path.join(Base_dir, "db", "teacher") 6 Curriculum_dir = os.path.join(Base_dir, "db", "curriculum") 7 Student_dir = os.path.join(Base_dir, "db", "student")
db目录
管理员目录 存放密码文件 admin
学生目录 存放密码文件 student
lib 目录
1 #!/usr/bin/env python 2 # -*- coding:utf-8 -*- 3 import pickle, os, time 4 from conf import settings 5 6 7 class Admin: 8 """ 9 管理员类,注册、登陆。 10 """ 11 12 def __init__(self): 13 self.username = None 14 self.password = None 15 16 def register(self, name, pwd): 17 self.username = name 18 self.password = pwd 19 path = os.path.join(settings.Base_admin_dir, self.username) 20 if os.path.exists(path): 21 return False 22 else: 23 24 pickle.dump(self, open(path, 'xb')) # xb如果没有就新建一个 25 return True 26 27 def login(self, name, pwd): 28 if name == self.username and pwd == self.password: 29 return True 30 else: 31 return False 32 33 34 class Student: 35 """ 36 管理员类,注册、登陆。 37 """ 38 39 def __init__(self): 40 self.username = None 41 self.password = None 42 43 self.curriculum = [] 44 self.study = {} 45 46 def select_curriculum(self, curriculum_obj): 47 if curriculum_obj in self.curriculum: 48 return False 49 else: 50 self.curriculum.append(curriculum_obj) 51 return True 52 53 def curriculum_study(self, curriculum): 54 if curriculum not in self.curriculum: 55 return False 56 elif curriculum not in self.study: 57 self.study[curriculum] = 1 58 return True 59 else: 60 self.study[curriculum] += 1 61 return True 62 63 64 def register(self, name, pwd): 65 self.username = name 66 self.password = pwd 67 path = os.path.join(settings.Student_dir, self.username) 68 if os.path.exists(path): 69 return False 70 else: 71 72 pickle.dump(self, open(path, 'xb')) # xb如果没有就新建一个 73 return True 74 75 def login(self, name, pwd): 76 if name == self.username and pwd == self.password: 77 return True 78 else: 79 return False 80 81 82 class Curriculum: 83 def __init__(self, name, cost, teacher, admin): 84 self.curriculum_name = name 85 self.curriculum_teacher = teacher 86 self.curriculum_cost = cost 87 self.create_time = time.strftime('%Y-%m-%d %H:%M:%S') 88 self.create_admin = admin.username 89 90 91 class Teacher: 92 def __init__(self, name, age, admin): 93 self.teacher_name = name 94 self.teacher_age = age 95 self.create_time = time.strftime('%Y-%m-%d %H:%M:%S') 96 self.create_admin = admin.username