SRE/6.py
2025-07-06 22:31:32 +08:00

138 lines
4.0 KiB
Python
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

class Product:
def __init__(self, product_id, name, base_price):
# 封装基本属性
self._product_id = product_id
self._name = name
self._base_price = base_price
@property
def product_id(self):
return self._product_id
@property
def name(self):
return self._name
@property
def base_price(self):
return self._base_price
def calculate_price(self, quantity=1):
"""计算商品价格(基础实现)"""
return self._base_price * quantity
def display_info(self):
"""显示商品信息"""
return f"{self._name} (ID: {self._product_id}) - ${self._base_price:.2f}"
class DigitalProduct(Product):
def __init__(self, product_id, name, base_price, license_type):
super().__init__(product_id, name, base_price)
# 添加数字商品特有属性
self._license_type = license_type
@property
def license_type(self):
return self._license_type
def calculate_price(self, quantity=1):
"""数字商品价格计算(无数量折扣)"""
# 数字商品购买数量只能是1
return self._base_price
class PhysicalProduct(Product):
def __init__(self, product_id, name, base_price, weight):
super().__init__(product_id, name, base_price)
# 添加实体商品特有属性
self._weight = weight
@property
def weight(self):
return self._weight
def calculate_price(self, quantity=1):
"""实体商品价格计算(有批量折扣)"""
if quantity >= 10:
return self._base_price * quantity * 0.9 # 10件以上9折
return self._base_price * quantity
class ShoppingCart:
def __init__(self):
# 使用字典存储商品和数量 {product: quantity}
self._items = {}
def add_item(self, product, quantity=1):
"""添加商品到购物车"""
if not isinstance(product, Product):
raise ValueError("只能添加Product类型商品")
if product in self._items:
self._items[product] += quantity
else:
self._items[product] = quantity
def remove_item(self, product, quantity=1):
"""从购物车移除商品"""
if product in self._items:
if self._items[product] <= quantity:
del self._items[product]
else:
self._items[product] -= quantity
def calculate_total(self):
"""计算购物车总价"""
total = 0
for product, quantity in self._items.items():
total += product.calculate_price(quantity)
return total
def display_cart(self):
"""显示购物车内容"""
if not self._items:
print("购物车是空的")
return
print("购物车内容:")
for product, quantity in self._items.items():
price = product.calculate_price(quantity)
print(f"- {product.name} × {quantity}: ${price:.2f}")
print(f"总计: ${self.calculate_total():.2f}\n")
# 使用示例
if __name__ == "__main__":
# 创建商品
book = PhysicalProduct("B001", "Python编程书", 39.99, 0.5)
movie = DigitalProduct("D001", "科幻电影", 12.99, "永久")
headphone = PhysicalProduct("E001", "无线耳机", 89.99, 0.2)
# 创建购物车
cart = ShoppingCart()
# 添加商品
cart.add_item(book, 2)
cart.add_item(movie)
cart.add_item(headphone, 3)
# 显示购物车
cart.display_cart()
# 测试移除商品
print("移除一副耳机...")
cart.remove_item(headphone)
cart.display_cart()
# 测试批量折扣
print("添加10本书...")
cart.add_item(book, 10)
cart.display_cart()
# 显示商品信息
print("\n商品详细信息:")
print(book.display_info())
print(movie.display_info())
print(f"耳机重量: {headphone.weight}kg")
print(f"电影授权类型: {movie.license_type}")