1109Python面向对象作业讲解

"""
____author____ = '霍格沃兹测试开发学社'
____desc____ = '更多测试开发技术探讨,请访问:https://ceshiren.com/t/topic/15860'
"""

# 定义一个学生类
import os

class Student(object):
    def __init__(self, sid, name, age, gender, addres, phone, mail, qq, wx):
        self.sid = sid
        self.name = name

    # 重写对象的显示格式 方法
    def __str__(self):
        return f"SID: {self.sid} --- Name: {self.name}"


# 封装管理类
class SMS(object):
    def __init__(self):
        # 定义一个全局变量,用来保存学生的信息,方法各个函数之间进行访问
        self.__students = []

        # 加载数据
        self.__load__data()

    def __menu(self):
        print("****************************************")
        print("*				学生管理系统			 *")
        print("*  	    1. 添加新学生信息              *")
        print("* 	    2. 通过学号修改学生信息		 *")
        print("*		3. 通过学号删除学生信息		 *")
        print("*		4. 通过姓名删除学生信息		 *")
        print("* 	    5. 通过学号查询学生信息          *")
        print("*		6. 通过姓名查询学生信息          *")
        print("*		7. 显示所有学生信息             *")
        print("*		8. 退出系统			  		 *")
        print("****************************************")

    # 管理函数
    def start(self):
        while True:
            self.__menu()
            select__op = input("输入编号选择操作:")

            if len(select__op) == 1 and select__op in "12345678":
                if select__op == "1":
                    sid = self.__getSid()
                    sname = self.__getSName()
                    self.__addStuedent(sid, sname)
                elif select__op == "2":
                    sid = self.__getSid()
                    self.__modifyStudentByID(sid)
                elif select__op == "3":
                    sid = self.__getSid()
                    self.__deleteStudentByID(sid)
                elif select__op == "4":
                    sname = self.__getSName()
                    self.__deleteStudentByName(sname)
                elif select__op == "5":
                    sid = self.__getSid()
                    self.__queryStudentByID(sid)
                elif select__op == "6":
                    sname = self.__getSName()
                    self.__queryStudentByName(sname)
                elif select__op == "7":
                    self.__show()
                else:
                    self.__save__data()
                    break
            else:
                print("输入的数据不合法,请输入在合法范围内的操作编号!!!")

    # 向文件中写入数据,进行持久化存储
    def __save__data(self):
        with open("db.txt", "w") as file:
            for s in self.__students:
                s__str = f"{s.sid}-{s.name}\n"
                file.write(s__str)

    # "s01-tom-23-male-beijing\n"
    # "s01-tom-23-male-beijing\n"
    # "s01-tom-23-male-beijing\n"
    # "s01-tom-23-male-beijing\n"

    # 从文件中读取数据,加载到内存
    def __load__data(self):
        if os.path.exists("db20231109.txt"):
            with open("db.txt", "r") as file:
                content = file.read().split()
                for s in content:
                    s = s.split(", ")
                    stu = Student(*s)
                    self.__students.append(stu)

    # 获取学生ID 的方法
    def __getSid(self):
        sid = input("请输入学生ID:")
        return sid

    # 获取学生姓名的方法
    def __getSName(self):
        sname = input("请输入学生姓名:")
        return sname

    # 添加学生
    def __addStuedent(self, sid, name):
        for s in self.__students:
            if s.sid == sid:
                print("学号已存在,添加失败")
                return "添加失败"
        else:
            # 实例了一个学生对象
            student = Student(sid, name)
            self.__students.append(student)
            print("添加学生信息成功")
            return '添加成功'

    # 通过学号修改学生信息
    def __modifyStudentByID(self, sid):
        for s in self.__students:
            if s.sid == sid:
                name = self.__getSName()
                s.name = name  # s["name"] = name
                print("修改成功")
                return "修改成功"
        else:
            print(f'没有 {sid} 对应的学生信息')
            return "修改失败"


    # 通过ID删除学生信息
    def __deleteStudentByID(self, sid):
        for s in self.__students:
            if s.sid == sid:
                self.__students.remove(s)
                print("删除成功")
                return "删除成功"
        else:
            print(f'没有 {sid} 对应的学生信息')
            return "删除失败"
    
    # 通过学生姓名 删除所有符合的学生
    def __deleteStudentByName(self, name):
        exist__s = []
        # 找出所有要删除的学生
        for s in self.__students:
            if s.name.upper() == name.upper():
                exist__s.append(s)
    
        # 开始删除
        if len(exist__s) > 0:
            for s in exist__s:
                self.__students.remove(s)
                print(f"姓名为 { name } 的学生删除成功")
            else:
                print(f"成功删除 {len(exist__s)} 个学生")
                return "删除成功"
        else:
            print(f"姓名为 【{name}】不存在,无法删除")
            return "删除失败"
    
    # 通过学号查询学生信息
    def __queryStudentByID(self, sid):
        for s in self.__students:
            if s.sid == sid:
                print(s)
                return "查询成功"
        else:
            print(f"学号 {sid} 的学生不存在")
            return "查询失败"
    
    # 通过姓名查询学生信息
    def __queryStudentByName(self, name):
        result = []
        for s in self.__students:
            if s.name.lower() == name.lower():
                result.append(s)
    
        if len(result) > 0:
            for s in result:
                print(s)
            return "查询成功"
        else:
            print("查询失败")
            return "查询失败"
    
    # 显示所有学生信息
    def __show(self):
        for s in self.__students:
            print(s)


# 程序入口
if __name__ == '__main__':
   # SMS().start()
    sms = SMS()
    sms.start()
    # sms._SMS__addStuedent("s01","Tom")

   # __show()
   # _SMS__show()
    # sms.__show()
    # sms.____students.append("1")
    # print(sms.____students)
   
"""
public      公有权限  self.name
protected   保护权限  self._proname
private     私有权限  self.__priname

s = Student()
s.name


int sum(int, int) -> sumii(a,b)
int sum(float, float) -> sumff(a,b)

sum(1,2)
sum(3.13,3.2)

"""




# class Studenet(object):
#     def ____init____(self, sid, name,age,gender):
#         self.sid = sid
#
# class SMS(object):
#     def ____init____(self):
#         self.students = []
#
#     def addStudent(self,obj):
#         self.students.append(obj)
#         return "添加成功"
#
#     def start(self):
#         self.menu()
#         select__op = input("请输入ID:")
#         if len(select__op) == 1 and select__op in "12345":
#             if select__op == "1":
#                 sid = input("输入ID")
#                 obj = Student(sid, "Tom","11","male")
#                 self.addStudent(obj)
#             elif select__op == "2":
#                 print('修改')
#             elif select__op == "3":
#                 print('查找')
#             elif select__op == "4":
#                 print('删除')
#             else:
#                 print("退出")
#         else:
#             print("输入ID不正确")
#
#
#     def menu(self):
#         print("菜单")
#
# if ____name____ == '____main____':
#     SMS().start()


# 1. 先一遍理解代码
# 2. 抄一遍纯代码
# 3. 按照自己的理解给写代码写注释,逐行写
# 4. 复制文件 ,将新文件中的代码全删除,按照你自己写的注释,将代码再补全

# class Studenet(object):
#     def ____init____(self, sid, name,age,gender):
#         self.sid = sid
#
# if ____name____ == '____main____':
#     SMS().start()