1. 程式人生 > >python--學校管理系統(在學校接口上,增加了老師的管理接口和學生的管理接口)

python--學校管理系統(在學校接口上,增加了老師的管理接口和學生的管理接口)

建立 load style inpu core open .com 只需要 繼續

和上一篇隨筆一樣的要求,完善了一下老師接口和學生接口。

搞清楚了一件事情,就是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

python--學校管理系統(在學校接口上,增加了老師的管理接口和學生的管理接口)