面向对象的学员管理系统

面向对象的学员管理系统

main.py

from managerSystem import *

if __name__ == '__main__':
    student_manager = StudentManager()
    student_manager.run()

student.py

class Student(object):
    def __init__(self, name, gender, tel):
        self.name = name
        self.gender = gender
        self.tel = tel

    def __str__(self):
        return f'{self.name}, {self.gender}, {self.tel}'

managerSystem.py

from student import *

class StudentManager(object):
    def __init__(self):
        self.student_list = []


    def run(self):
        """程序入口函数"""
        self.load_student()
        while True:
            """显示功能菜单"""
            self.show_menu()

            menu_num = int(input('请输入功能序号:'))

            if menu_num == 1:
                self.add_student()
            elif menu_num == 2:
                self.del_student()
            elif menu_num == 3:
                self.modify_student()
            elif menu_num == 4:
                self.search_student()
            elif menu_num == 5:
                self.show_student()
            elif menu_num == 6:
                self.save_student()
            elif menu_num == 7:
                """退出系统"""
                break


    @staticmethod
    def show_menu():
        print('请选择如下功能:')
        print('1.添加学员')
        print('2.删除学员')
        print('3.修改学员信息')
        print('4.查询学员信息')
        print('5.显示所有学员信息')
        print('6.保存学员信息')
        print('7.退出系统')


    def add_student(self):
        """添加学员"""
        name = input('请输入您的姓名')
        gender = input('请输入您的性别')
        tel = input('请输入您的手机号')

        student = Student(name, gender, tel)

        self.student_list.append(student)

        print(self.student_list)
        print(student)


    def del_student(self):
        """删除学员"""
        del_name = input("请输入要删除学员的姓名:")

        for i in self.student_list:
            if i.name == del_name:
                self.student_list.remove(i)
                break
        else:
            print('查无此人')

        print(self.student_list)


    def modify_student(self):
        """修改学员信息"""
        modify_name = input("请输入要修改学员的姓名:")

        for i in self.student_list:
            if i.name == modify_name:
                i.name = input('请输入学员姓名')
                i.gender = input('请输入学员性别')
                i.tel = input('请输入学员电话')
                print(f'修改成功的学员信息:姓名{i.name},性别{i.gender},电话{i.tel}')
                break
        else:
            print('查无此人')


    def search_student(self):
        """查询学员信息"""
        search_name = input("请输入要查询学员的姓名:")

        for i in self.student_list:
            if i.name == search_name:
                print(f'姓名{i.name},性别{i.gender},电话{i.tel}')
                break
        else:
            print('查无此人')


    def show_student(self):
        """显示所有学员信息"""
        print('姓名\t性别\t电话')
        for i in self.student_list:
            print(f'{i.name}\t{i.gender}\t{i.tel}')


    def save_student(self):
        """保存学员信息"""
        f = open('student.data', 'w')
        new_list = [i.__dict__ for i in self.student_list]  # 列表中对象转成字典
        print(new_list)
        f.write(str(new_list))  # 字典转成字符串写入
        f.close()

    def load_student(self):
        """加载学员信息"""
        try:
            f = open('student.data', 'r')
        except:
            f = open('student.data', 'w')
        else:
            # 读取数据
            data = f.read()
            # 文件中读取的字符串还原列表类型,再将[{}]转换为[学员对象]
            new_list = eval(data)
            self.student_list = [Student(i['name'], i['gender'], i['tel']) for i in new_list]
        finally:
            f.close()

student.data

[{'name': 'lily', 'gender': 'f', 'tel': '123456'}, {'name': 'huhu', 'gender': 'm', 'tel': '123456789'}]

你可能感兴趣的:(Python(学习笔记))