學生選課系統(模擬)


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

學生用戶 :對於學生用戶來說,登陸之后有三個功能
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()

 

 
       


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM