1. 程式人生 > >學生選課系統(模擬)

學生選課系統(模擬)

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

學生使用者 :對於學生使用者來說,登陸之後有三個功能
1、檢視所有課程

2、選擇課程
3、檢視所選課程
4、退出程式
管理員使用者:管理員使用者除了可以做一些檢視功能之外,還有很多建立工作
1、建立課程
2、建立學生學生賬號
3、檢視所有課程
4、檢視所有學生
5、檢視所有學生的選課情況
6、退出程式
注意:
'''程式碼還有待完善,寫了兩遍emmmm'''
'''注意:在執行前先建立一個user.txt檔案用於存放賬號,還得進入裡面編輯一行:admin|123|admin,
這個用作後面的管理員與使用者區分。
還得建立兩個空檔案:class_all.txt 用於存放所有課程名

choice_class.txt 用於存放使用者所選課程,注意還得在裡面新增一個 {} 用於序列化
注意管理員為:admin pass:123'''
import os
import json


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

    def User_see(self):  # 檢視所有課程
        with open('class_all.txt', encoding='utf-8') as file:
            tmp 
= {} # 通過字典新增值 for index, i in enumerate(file.read().split('|'), 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() def Show(self): # see_all 這個字典是功能名和要執行的函式 see_all = {"檢視課程": self.User_see, '選擇課程': self.Choice_course, "檢視所選課程": self.Selected, "退出": self.Exit} while True: tmp = {} # 修改see_all這個字典,保留索引和函式 for index, i in enumerate(see_all, 1): print(index, i) tmp[str(index)] = see_all[i] Choice = input('請輸入你選擇') # 根據不同的選擇執行不同的函式 if Choice in tmp: tmp[Choice]() class Admin: 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 + '|') def Create_user(self): # 建立學生賬號 stu_name = input('請輸入學生賬號:').strip() stu_pwd = input('請輸入學生密碼:').strip() # stu_pwd_md5 = get_pwd(stu_pwd) with open('user.txt', encoding='utf-8', mode='a') as file: file.write('\n{}|{}'.format(stu_name, stu_pwd)) def Select_all_course(self): # 檢視所有課程 with open('class_all.txt', encoding='utf-8') as f: tmp = {} for index, i in enumerate(f.read().split('|'), 1): print(index, i) tmp.setdefault(str(index), i) return tmp def Select_user_all(self): # 檢視所有學生 with open('user.txt', encoding='utf-8') as file: for i in file.readlines(): print(i.strip()) def Select_course_user(self): # 檢視所有學生的選課情況 with open('choice_class.txt', encoding='utf-8') as file: for i in file.readlines(): # 列出檔案的每行類容 print(i) def Exit(self): # 退出 exit() def Show(self): # 宗上所述 see_all = {"建立課程": self.Create_course, '建立學生學生賬號': self.Create_user, "檢視所有課程": self.Select_all_course, "檢視所有學生": self.Select_user_all, "檢視所有學生的選課情況": self.Select_course_user, '退出程式': self.Exit} while True: tmp = {} for index, i in enumerate(see_all, 1): print(index, i) tmp[str(index)] = see_all[i] Choice = input('請輸入你選擇') if Choice in tmp: tmp[Choice]() user = {'user': None, 'status': False} def login(): print('歡迎來到學員管理系統') flag = True while flag: if user['status']: print('你已經登入') break else: username = input('輸入使用者名稱') password = input('輸入密碼') with open('user.txt', encoding='utf-8') as f1: for i in f1: p1 = i.strip().split('|') p2 = len(p1) if username == p1[0] and password == p1[1]: if p2 == 2: # 用於判斷是否為普通使用者 user['user'] = username user['status'] = True flag = False return username # 返回登入名 elif p2 == 3: # 判斷是否為管理員 user['user'] = username user['status'] = True flag = False return 2 # 返回2 用於下面判斷 else: print('輸入的使用者名稱或者密碼錯誤,請再次輸入') continue ret = login() while True: if ret == 2: # 執行管理員class p1 = Admin('admin') p1.Show() else: # 執行使用者class p2 = User(ret) p2.Show()