1) 程序主功能代码 def config_path(arg): ''' 返回当前系统下的数据文件的存放位置 ''' # BASE_DIR = os.path.dirname(os.path.abspath(os.path.abspath(__file__))) if platform.system() == "Windows": conf_path = (PATH_DIR+'\'+'database'+'\'+arg) else: conf_path = (PATH_DIR+'/'+'database'+'/'+arg) return conf_path class file_open(object): ''' 文件读写操作 ''' def __init__(self,database,mothed): self.db = database self.mode = mothed def write_f(self,info): self.info = info f = open(self.db,self.mode, encoding='utf-8') f.write(json.dumps(self.info,ensure_ascii=False)) def read_f(self): f = open(self.db,self.mode, encoding='utf-8') info = json.loads(f.read()) return (info) def __del__(self): pass class school(file_open): def __init__(self,school_name): self.sc_name = school_name def get_school(self): info = file_open(config_path("db_school.txt"),'r').read_f() if self.sc_name in info.keys(): print((self.sc_name+"校区").center(35,'+'),' ',('{0:1s}{1:10s}{2:10s}'.format('%s'.center(0), '%s'.center(10), '%s'.center(10)) % (info[self.sc_name][0],info[self.sc_name][1],info[self.sc_name][2]))) # return info[self.sc_name] else: # print('%s校区不存在'%self.sc_name) return "NULL" def creat_school(self,school_fullname,school_address,school_city): self.sc_fname = school_fullname self.sc_address = school_address self.sc_city = school_city info = {self.sc_name:[self.sc_fname,self.sc_address,self.sc_city]} print("创建学校".center(45,'+'),' ',('{0:1s}{1:10s}{2:10s}{3:10s}'.format('%s'.center(0), '%s'.center(10),'%s'.center(10), '%s'.center(10)) % (self.sc_name,self.sc_fname,self.sc_address,self.sc_city))) # file_open(config_path("db_school.txt"),'a+').write_f(info) r_info = file_open(config_path("db_school.txt"),'r+').read_f() if self.sc_name in r_info.keys(): print("修改%s信息"%self.sc_fname) else: r_info.update(info) file_open(config_path("db_school.txt"), 'w+').write_f(r_info) class course(school): def __init__(self,course_name,school_name): super(course,self).__init__(school_name) self.co_name = course_name ''' 预留课程创建权限 ''' def creat_course(self,course_semeste,course_time,course_price,course_teacher): self.co_semeste = course_semeste self.co_time = course_time self.co_price = course_price self.co_teacher = course_teacher # 'SH':[['go','3期','2017.8.1','8000','luo']] info = {self.sc_name:[[self.co_name,self.co_semeste,self.co_time,self.co_price,self.co_teacher]]} if school(self.sc_name).get_school() =="NULL": print("校区不存在!") def get_course(self): # print(info["SH"][0]) >> ['go', '3期','2017.8.1', '8000', 'luo'] info = file_open(config_path("db_course.txt"), 'r').read_f() for n in filter(lambda x: self.co_name in x, [x for x in info[self.sc_name]]): return n def classes_info(self): info = self.get_course() self.get_school() tip = ('{0:10s}{1:10s}{2:10s}{3:10s}'.format('课程'.center(10),'学期'.center(10),'开课时间'.center(10),'导师'.center(10))) msg = ('{0:10s}{1:10s}{2:10s}{3:10s}'.format('%s'.center(10),'%s'.center(10),'%s'.center(10),'%s'.center(10))%(info[0],info[1],info[2],info[4])) print(("%s班级"%self.co_name).center(45,'-'),' ',tip,' ',msg) return info class people(course): def __init__(self,school_name): # super(people,self).__init__(school_name) self.sc_name = school_name def new_one(self,p_name,p_age,p_passwd,p_role): self.p_name = p_name self.p_age = p_age self.p_passwd = p_passwd self.p_role = p_role info = file_open(config_path("db_members.txt"), 'r').read_f() old_p = info[self.sc_name][p_role] #现有的学员/导师 new_p = [self.p_name, self.p_age, self.p_passwd] #新学员/导师 old_p.append(new_p) info[self.sc_name][p_role] = old_p file_open(config_path("db_members.txt"), 'w').write_f(info) def get_msg(self,course_name): super(people,self).__init__(course_name) info = file_open(config_path("db_members.txt"), 'r').read_f() def goods_list(username): menus_tmp = [] #临时存放货物类别 goods = json.loads(goodsinfo) while True: #打印一级目录 print("Credit card mall!".center(60, "-")) for k in enumerate(goods): print(k[0], '.', k[1]) print(' ') menus_tmp.append(k) for j in enumerate(goods[k[1]]): print(' ', j[0], '.', j[1]) try: i = input("一输入你想购买的商品类别编号:") while True: if i.isdigit(): i = int(i) if i >= 0 and i <= len(goods.items()) - 1: break else: i = input("超出商品列表范围!") continue else: i = input("请输入数字!") continue except ValueError as e: print(e) pass # 打印二级商品栏目 s = goods.get(menus_tmp[i][1]) shopping_car = [] for j in range(len(s)): print(j, s[j]) k = int(input("三输入你想购买的商品编号:")) if k not in range(len(s)): print("输入的数值不在商品编号范围内!") pass else: n = int(input("四请输入你想要购买的商品数量:")) cost = s[k][1] * n message=("您购买 %s ,单价 %0.2f 数量为%d ;共计花费:%0.2f" % (s[k][0], s[k][1], n, cost)) settings.make_transaction(username, 'withdraw', cost,message) print(message) stat = input("重新请输入y,退出请输入n") if stat == 'n': print("欢迎下次光临!") break else: continue return "购物愉快!" 2)注册 F:Python35python.exe F:/mologa-workspace/week_4/choose_lesson/core/classes.py ~~~~~~~~~~~~~~~~~~~欢迎来到oldboy学院~~~~~~~~~~~~~~~~~~~ 1.登录 2.注册 3.关闭 输入选项序号:2 BJ FJ SH 输入校区:BJ 请选择角色:[1.教师 2.学生]2 请输入用户名:lisi 请输入年龄:45 请输入密码:li123 1.登录 2.注册 3.关闭 输入选项序号:1 请输入用户名:lisi 请输入密码:lisi123 密码错误! 请输入用户名:lisi 请输入密码:li123 1.选课 2.成绩 3.上课纪录 3.关闭 请输入功能序号:1 课程 学期 开课时间 学费 导师 go 3期 2017.10.1 8000 luo linux 16期 2017.6.1 9000 alex python 13期 2017.8.1 9500 alex 3) 系统会根据登录用户名来判断属性[student,teacher]决定功能 -------------------------teacher---------------------- F:Python35python.exe F:/mologa-workspace/week_4/choose_lesson/core/classes.py ~~~~~~~~~~~~~~~~~~~欢迎来到oldboy学院~~~~~~~~~~~~~~~~~~~ 1.登录 2.注册 3.关闭 输入选项序号:alex 1.登录 2.注册 3.关闭 输入选项序号:1 请输入用户名:alex 请输入密码:alex123 1.负责班级 2.关闭 请输入功能序号:1 课程 学期 开课时间 linux 16期 2017.6.1 python 13期 2017.8.1 1.负责班级 2.关闭 请输入功能序号:2 欢迎下次光临! …… -------------------------student---------------------- F:Python35python.exe F:/mologa-workspace/week_4/choose_lesson/core/classes.py ~~~~~~~~~~~~~~~~~~~欢迎来到oldboy学院~~~~~~~~~~~~~~~~~~~ 1.登录 2.注册 3.关闭 输入选项序号:1 请输入用户名:miao 请输入密码:miao123 1.选课 2.成绩 3.上课纪录 3.关闭 请输入功能序号:1 课程 学期 开课时间 学费 导师 go 3期 2017.10.1 8000 luo linux 16期 2017.6.1 9000 alex python 13期 2017.8.1 9500 alex 请选择课程:linux ---------miao选择linux,扣款9000---------- linux 16期 2017.6.1 9000 alex 1.选课 2.成绩 3.上课纪录 3.关闭 请输入功能序号:2 python 85 go 100 1.选课 2.成绩 3.上课纪录 3.关闭 请输入功能序号:3 python 5 go 2 1.选课 2.成绩 3.上课纪录 3.关闭 请输入功能序号:4 欢迎再次光临! Process finished with exit code 1 总结备注 这次作业持续了很久,数据表设计很久还是没有很好的办法,最后还是用字典存储,没有用pickle而是用json序列化,因为这样我能随时看数据结构和修改,些许功能暂未实现,希望自己能坚持学习。 学习: 1.class类中是方法和功能的集合 2.选项类别可以用字典这么写,避免代码冗长和繁杂 def login(self): menu = u''' ------- 欢迎进入管理视图 --------- 33[32;1m 1. 校区管理 2. 讲师管理 3. 学员管理 4. 课程管理 5. 返回 33[0m''' menu_dic = { '1': Admin_view.school_manager, '2': Admin_view.teacher_manager, '3': Admin_view.student_manager, '4': Admin_view.course_manager, '5': "logout", } username = input("输入用户名:").strip() password = input("输入密码:").strip() auth = Admin_view.auth(self,username,password) if auth: exit_flag = False while not exit_flag: print(menu) option = input("请选择:").strip() if option in menu_dic: if int(option) == 5: exit_flag = True else: print(menu_dic[option]) menu_dic[option](self) else: print(" 33[31;1m输入错误,重新输入 33[0m")