1. 程式人生 > >學員選課系統(進階)

學員選課系統(進階)

從“學生選課系統” 這幾個字就可以看出來,我們最核心的功能其實只有 選課。
角色:
    學生、管理員
功能:
    登陸 : 管理員和學生都可以登陸,且登陸之後可以自動區分身份
    選課 : 學生可以自由的為自己選擇課程
    建立使用者 : 選課系統是面向本校學生的,因此所有的使用者都應該由管理員完成
    檢視選課情況 :每個學生可以檢視自己的選課情況,而管理員應該可以檢視所有學生的資訊
工作流程:
    登陸 :使用者輸入使用者名稱和密碼
    判斷身份 :在登陸成果的時候應該可以直接判斷出使用者的身份 是學生還是管理員

    學生使用者 :對於學生使用者來說,登陸之後有三個功能
        1、檢視所有課程
        2、選擇課程
        3、檢視所選課程
        4、退出程式
    管理員使用者:管理員使用者除了可以做一些檢視功能之外,還有很多建立工作
        1、建立課程
        2、建立學生學生賬號
        3、檢視所有課程
        4、檢視所有學生
        5、檢視所有學生的選課情況
        6、退出程式
'''直接執行即可,注意細節'''
'''注意管理員為自動建立,user=admin, pass=123,直接登入即可'''

import os
import json


class User:
    def __init__(self, name):  # 傳入使用者名稱
        self.name = name

    dic = {"檢視課程": 'User_see', '選擇課程': 'Choice_course', "檢視所選課程": 'Selected', "退出": 'Exit'}

    def User_see(self):  # 檢視所有課程
        with open(
'class_all.txt', encoding='utf-8') as file: tmp = {} # 通過字典新增值 for index, i in enumerate(file.read().split('|')[:-1], 1): print(index, i) tmp.setdefault(str(index), i) # 把得到的結果放入到tmp中 return tmp # 把返回值return出去 def Choice_course(self): # 選擇課程 tmp
= self.User_see() # 呼叫檢視課程的方法顯示 Choice = input('請輸入你選擇的課程式號') if Choice in tmp: # 判斷選的課在不在列表裡 with open('choice_class.txt', encoding='utf-8') as file: chice_class = json.load(file) # 把chice_class.txt序列化出來,得到字典 if chice_class.get(self.name): # 判斷使用者和課程裡有沒有那個使用者 chice_class.get(self.name).append(tmp[Choice]) # 新增到字典中 else: chice_class.setdefault(self.name, [tmp[Choice]]) with open('choice_class.txt', encoding='utf-8', mode='w') as file: json.dump(chice_class, file, ensure_ascii=False) # 再把chice_class值放入到檔案中 else: print('不在範圍內') def Selected(self): # 檢視所選課程 with open('choice_class.txt', encoding='utf-8') as file: user_course = json.load(file) # 把chice_class.txt序列化出來,得到字典 print(user_course.get(self.name)) def Exit(self): # 退出 exit('歡迎下次再來。。。') class Admin: dic = {"建立課程": 'Create_course', '建立學生學生賬號': 'Create_user', "建立講師賬號": 'Create_teacher', "為講師指定班級": "Appoint_class", "為學生指定班級": "Student_class", "檢視所有課程": 'Select_all_course', "建立班級": "Create_class", "檢視所有學生": 'Select_user_all', "檢視所有學生的選課情況": 'Select_course_user', '退出程式': 'Exit'} def __init__(self, name): self.name = name def Create_course(self): # 建立課程 Choice = input('輸入建立課程名稱') with open('class_all.txt', encoding='utf-8', mode='a') as file: file.write(Choice + '|') print('建立{}課程成功'.format(Choice)) def Create_user(self): # 建立學生賬號 stu_name = input('請輸入學生賬號:').strip() stu_pwd = input('請輸入學生密碼:').strip() with open('user.txt', encoding='utf-8', mode='a') as file: file.write('\n{}|{}|student'.format(stu_name, stu_pwd)) print('{}同學建立成功'.format(stu_name)) def Create_teacher(self): # 建立講師賬號 stu_name = input('請輸入講師賬號:').strip() stu_pwd = input('請輸入講師密碼:').strip() with open('user.txt', encoding='utf-8', mode='a') as file: file.write('\n{}|{}|Teacher'.format(stu_name, stu_pwd)) with open('teacher.txt', encoding='utf-8', mode='a') as file: # 建立完成後把name寫入teacher.txt檔案中 file.write(stu_name + '|') print('{}講師建立成功'.format(stu_name)) def Appoint_class(self): # 為講師指定班級 with open('Create_class.txt', encoding='utf-8') as file: p1 = json.load(file) # 把班級檔案序列化出來 tmp1 = [] if os.path.exists('teacher.txt'): # 判斷檔案是否存在 with open('teacher.txt', encoding='utf-8', mode='r') as file: for i in file.readlines(): tmp1 = i.strip().split('|')[:-1] # 把值新增到tmp1列表中 for i, index in enumerate(tmp1, 1): # 顯示老師名稱 print(i, index) for i, index in enumerate(p1, 1): # 顯示班級名稱 print(i, index) choice_tracher = int(input('輸入你選擇的講師')) - 1 with open('teacher_class.txt', encoding='utf-8', mode='r') as file: l1 = json.load(file) if p1 == []: # 判斷是否有班級存在 print('如果要新增則請先建立班級') else: choice_class = int(input('輸入你選擇的班級')) - 1 if l1.get(tmp1[choice_tracher]): # 判斷l1字典中是否為新使用者 l1.get(tmp1[choice_tracher]).append(p1[choice_class]) # 追加到後面list中 else: l1.setdefault(tmp1[choice_tracher], [p1[choice_class]]) # 單個key value l1[tmp1[choice_tracher]] = list(set(l1[tmp1[choice_tracher]])) # 去重 with open('teacher_class.txt', encoding='utf-8', mode='w') as file: json.dump(l1, file, ensure_ascii=False) # 寫入檔案 print('{}老師指定{}成功'.format(tmp1[choice_tracher], p1[choice_class])) else: print('清先建立講師') def Student_class(self): # 為學生指定班級 p1 = self.Select_user_all() # 呼叫Select_user_all方法,查看出有哪些學生,p1為列表 if p1 == []: # 是否有學生存在 print('清先建立學生後在來新增') else: if os.path.exists('Create_class.txt'): print('所有班級為:') with open('Create_class.txt', encoding='utf-8') as file: tmp1 = json.load(file) for i, index in enumerate(tmp1, 1): print(i, index) user = int(input('輸入你新增的學生序號')) - 1 student = int(input('輸入你新增的班級序號')) - 1 with open('student_class.txt', encoding='utf-8', mode='r') as file: l1 = json.load(file) if l1.get(tmp1[student]): # 判斷l1中key是否有此使用者 l1.get(tmp1[student]).append(p1[user]) else: l1.setdefault(tmp1[student], [p1[user]]) l1[tmp1[student]] = list(set(l1[tmp1[student]])) # 去重 with open('student_class.txt', encoding='utf-8', mode='w') as file: json.dump(l1, file, ensure_ascii=False) print('{}新增{}學生成功'.format(tmp1[student], p1[user])) else: print('請先建立班級後再來新增') def Create_class(self): # 建立班級 Test_class = input('輸入你建立班級的名稱如(-年級-班)') if os.path.exists('Create_class.txt'): # 判斷檔案是否存在 with open('Create_class.txt', encoding='utf-8') as file: p1 = json.load(file) p1.append(Test_class) # 新增班級 else: with open('Create_class.txt', encoding='utf-8', mode='w') as file: file.write('[]') # 建立檔案,新增 [] with open('Create_class.txt', encoding='utf-8') as file: p1 = json.load(file) p1.append(Test_class) with open('Create_class.txt', encoding='utf-8', mode='w') as file: json.dump(p1, file, ensure_ascii=False) # 寫入檔案 print('建立{}成功'.format(Test_class)) return p1 # 把列表返回出去 def Select_all_course(self): # 檢視所有課程 print('檢視的課程為') with open('class_all.txt', encoding='utf-8') as f: tmp = {} for index, i in enumerate(f.read().split('|')[:-1], 1): print(index, i, ) tmp.setdefault(str(index), i) return tmp # 方便後面方法使用 def Select_user_all(self): # 檢視所有學生 student = [] with open('user.txt', encoding='utf-8') as file: print('學生使用者為:') for i, index in enumerate(file.readlines()): if index.strip().split('|')[-1] == 'student': # 取出學生 print(i, index.strip().split('|')[0]) student.append(index.strip().split('|')[0]) return student def Select_course_user(self): # 檢視所有學生的選課情況 if os.path.exists('choice_class.txt'): with open('choice_class.txt', encoding='utf-8') as file: for i in file.readlines(): # 列出檔案的每行類容 print(i) else: print('清先登入學生賬號新增課程後檢視') def Exit(self): # 退出 exit('歡迎下次再來。。。') class Teacher: def __init__(self, name): self.name = name dic = {'檢視所有課程': 'Select_choice', '檢視所教班級': 'Select_student', '檢視班級中的學生': 'Select_class_student', '退出': 'Exit'} def Select_choice(self): # 檢視所有課程 if os.path.exists('class_all.txt'): # 判斷課程是否存在 with open('class_all.txt', encoding='utf-8') as file: for i in file.readlines(): print(i.strip().split('|')[:-1]) else: print('請先登入管理員建立課程') def Select_student(self): # 檢視所教班級 if os.path.exists('teacher_class.txt'): with open('teacher_class.txt', encoding='utf-8') as file: tmp1 = json.load(file).get(self.name) print(tmp1) return tmp1 else: print('目前暫無,請登入管理員新增') def Select_class_student(self): # 檢視班級中的學生 if os.path.exists('teacher_class.txt') and os.path.exists('student_class.txt'): tmp = self.Select_student() # 先呼叫Select_student方法檢視,tmp得到返回值 with open('student_class.txt', encoding='utf-8') as file: student = json.load(file) if tmp == None: print('當前老師班級沒有學生') else: for i in tmp: if student.get(i): print(i, student[i]) else: print('目前暫無,請登入管理員新增') def Exit(self): # 退出 exit('歡迎下次再來。。。') class login_Test: # 判斷登入賬戶 print('歡迎來到學員管理系統') def __init__(self, username, password): self.username = username self.password = password def login(self): with open('user.txt', encoding='utf-8') as file: for i in file.readlines(): # 取出每一行 '''判斷使用者密碼是否正確''' if self.username == i.strip().split('|')[0] and self.password == i.strip().split('|')[1]: if i.strip().split('|')[-1] == 'admin': return 'admin' elif i.strip().split('|')[-1] == 'student': return 'student' elif i.strip().split('|')[-1] == 'Teacher': return 'teacher' else: print('使用者名稱密碼錯誤') def log_status(): # 方便後面呼叫 tmp = [] username = input('請輸入使用者名稱') password = input('請輸入密碼') p1 = login_Test(username, password) tmp.append(username) tmp.append(p1.login()) return tmp def func(class_func): tmp = {} while 1: for index, i in enumerate(class_func.dic, 1): print(index, i) tmp.setdefault(str(index), class_func.dic[i]) p2 = input("輸入你選擇的序號") getattr(class_func, tmp[p2])() '''用於檔案建立''' if os.path.exists('user.txt') == False: with open('user.txt', encoding='utf-8', mode='w') as file: file.write('admin|123|admin') if os.path.exists('teacher_class.txt') == False: with open('teacher_class.txt', encoding='utf-8', mode='w') as file: file.write('{}') if os.path.exists('choice_class.txt') == False: with open('choice_class.txt', encoding='utf-8', mode='w') as file: file.write('{}') if os.path.exists('Create_class.txt') == False: with open('Create_class.txt', encoding='utf-8', mode='w') as file: file.write('[]') if os.path.exists('class_all.txt') == False: with open('class_all.txt', encoding='utf-8', mode='w') as file: pass if os.path.exists('student_class.txt') == False: with open('student_class.txt', encoding='utf-8', mode='w') as file: file.write('{}') login_status = log_status() # class_all.txt while True: if login_status[1] == 'student': p1 = User(login_status[0]) func(p1) elif login_status[1] == 'admin': p1 = Admin(login_status[0]) func(p1) elif login_status[1] == 'teacher': p1 = Teacher(login_status[0]) func(p1) else: break