去年有个朋友在学习python的过程中,培训班老师给他们出了一道课后练习,用类来写一个管理系统的题目,里面的主体部分已经给出了,让他们自己补充里面的内容,主要涉及到一些函数功能以及使用self来调用类中的功能,估计是上课没听明白,他将题目发我看了下。我当时写出来后就一直扔在电脑里面,最近整理电脑的时候,顺便将这段放出来。感觉这个代码还可以优化下,可以让Manage这个类去继承Student的内容,不过我自己也懒得去改了。
#!/anaconda3/envs/FEALPy/bin python3.7
# -*- coding: utf-8 -*-
# ---
# @File: 学生管理系统.py
# 
# ---
# 需求:
# 实现一个学生管理系统
# 命令行实现
#     增加学生信息
#     显示学生信息
#     删除学生信息
#     退出系统
# 数据需要持久储存
# 关于学生
# 学生需要有姓名\学号\分数这三个属性
# 分析:
# 可以分为 学生类/管理系统类
class Student():
    def __init__(self,id,name,score):
        self.id = id
        self.name = name
        self.score = score
    #
    def __str__(self):#魔法方法:使用print函数运行的时候,会显示这个retuen
        return f'{self.id},{self.name},{self.score}'
#obj = Student('1','bull','90')
#print(obj)
class Manage():
    student_dict = {}
    def main_func(self):        
        def add_student():
            id = input('请输入学号')
            name = input('请输入姓名')
            score = input('请输入分数')
            student_obj = Student(id, name, score)
            print(student_obj)
            self.student_dict[id]=student_obj.__str__()
            print(self.student_dict)
        def del_student():
            id = input('请输入要删除数据的学号')
            del self.student_dict[id]
            print(self.student_dict)
            with open('data.txt','w') as f:
                for line in self.student_dict:
                    fline = line + ' ' + self.student_dict[line] + '\n'
                    f.write(fline)
        def show_students():
            pass
        def save_data():
            with open('data.txt','a+') as f:
                for line in self.student_dict:
                    print(line)
                    print(self.student_dict[line])
                    fline = line + ' ' + self.student_dict[line] + '\n'
                    f.write(fline)
        def load_data():
            # 读取文件数据到变量中
            with open('data.txt','r') as f:
                for line in f.readlines():
                    idnum = line.split()[0]
                    mess = line.split()[1]
                    self.student_dict[idnum] = mess
            print(self.student_dict)
        def menu():
            print('增加学员信息,请输入1')
            print('显示学员信息,请输入2')
            print('删除学员信息,请输入3')
            print('退出系统,请输入0')
        menu()
        code = int(input('请输入要执行的操作: '))
        while code != 0:
            if code == 1:
                # 增加学员信息
                #load_data()
                add_student()
                save_data()
                menu()
                code = int(input('请输入要执行的操作: '))
            elif code == 2:
                # 显示学员信息
                load_data()
                #show_students()
                menu()
                code = int(input('请输入要执行的操作: '))
            elif code == 3:
                # 删除学员信息
                load_data()
                del_student()
                menu()
                code = int(input('请输入要执行的操作: '))
            else:
                # 退出系统
                break
def main():
    std_sys = Manage()
    std_sys.main_func()
if __name__ == "__main__":
    main()
