实现信息通讯录的面向对象02

def menu():
    print("#" * 78)
    print("{:^78}".format("通讯录管理系统v0.01a\n"))
    print("{:^78}".format("注册信息请按[a]"))
    print("{:^78}".format("查看信息请按[s]"))
    print("{:^78}".format("删除信息请按[d]"))
    print("{:^78}".format("修改信息请按[m]"))
    print("{:>70}".format("返回菜单请按[q]"))
    print("#" * 78)


class User:
    def __init__(self, name, qq, ph, mail):
        self.name = name
        self.qq = qq
        self.ph = ph
        self.mail = mail


class Account:
    def __init__(self):
        self.user_list = []  # 用户列表,数据格式:[User 对象, User 对象, User 对象]
        self.title = ["序号", "姓名", "QQ", "电话", "邮箱"]

    def menu_a(self):
        input_name = input("请输入您的名字:")
        input_qq = input("请输入您的QQ号码:")
        while input_qq.isnumeric() is False and input_qq != "":
            print("QQ号码格式错误!请不要输入数字以外的字符!")
            input_qq = input("{: <}".format("请输入正确的QQ号码: "))
        input_ph = input("请输入您的电话号码:")
        while input_ph.isnumeric() is False and input_ph != "":
            print("电话号码格式错误!请不要输入数字以外的字符!")
            input_ph = input("{: <}".format("请输入正确的电话号码: "))
        input_mail = input("请输入您的邮箱:")
        user = User(input_name, input_qq, input_ph,
                    input_mail)  # new_user = [input_name, input_qq, input_ph, input_mail]
        self.user_list.append(user)  # user_date.append(new_user)

        print("{:~^78}".format("添加/修改数据"))
        print("{0:<3}{1:<8}{2:<15}{3:<13}{4:<13}".format(self.title[0], self.title[1], self.title[2], self.title[3],
                                                         self.title[4]))
        i = 1
        for q in self.user_list:
            if user != q:
                i += 1

        print("{0:<5}{1:<10}{2:<15}{3:<15}{4:<15}".format(i, user.name, user.qq, user.ph, user.mail))
        print("~" * 78)

    def menu_s(self):
        print("{:~^61}".format("通讯录数据列表"))
        print("{0:<3}{1:<8}{2:<15}{3:<13}{4:<13}".format(self.title[0], self.title[1], self.title[2], self.title[3],
                                                         self.title[4]))
        num = 1
        for user in self.user_list:
            print("{0:<5}{1:<10}{2:<15}{3:<15}{4:<15}".format(num, user.name, user.qq, user.ph, user.mail))
            num += 1
        print("~" * 65)

    def test_date(self):
        self.user_list.append(["a", "1420475518", "16622720396", "1420475518@qq.com"])

    def menu_d(self):
        user_order = int(input("请输入需删除序号: "))
        if user_order > len(self.user_list) or user_order < 0:
            print("序号不存在,请重新输入")
            self.menu_d()
        elif user_order != 0:
            del self.user_list[user_order - 1]
            print("删除成功")
        elif user_order == 0:
            print("返回命令输入")

    def menu_m(self):
        num = input("请输入需修改序号: ")
        while not num.isdigit():
            print("请输入正确的序号")
            num = input("请输入需修改序号: ")
        user_order = int(num)
        if user_order > len(self.user_list) or user_order < 0:
            print("序号不存在,请重新输入")
            self.menu_m()
        elif user_order != 0:
            input_name = input("请输入您的姓名: ")
            input_qq = input("请输入您的QQ号吗: ")
            input_ph = input("请输入您的电话号码: ")
            input_mail = input("请输入您的邮箱: ")
            print("修改成功")
            input_name = input_name if input_name != " " else self.user_list[user_order - 1].name
            input_qq = input_qq if input_qq != " " else self.user_list[user_order - 1].qq
            input_ph = input_ph if input_ph != " " else self.user_list[user_order - 1].ph
            input_mail = input_mail if input_mail != " " else self.user_list[user_order - 1].mail
            self.user_list[user_order - 1].name = input_name
            self.user_list[user_order - 1].qq = input_qq
            self.user_list[user_order - 1].ph = input_ph
            self.user_list[user_order - 1].mail = input_mail
        elif user_order == 0:
            print("返回命令输入")

    def run(self):
        """
        主程序,先进行 2 次用户注册,再进行用户登录(3 次重试机会)
        """
        menu()
        while True:
            input_str = input("请输入相应的命令:")
            if input_str not in "asdmq":
                print("请输入正确的命令")
                return menu()
            else:
                if input_str == "a":
                    self.menu_a()
                elif input_str == "s":
                    self.menu_s()
                elif input_str == "d":
                    self.menu_d()
                elif input_str == "m":
                    self.menu_m()
                elif input_str == "q":
                    menu()
                else:
                    print("请输入正确的命令")
                    continue


if __name__ == "__main__":
    obj = Account()
    obj.run()

上一篇:无毛刺时钟切换电路


下一篇:2021.12.1Python基础打卡(四)