用python做的手机通讯录,实现了基本功能
#!/usr/bin/python
# -*- coding: UTF-8 -*-
"""
手机通讯录
功能实现
存储手机号码,姓名,地址,年龄
删除
修改
查询
"""
class ContactModel:
"""
创建联系人管理器
"""
def __init__(self,name,age,phone_num,addr):
self.name = name
self.age = age
self.phone_num = phone_num
self.addr = addr
class ContactManagerContrller:
def __init__(self):
self.__contactlist =[]
@property
def contactlist(self):
return self.__contactlist
def addcontact(self,contact_info):
self.__contactlist.append(contact_info)
def delcontact(self,name):
for item in self.__contactlist:
if item.name == name:
self.__contactlist.remove(item)
def updatecontact(self,contact_info):
for item in self.__contactlist:
if item.name == contact_info.name:
item.name= contact_info.name
item.age = contact_info.age
item.phone_num = contact_info.phone_num
item.addr = contact_info.addr
class ContactViewContrller:
"""
管理器视图
"""
def __init__(self):
self.__manager = ContactManagerContrller()
def __display_memu(self):
print("1)添加联系人")
print("2)显示联系人")
print("3)删除联系人")
print("4)修改联系人")
def __select_memu(self):
item = input("请输入选择:")
if item == "1":
self.__input_stu()
elif item == "2":
self.__outprint(self.__manager.contactlist)
elif item == "3":
self.__del_stu()
elif item == "4":
self.__modify_student()
def __input_stu(self):
name = input("请输入姓名:")
age = input("请输入年龄:")
phone_num= input("请输入号码:")
addr = input("请输入地址")
stu = ContactModel(name, age, phone_num,addr)
self.__manager.addcontact(stu)
def __outprint(self, stu_list):
for item in stu_list:
print(item.name, item.age, item.phone_num,item.addr)
def __del_stu(self):
name = input("请输入姓名删除:")
self.__manager.delcontact(name)
def __modify_student(self):
name = input("请输入需要修改的联系人名字:")
#name = input("请输入新的联系人名称:")
age = int(input("请输入新的联系人年龄:"))
phone_num = int(input("请输入新的联系人号码"))
addr = input("请输入新的联系人地址")
stu =ContactModel(name, age, phone_num, addr)
if self.__manager.updatecontact(stu):
print("修改成功")
else:
print("修改失败")
def main(self):
while True:
self.__display_memu()
self.__select_memu()
view = ContactViewContrller()
view.main()