class ShoppingCart:
def __init__(self):
self.items = []
def add_items(self, item):
if item in items_price:
self.items.append(item)
print(f"{item}已添加到购物车")
else:
print(f"没有{item}商品")
def del_items(self, item):
if item in self.items:
self.items.remove(item)
print(f"{item}已从购物车中删除")
else:
print(f"{item}不在购物车中")
def display_items(self):
if self.items:
print("购物车中的物品:")
for item in self.items:
print(item)
else:
print("购物车为空")
def get_total_price(self):
total_price = 0
for item in self.items:
total_price += items_price[item]
return total_price
def get_item_num(self):
return len(self.items)
items_price = {
"苹果": 5,
"香蕉": 3,
"橙子": 2,
"梨": 4,
"西瓜": 1
}
cart = ShoppingCart()
cart.add_items("苹果")
cart.add_items("橙子")
cart.add_items("草莓")
cart.add_items("梨")
cart.add_items("西瓜")
cart.del_items("香蕉")
cart.add_items("橙子")
cart.del_items("橙子")
cart.display_items()
print("购物车中的物品数量:", cart.get_item_num())
print("购物车中的物品总价:", cart.get_total_price())
© 版权声明
THE END
暂无评论内容