python基础入门之十八 —— 面向对象版学员管理系统

时间:2020-04-07 00:29:52   收藏:0   阅读:236

技术图片

 

 main.py:

from StudentMannager.manager import *

if __name__ == __main__:
    student_manager = Manager()
    student_manager.run()

manager.py:

from StudentMannager.student import *

class Manager(object):
    def __init__(self):
        self.stu_list = []

    # 一. 程序入口函数
    def run(self):
        # 1. 加载文件里面的学员数据
        self.load_student()
        while True:
            # 2. 显示功能菜单
            self.show_menu()
            # 3. 用户输入目标功能序号
            menu_num = int(input(请输入您需要的功能序号:))
            # 4. 根据用户输入的序号执行不同的功能 -- 如果用户输入1,执行添加
            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

    # 二. 系统功能函数
    # 2.1 显示功能菜单 -- 打印序号的功能对应关系 -- 静态
    @staticmethod
    def show_menu():
        print(请选择如下功能:)
        print(1:添加学员)
        print(2:删除学员)
        print(3:修改学员信息)
        print(4:查询学员信息)
        print(5:显示所有学员信息)
        print(6:保存学员信息)
        print(7:退出系统)

    # 2.2 添加学员
    def add_student(self):
        name = input("name = ")
        gender = input("gender = ")
        tel = input("tel = ")
        stu = student(name,gender,tel)
        self.stu_list.append(stu)
        print(添加成功)

    # 2.3 删除学员
    def del_student(self):
        del_name = input("del_name = ")
        for i in self.stu_list:
            if del_name == i.name:
                self.stu_list.remove(i)
                break
        else:
            print(查无此人!)
        print(删除成功)

    # 修改学员信息
    def modify_student(self):
        modify_name = input("modify_name = ")
        for i in self.stu_list:
            if modify_name == i.name:
                i.gender = input(f{i.name}\‘s gender = )
                i.tel = input(f{i.name}\‘s tel = )
                print(修改成功)
                break
        else:
            print(查无此人!)

    # 查询学员信息
    def search_student(self):
        search_name = input("search_name = ")
        for i in self.stu_list:
            if search_name == i.name:
                print(i)
                break
        else:
            print(查无此人!)

    # 显示所有学员信息
    def show_student(self):
        print(姓名\t年龄\t电话\t)
        for i in self.stu_list:
            print(f{i.name}\t{i.gender}\t{i.tel})
        print(-*20)

    # 保存学员信息
    def save_student(self):
        try:
            f = open(student.data,w)
            new_list = [i.__dict__ for i in self.stu_list]  # __dict__ 返回类内部所有的属性和方法对应的字典
print(new_list)
            f.write(str(new_list))
            f.close()
        except Exception as result:
            print(result)

    # 加载学员信息
    def load_student(self):
        try:
            f = open(student.data,r)
        except:
            f = open(student.data, w)
        else:
            data = f.read()
            new_list = eval(data)  # eval() 函数用来执行一个字符串表达式,并返回表达式的值。
            self.stu_list = [student(i[name],i[gender],i[tel]) for i in new_list]
        finally:
            f.close()

student.py:

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

    def __str__(self):
        return fname = {self.name},gender = {self.gender},tel = {self.tel}

 

评论(0
© 2014 mamicode.com 版权所有 京ICP备13008772号-2
迷上了代码!