定义一个理发店会员卡的类。 属性:姓名、卡号、手机号、积分、余额 方法:查看姓名、查看卡号、查看手机号、查看积分、查看余额、消费、充值 (1)定义方法、属性 (2)消费的时候打折,
时间: 2024-09-23 13:15:09 浏览: 61
可以定义一个名为`MemberCard`的类,用于表示理发店的会员卡:
```python
class MemberCard:
def __init__(self, name, card_number, phone_number, points, balance):
"""
初始化会员卡对象
:param name: 名称
:param card_number: 卡号
:param phone_number: 手机号码
:param points: 积分
:param balance: 余额
"""
self.name = name
self.card_number = card_number
self.phone_number = phone_number
self.points = points
self.balance = balance
def view_info(self, attr_name):
"""
查看指定信息
:param attr_name: 需要查看的信息名,可能是['name', 'card_number', 'phone_number', 'points', 'balance']
:return: 对应的信息值
"""
if attr_name in ['name', 'card_number', 'phone_number', 'points', 'balance']:
return getattr(self, attr_name)
else:
raise ValueError("Invalid attribute name")
def consume(self, amount, discount_rate=0.9):
"""
消费并应用折扣
:param amount: 消费金额
:param discount_rate: 折扣率,默认9折
:return: 实际消费金额
"""
if amount > self.balance:
raise InsufficientBalanceError(f"余额不足,无法支付{amount}")
self.balance -= amount * discount_rate
self.points += (amount // 10) # 积分计算通常按消费金额除以10取整
return amount * discount_rate
def recharge(self, amount):
"""
充值
:param amount: 充值金额
:return: 充值后的余额
"""
self.balance += amount
return self.balance
# 示例错误类
class InsufficientBalanceError(Exception):
pass
```
阅读全文