模拟一个超市会员管理系统,其业务范围包括: 1开卡 2修改密码 3积分累计 4积分兑换 5查询剩余积分 6退出
时间: 2024-02-16 20:02:43 浏览: 91
好的,我可以为您模拟一个超市会员管理系统,包括开卡、修改密码、积分累计、积分兑换、查询剩余积分和退出功能。
首先,我们可以定义一个Member类,表示超市会员,包含以下属性:
- 姓名(name)
- 会员卡号(card_number)
- 手机号码(phone_number)
- 密码(password)
- 积分(points)
代码如下:
```python
class Member:
def __init__(self, name, card_number, phone_number, password, points=0):
self.name = name
self.card_number = card_number
self.phone_number = phone_number
self.password = password
self.points = points
```
然后,我们可以定义一个MemberManagement类,表示超市会员管理系统,包含以下方法:
- 开卡(create_member)
- 修改密码(update_password)
- 积分累计(add_points)
- 积分兑换(exchange_points)
- 查询剩余积分(query_points)
- 退出(exit)
代码如下:
```python
class MemberManagement:
def __init__(self):
self.members = []
def create_member(self, name, phone_number, password):
# 生成会员卡号
card_number = str(len(self.members) + 1).zfill(6)
member = Member(name, card_number, phone_number, password)
self.members.append(member)
print(f"会员 {name} 开卡成功,卡号为 {card_number}")
def update_password(self, card_number, old_password, new_password):
member = self._get_member(card_number, old_password)
if member:
member.password = new_password
print("密码修改成功")
else:
print("卡号或密码错误")
def add_points(self, card_number, password, points):
member = self._get_member(card_number, password)
if member:
member.points += points
print(f"积分累计成功,当前积分为 {member.points}")
else:
print("卡号或密码错误")
def exchange_points(self, card_number, password, points):
member = self._get_member(card_number, password)
if member:
if member.points >= points:
member.points -= points
print(f"积分兑换成功,当前积分为 {member.points}")
else:
print("积分不足")
else:
print("卡号或密码错误")
def query_points(self, card_number, password):
member = self._get_member(card_number, password)
if member:
print(f"当前积分为 {member.points}")
else:
print("卡号或密码错误")
def exit(self):
print("欢迎下次再来")
def _get_member(self, card_number, password):
for member in self.members:
if member.card_number == card_number and member.password == password:
return member
return None
```
接下来,我们可以编写一些测试代码来测试我们的超市会员管理系统:
```python
# 创建会员管理系统
mm = MemberManagement()
# 开卡
mm.create_member("张三", "13800000001", "123456")
# 修改密码
mm.update_password("000001", "123456", "654321")
# 积分累计
mm.add_points("000001", "654321", 100)
# 积分兑换
mm.exchange_points("000001", "654321", 50)
# 查询剩余积分
mm.query_points("000001", "654321")
# 退出
mm.exit()
```
这样,我们就成功地模拟了一个超市会员管理系统,包括开卡、修改密码、积分累计、积分兑换、查询剩余积分和退出功能。
阅读全文