和上一篇随笔一样的要求,完善了一下老师接口和学生接口。
搞清楚了一件事情,就是import那么多文件,该怎么搞才比较方便。
1、先上文件目录
在lib中定义了各个类,一看名字就知道是啥类了哦
在interface中定义了接口程序,用户验证身份之后就调用相应的接口
在dumpfile中分了几个文件夹,是分别存放pickle的内容的
main就是主入口了
2、程序代码
main是程序入口,由用户输入选项,选择是学校管理员、老师、还是学生,对应调用interface包中的schoolcontrol、teachercontrol、studentcontrol函数。
1 #main.py 2 from interface import * 3 from lib import * 4 5 programexitflag=False 6 optdic={‘1‘:schoolmanage.schoolcontrol,‘2‘:teachermanage.teachercontrol,‘3‘:studentmanage.studentcontrol,‘4‘:exit} 7 8 while not programexitflag: 9 idcheck=input("Who are you?\n1:Administrator\n2:Teacher\n3:Student\n4:exit\n") 10 if idcheck in optdic.keys(): 11 optdic[idcheck]()
schoolmanage.py是学校管理接口的程序。
1 from lib import * 2 import pickle 3 import os 4 5 def schoolcontrol(): 6 print("This is school management view.") 7 schoolobj = School.School() 8 9 def chooseschool(): 10 namein=input("请输入学校名字:") 11 fddir = os.path.dirname(os.path.dirname(__file__)) 12 with open(‘%s/dumpfile/school/%s‘%(fddir,namein),‘rb‘) as fd: 13 schoolobj=pickle.load(fd) 14 schoolobj.getteacher() 15 schoolobj.getcourse() 16 schoolobj.getgroup() 17 schoolobj.getstudent() 18 return schoolobj 19 20 schoolmanageoptdic={‘1‘:schoolobj.setschool,‘2‘:chooseschool,‘3‘:schoolobj.addteacher,‘4‘:schoolobj.addcourse,‘5‘:schoolobj.addgroup,‘6‘:exit} 21 flag=True 22 while flag: 23 schoolmanopt=input("你要干什么?\n1:建校\n2:选择学校\n3:招聘老师\n4:开设课程\n5:开设班级\n6:退出\n") 24 if schoolmanopt in schoolmanageoptdic.keys(): 25 schoolobj=schoolmanageoptdic[schoolmanopt]() 26 return
teachermanage是老师管理的程序
1 import os 2 import pickle 3 from lib import * 4 5 def teachercontrol(): 6 print("This is teacher management view.") 7 teacherobj=Teacher.Teacher() 8 9 def slogin(): 10 name=input("请输入您的名字:") 11 fddir = os.path.dirname(os.path.dirname(__file__)) 12 with open(‘%s\dumpfile\\teacher\%s‘ % (fddir, name), ‘rb‘) as fd: 13 teacherobj=pickle.load(fd) 14 return teacherobj 15 16 teachermanageoptdic = {‘1‘: slogin(), ‘2‘:teacherobj.checkgroupinfo, ‘3‘:teacherobj.changescore,‘4‘:exit} 17 flag = True 18 while flag: 19 teachermanopt = input("你要干什么?\n1:登陆\n2:查看班级信息\n3:登记学生成绩\n4:退出\n") 20 if teachermanopt in teachermanageoptdic.keys(): 21 teacherobj = teachermanageoptdic[teachermanopt]() 22 return
studentmanage.py是学生管理的程序
1 from lib import * 2 import pickle 3 import os 4 5 def studentcontrol(): 6 print("This is student management view.") 7 studentobj=Student.Student() 8 9 def slogin(): 10 name=input("请输入您的名字:") 11 fddir = os.path.dirname(os.path.dirname(__file__)) 12 with open(‘%s\dumpfile\student\%s‘ % (fddir, name), ‘rb‘) as fd: 13 studentobj=pickle.load(fd) 14 return studentobj 15 16 studentmanageoptdic={‘1‘:studentobj.reg,‘2‘:slogin,‘3‘:studentobj.addgroup,‘4‘:studentobj.scorecheck,‘5‘:studentobj.pay,‘6‘:exit} 17 flag=True 18 while flag: 19 studentmanopt=input("你要干什么?\n1:注册\n2:登陆\n3:报名(选择要上的班级)\n4:查成绩\n5:缴费\n6:退出\n") 20 if studentmanopt in studentmanageoptdic.keys(): 21 studentobj=studentmanageoptdic[studentmanopt]() 22 return
3、类统一都放在lib包中,在使用时只需要在__init__.py文件中,把lib包中所有类模块的名字加进去,然后在需要导入的文件中用from lib import *,在导入文件的程序中,先创建类对象实例,再用对象实例.属性(方法)的方法就可以使用类了。interface包也是一样处理。
1 #school类 2 import pickle 3 import os 4 from . import Course 5 from . import Group 6 from . import Student 7 from . import Teacher 8 9 class School(object): 10 def __init__(self): 11 self.sname=‘‘ 12 self.saddress=‘‘ 13 self.teacherlist=[] 14 self.courselist=[] 15 self.grouplist=[] 16 self.studentlist=[] 17 return 18 19 def store(self): 20 fddir=os.path.dirname(os.path.dirname(__file__)) 21 with open(‘%s\dumpfile\school\%s‘%(fddir,self.sname),‘wb‘) as fd: 22 pickle.dump(self,fd) 23 return 24 25 def setschool(self): 26 name=input("学校名字:") 27 address=input("学校地址:") 28 self.sname=name 29 self.saddress=address 30 self.store() 31 return self 32 33 def getteacher(self): 34 print("本校有以下老师:") 35 for t in self.teacherlist: 36 print(t.tname) 37 return 38 39 def getcourse(self): 40 print("本校开设以下课程:") 41 for c in self.courselist: 42 print(c.cname) 43 return 44 45 def getgroup(self): 46 print("本校开设以下班级:") 47 for g in self.grouplist: 48 print(g.gname) 49 return 50 51 def getstudent(self): 52 print("本校有以下学员:") 53 for s in self.studentlist: 54 print(s.sname) 55 return 56 57 def addteacher(self): 58 print("招募讲师......") 59 name=input("老师姓名:") 60 newteacher=Teacher.Teacher(name) 61 self.teacherlist.append(newteacher) 62 print("%s 老师到岗。"%newteacher.tname) 63 newteacher.store() 64 self.store() 65 return 66 67 def addcourse(self): 68 print("开设课程......") 69 name=input("课程名称:") 70 price=input("课程价格:") 71 newcourse=Course.Course(name,price) 72 self.courselist.append(newcourse) 73 print("%s 课程开设完毕。"%newcourse.cname) 74 newcourse.store() 75 self.store() 76 return 77 78 def addgroup(self): 79 print("开设班级......") 80 csname=input("课程名字:") 81 name=input("班级名字:") 82 maxnum=input("班级最多人数:") 83 teachername=input("带班老师:") 84 85 fddir = os.path.dirname(os.path.dirname(__file__)) 86 with open(‘%s\dumpfile\\teacher\%s‘ % (fddir, teachername), ‘rb‘) as fd: 87 teacherobj=pickle.load(fd) 88 89 cnamelist=[] 90 for c in self.courselist: 91 cnamelist.append(c.cname) 92 if csname in cnamelist: 93 tnamelist=[] 94 for t in self.teacherlist: 95 tnamelist.append(t.tname) 96 if teachername in tnamelist: 97 newgroup=Group.Group(name,maxnum,teachername) 98 self.grouplist.append(newgroup) 99 newgroup.store() 100 teacherobj.grouplist.append(newgroup) 101 teacherobj.store() 102 else: 103 print("没有你要的带班老师,班级开设失败。") 104 else: 105 print("本校没有你输入的课程,班级开设失败") 106 self.store() 107 return
1 #Course类 2 import os 3 import pickle 4 5 class Course(object): 6 def __init__(self,name,price): 7 self.cname=name 8 self.cprice=price 9 self.cgroup=[] 10 return 11 12 def store(self): 13 fddir = os.path.dirname(os.path.dirname(__file__)) 14 with open(‘%s\dumpfile\course\%s‘ % (fddir, self.cname), ‘wb‘) as fd: 15 pickle.dump(self, fd) 16 return
1 #Group类 2 import os 3 import pickle 4 5 class Group(object): 6 def __init__(self,name,maxnumber,teachername): 7 self.gname=name 8 self.gmaxstudent=maxnumber 9 self.gteacher=teachername 10 self.gstudentdict={}#学生姓名:成绩 11 return 12 13 def store(self): 14 fddir = os.path.dirname(os.path.dirname(__file__)) 15 with open(‘%s\dumpfile\group\%s‘ % (fddir, self.gname), ‘wb‘) as fd: 16 pickle.dump(self, fd) 17 return 18 19 def addstudent(self,sname): 20 self.gstudentdict[sname]=0 21 return
1 #Teacher类 2 import os 3 import pickle 4 5 class Teacher(object): 6 def __init__(self,name=‘‘): 7 self.tname=name 8 self.grouplist=[]#放的是group对象,在school建立班级的时候关联上去的 9 return 10 11 def store(self): 12 fddir = os.path.dirname(os.path.dirname(__file__)) 13 with open(‘%s\dumpfile\\teacher\%s‘ % (fddir, self.tname), ‘wb‘) as fd: 14 pickle.dump(self, fd) 15 return 16 17 def checkgroupinfo(self): 18 for g in self.grouplist: 19 print("%s 班级有以下学生:"%g.gname) 20 for key in g.gstudentdict.keys(): 21 print(key) 22 return self 23 24 def changescore(self): 25 gname=input("请输入要登记成绩的班级名字:") 26 for g in self.grouplist: 27 if gname==g.gname: 28 flag=True 29 while flag: 30 sname=input("请输入学生姓名:") 31 sscore=input("请输入学生成绩:") 32 if sname in g.gstudentdict.keys(): 33 g.gstudentdict[sname]=sscore 34 else: 35 print("没有这个学生。") 36 cont=input("继续登记吗?y/n") 37 if cont==n: 38 flag=False 39 return self
1 #Student类 2 import os 3 import pickle 4 from . import Group 5 from . import School 6 from . import Course 7 from . import Teacher 8 9 class Student(object): 10 def __init__(self): 11 self.sname=‘‘ 12 self.schname=‘‘ 13 self.scoursedic={} 14 self.sgrouplist=[] 15 return 16 17 def store(self): 18 fddir = os.path.dirname(os.path.dirname(__file__)) 19 with open(‘%s\dumpfile\student\%s‘ % (fddir, self.sname), ‘wb‘) as fd: 20 pickle.dump(self, fd) 21 return 22 23 def reg(self): 24 name=input("请输入您的名字:") 25 schoolname=input("请输入学校名字:") 26 self.sname = name 27 self.schname = schoolname 28 29 fddir=os.path.dirname(os.path.dirname(__file__)) 30 with open(‘%s\dumpfile\school\%s‘%(fddir,schoolname),‘rb‘) as fd: 31 schoolobj=pickle.load(fd) 32 schoolobj.studentlist.append(self) 33 schoolobj.store() 34 self.store() 35 print(‘注册成功‘) 36 return self 37 38 def addgroup(self): 39 fddir = os.path.dirname(os.path.dirname(__file__)) 40 with open(‘%s\dumpfile\school\%s‘ % (fddir, self.schname), ‘rb‘) as fd: 41 schoolobj = pickle.load(fd) 42 schoolobj.getcourse() 43 schoolobj.getgroup() 44 choosecourse=input("输入你要上的课程名字:") 45 choosegroup=input("输入你要上的班级名字:") 46 47 with open(‘%s\dumpfile\group\%s‘%(fddir,choosegroup),‘rb‘) as fd1: 48 groupobj=pickle.load(fd1) 49 if len(groupobj.gstudentdict)<int(groupobj.gmaxstudent): 50 self.scoursedic[choosecourse]=‘未缴费‘ 51 groupobj.addstudent(self.sname) 52 groupobj.store() 53 print("报名成功,未缴费。") 54 else: 55 print("该班满员,不能报名。") 56 self.store() 57 return self 58 59 def scorecheck(self): 60 fddir = os.path.dirname(os.path.dirname(__file__)) 61 for i in self.sgrouplist: 62 with open(‘%s\dumpfile\group\%s‘ % (fddir, i), ‘rb‘) as fd: 63 groupobj=pickle.load(fd) 64 print("%s 课程的成绩是 %d"%(i,groupobj.gstudentdict[self.sname])) 65 return self 66 67 def pay(self): 68 for key,ifpay in self.scoursedic.items(): 69 if ifpay==‘未缴费‘: 70 fddir = os.path.dirname(os.path.dirname(__file__)) 71 with open(‘%s\dumpfile\course\%s‘ % (fddir, key), ‘rb‘) as fd: 72 courseobj=pickle.load(fd) 73 price=courseobj.cprice 74 print("课程 %s 需要缴费 %s 元"%(key,price)) 75 yorno=input("是否缴费?y/n") 76 if yorno==‘y‘: 77 self.scoursedic[key]=‘已缴费‘ 78 print("已缴费") 79 self.store() 80 return self
时间: 2024-10-20 11:55:54