用python写一个好友管理系统
时间: 2023-07-18 12:13:50 浏览: 195
以下是一个简单的好友管理系统的参考代码,使用Python语言编写:
```python
class Friend:
def __init__(self, name, age, gender, phone):
self.name = name
self.age = age
self.gender = gender
self.phone = phone
class FriendManagementSystem:
def __init__(self):
self.friends = []
# 添加好友
def add_friend(self, friend):
self.friends.append(friend)
# 删除好友
def remove_friend(self, name):
for friend in self.friends:
if friend.name == name:
self.friends.remove(friend)
return True
return False
# 查找好友
def find_friend(self, name):
for friend in self.friends:
if friend.name == name:
return friend
return None
# 显示所有好友
def show_all_friends(self):
for friend in self.friends:
print(f"Name: {friend.name}, Age: {friend.age}, Gender: {friend.gender}, Phone: {friend.phone}")
# 测试代码
if __name__ == "__main__":
# 创建好友管理系统对象
fms = FriendManagementSystem()
# 添加好友
f1 = Friend("Tom", 18, "Male", "123456789")
f2 = Friend("Lucy", 19, "Female", "987654321")
fms.add_friend(f1)
fms.add_friend(f2)
# 查找好友
f = fms.find_friend("Tom")
if f:
print(f"Find friend: Name: {f.name}, Age: {f.age}, Gender: {f.gender}, Phone: {f.phone}")
else:
print("Not found friend.")
# 删除好友
result = fms.remove_friend("Tom")
if result:
print("Remove friend success.")
else:
print("Remove friend fail.")
# 显示所有好友
fms.show_all_friends()
```
这个好友管理系统可以添加、删除、查找和显示所有好友。你可以根据自己的需求进行修改和扩展。
阅读全文