Python 大作业4:选课系统
python"># 作业详细要求见:# 景女神的blog https://www.cnblogs.com/Eva-J/articles/9235899.html
import pickle
import sys
class User:
def __init__(self, username, password, permission):
self.username = username
self.password = password
self.permission = permission
def view_all_course(self):
with open(COURSE_PATH, mode=\'rb\') as file_handler:
for i in range(1, INF):
try:
ret = pickle.load(file_handler)
print(i, ret.name, ret.cycle, ret.teacher)
except EOFError:
print(\'-\' * 10)
break
@staticmethod
def my_exit():
global Login_user
Login_user = False
REGISTER_PATH = \'register\'
COURSE_PATH = \'course\'
CHOOSE_PATH = \'choose\'
INF = 0x7FFFFFFF
Login_user = User(None, None, None)
class Course:
def __init__(self, name, cycle, teacher):
self.name = name
self.cycle = cycle
self.teacher = teacher
@staticmethod
def Num():
num = 0
with open(COURSE_PATH, mode=\'rb\') as file_handler:
for i in range(INF):
try:
pickle.load(file_handler)
num += 1
except EOFError:
return num
class Student(User):
Ins = [(\'查看所有课程\', \'view_all_course\'), (\'选择课程\', \'choose_course\'),
(\'查看所选课程\', \'view_chosen_course\'), (\'退出程序\', \'my_exit\')]
def __init__(self, user, password, permission):
super().__init__(user, password, permission)
self.course = []
def choose_course(self):
while True:
self.view_all_course()
choose_course = input(\'choose ur course (input Q or q to exit). >>>\')
if choose_course.isdecimal():
if int(choose_course) > 0 and int(choose_course) <= Course.Num():
with open(COURSE_PATH, mode=\'rb\') as file_handler:
for i in range(int(choose_course)):
ret = pickle.load(file_handler)
self.course.append(ret)
elif choose_course.upper() == \'Q\':
with open(CHOOSE_PATH, encoding=\'utf-8\', mode=\'a\') as file_handler:
file_handler.write(self.username)
for c in self.course:
file_handler.write(\' \' + c.name)
file_handler.write(\'\n\')
print(\'Save success.\n\' + 10 * \'-\')
return
else:
print(\'Illegal input.\n\' + 10 * \'-\')
def view_chosen_course(self):
with open(CHOOSE_PATH, encoding=\'utf-8\', mode=\'r\') as file_handler:
flag = False
for line in file_handler:
if self.username in line:
print(line.strip())
print(10 * \'-\')
flag = True
if flag == False: print(\'No course.\n\' + 10 * \'-\')
@staticmethod
def get_student_list():
student_list = []
with open(REGISTER_PATH, mode=\'rb\') as file_handler:
for i in range(INF):
try:
ret = pickle.load(file_handler)
if ret.permission == \'student\':
student_list.append(ret)
except EOFError:
return student_list
class Administrator(User):
Ins = [(\'创建课程\', \'new_course\'), (\'创建学生学生账号\', \'new_stu\'), (\'查看所有课程\', \'view_all_course\'),
(\'查看所有学生\', \'view_all_stu\'), (\'查看所有学生选课情况\', \'view_course_selection\'), (\'退出程序\', \'my_exit\')]
def new_course(self):
course_name = input(\'new course name. >>>\')
course_cycle = input(\'new course cycle. >>>\')
course_teacher = input(\'new course teacher. >>>\')
new_cour = Course(course_name, course_cycle, course_teacher)
with open(COURSE_PATH, mode=\'ab\') as file_handler:
pickle.dump(new_cour, file_handler)
print(\'-\' * 10)
@staticmethod
def new_stu():
new_user(\'Student\')
def view_all_stu(self):
with open(REGISTER_PATH, mode=\'rb\') as file_handler:
for i in range(INF):
try:
ret = pickle.load(file_handler)
if ret.permission == \'Student\':
print(i, ret.username)
except EOFError:
print(\'-\' * 10)
break
@staticmethod
def view_course_selection():
with open(CHOOSE_PATH, encoding=\'utf-8\', mode=\'r\') as file_handler:
for line in file_handler:
print(line.strip())
print(\'-\' * 10)
def check_admin():
with open(REGISTER_PATH, mode=\'rb\') as file_handler:
for i in range(INF):
try:
ret = pickle.load(file_handler)
if ret.permission == \'Administrator\':
return True
except EOFError:
return False
def new_user(permission):
while True:
flag = True
user_username = input(\'new student username. >>>\')
user_password = input(\'new student password. >>>\')
with open(REGISTER_PATH, mode=\'rb\') as file_handler:
for i in range(INF):
try:
ret = pickle.load(file_handler)
if ret.username == user_username:
print(\'Username Exist.\')
print(\'-\' * 10)
flag = False
except EOFError:
break
if flag == True:
break
if permission == \'Student\':
new_u = Student(user_username, user_password, permission)
with open(REGISTER_PATH, mode=\'ab\') as file_handler:
pickle.dump(new_u, file_handler)
print(\'Create success.\')
print(\'-\' * 10)
elif permission == \'Administrator\':
new_u = Administrator(user_username, user_password, permission)
with open(REGISTER_PATH, mode=\'ab\') as file_handler:
pickle.dump(new_u, file_handler)
print(\'Create success.\')
print(\'-\' * 10)
else:
return False
def login():
for i in range(3):
username = input(\'username:\')
password = input(\'password:\')
with open(REGISTER_PATH, mode=\'rb\') as file_handler:
for i in range(INF):
try:
ret = pickle.load(file_handler)
if ret.username == username and ret.password == password:
global Login_user
Login_user = ret
print(f\'welcome {Login_user.username} Permission: {Login_user.permission}\')
print(\'-\' * 10)
return True
except EOFError:
break
print(\'Incorrect account or password.\')
print(\'-\' * 10)
return False
def run():
if check_admin() == False:
print(\'NO found Administrator, we have to create a new Administrator.\')
new_user(\'Administrator\')
if not login():
return
if hasattr(sys.modules[\'__main__\'], Login_user.permission):
if callable(getattr(sys.modules[\'__main__\'], Login_user.permission)):
while Login_user:
for i, choose in enumerate(getattr(sys.modules[\'__main__\'], Login_user.permission).Ins, 1):
print(f\'{i}、{choose[0]}\')
ins = input(\'choose ins. >>>\')
if hasattr(Login_user, getattr(sys.modules[\'__main__\'], Login_user.permission).Ins[int(ins) - 1][1]):
if callable(getattr(Login_user,
getattr(sys.modules[\'__main__\'], Login_user.permission).Ins[int(ins) - 1][1])):
getattr(Login_user,
getattr(sys.modules[\'__main__\'], Login_user.permission).Ins[int(ins) - 1][1])()
return
if __name__ == \'__main__\':
run()
以上是 Python 大作业4:选课系统 的全部内容, 来源链接: utcz.com/z/389319.html