Python簡易學生管理系統

2023-04-08 12:00:21

目錄結構:

 

 1、 學生檔案 student.py

# 學生類
class Student(object):
    # 存放學生資訊
    student_info = {}
    # 學生初始化方法
    def __init__(self,id,name,addr,tel):
        self.id = id
        self.name = name
        self.addr = addr
        self.tel = tel
        # 將學生的資訊新增到學生資訊字典裡
        self.student_info["學號"] = self.id
        self.student_info["姓名"] = self.name
        self.student_info["地址"] = self.addr
        self.student_info["電話"] = self.tel

2、 管理系統檔案 manager.py

import json
import student
# 管理系統類
class Manager(object):

    # 展示所有學生資訊的方法
    def show_all_student(self):
        with open("students_info.json", mode="r", encoding="utf8") as f:
            content = json.loads(f.read())
            if content:
                print("所有的學生資訊如下:")
                print("#######################################")
                for stu in content:
                    print(stu)
                print("#######################################")
            elif len(content) == 0:
                print("Sorry! 一個學生都沒有!")

    # 新增學生的方法
    def add_student(self):
        print("~~~~~~~~~~~~~~~~~~~~將要開始新增學生資訊~~~~~~~~~~~~~~~~~~~~~")
        id = input("請輸入學號:")
        name = input("請輸入姓名:")
        addr = input("請輸入地址:")
        tel = input("請輸入電話:")
        new_student = student.Student(id, name, addr, tel)
        with open("students_info.json", mode="r", encoding="utf8") as f1:
            content = json.loads(f1.read())

        new_content = list(content)
        new_content.append(new_student.student_info)
        json_content = json.dumps(new_content)

        with open("students_info.json",mode="w",encoding="utf8") as f2:
            f2.write(json_content)
        print("~~~~~~~~~~~~~~~~~~~~!!!新增完成!!!~~~~~~~~~~~~~~~~~~~~~")


    # 刪除學生的方法
    def delete_student(self):
        print("~~~~~~~~~~~~~~~~~~~~將要開始新增學生資訊~~~~~~~~~~~~~~~~~~~~~")
        id = input("請輸入要刪除的學生的學號:")
        with open("students_info.json",mode="r",encoding="utf8") as f:
            content = list(json.loads(f.read()))
            for item in content:
                if item["學號"] == id:
                    content.remove(item)
                    json_content = json.dumps(content)
                    with open("students_info.json", mode="w", encoding="utf8") as f2:
                        f2.write(json_content)
                    print("~~~~~~~~~~~~~~~~~~~~!!!刪除成功!!!~~~~~~~~~~~~~~~~~~~~~")
                    return
            print("輸入的學號不存在!!!")


    # 修改學生的方法
    def update_student(self):
        print("~~~~~~~~~~~~~~~~~~~~開始修改學生資訊~~~~~~~~~~~~~~~~~~~~~")
        with open("students_info.json", mode="r", encoding="utf8") as f:
            text = list(json.loads(f.read()))

        delete_number = input("請輸入要修改的學生學號:")
        for i in text:
            if i["學號"] == delete_number:
                new_addr = input("請輸入修改後的地址:")
                new_tel = input("請輸入修改後的電話:")
                i["地址"] = new_addr
                i["電話"] = new_tel
                json_text = json.dumps(text)
                with open("students_info.json", mode="w", encoding="utf8") as f2:
                    f2.write(json_text)
                print("~~~~~~~~~~~~~~~~~~~~修改成功~~~~~~~~~~~~~~~~~~~~~")
                return
        print("輸入的學號不存在!!!!!!!!!!!!!")


    # 系統啟動入口方法
    def system_start(self):
        while True:
            print("########### 歡迎來到學生管理系統 ############")
            print("################ 請登入系統 ################")
            user_name = input("請輸入使用者名稱: ")
            pass_word = input("  請輸入密碼: ")
            if user_name == "zyh" and pass_word == "123456":
                print("################ 登陸成功!################")
                break
            else:
                print("登入資訊錯誤,請重新登入!")

        while True:
            print("################ 你已經進入學生管理系統 ################")
            print("###################### 請選擇功能 ######################")
            print("1. 展示所有學生資訊")
            print("2. 新增學生")
            print("3. 刪除學生")
            print("4. 修改學生")
            print("5. 退出系統")
            print("#######################################################")

            while True:
                choice = int(input("請輸入功能對應的序號(1/2/3/4/5/6): "))
                if choice == 1:
                    # print("呼叫展示所有學生資訊的方法")
                    self.show_all_student()

                    break
                elif choice == 2:
                    # print("呼叫新增學生的方法")
                    self.add_student()

                    break
                elif choice == 3:
                    # print("呼叫刪除學生的方法")
                    self.delete_student()

                    break
                elif choice == 4:
                    # print("呼叫修改學生的方法")
                    self.update_student()

                    break
                elif choice == 5:
                    print("退出學生管理系統!!!!!!!!!!!")
                    return
                else:
                    print("輸入資訊不合法,請重新輸入!")

 

3、 程式入口檔案 main.py

import manager



if __name__ == '__main__':

    manager_sys = manager.Manager()
    manager_sys.system_start()

 

4、 學生資訊檔案  students_info.json

[
  {"學號": "0001", "姓名": "張三", "地址": "北京", "電話": "13244453359"},
  {"學號": "0002", "姓名": "張三2", "地址": "北京", "電話": "13244453359"},
  {"學號": "0003", "姓名": "張三3", "地址": "北京", "電話": "13244453359"},
  {"學號": "0004", "姓名": "張三4", "地址": "北京", "電話": "13244453359"}
]