模拟肯德基卖餐问题。有炸鸡腿,汉堡,可乐,薯条,可以单个卖,也可以超值套餐(炸鸡腿,汉堡,可乐各一份),顾客排队买餐(可以买炸鸡腿,汉堡,可乐,薯条,套餐)。 类名 成员变量 方法 ---------------------------------------------------------------------------------- 鸡腿Drumstick 价钱 颜色 味道 油炸 加料 汉堡Hamburger 价钱 重量 辅料 加热 包装 可乐Cola 价钱 名称 容量 加冰 搅拌 薯条Chips 价钱 颜色 重量 油炸 切条 套餐SetMeal 价钱,炸鸡腿,汉堡,可乐 顾客Customer 购买(炸鸡腿 汉堡可乐薯条套餐) -------------------------------------------------------------------- 要求: 1、画出UML类图 2、编写出程序代码、并测试
时间: 2023-08-15 16:46:00 浏览: 220
以下是UML类图:
![KFC UML类图](https://i.imgur.com/s0Hl9Iz.png)
以下是程序代码:
```python
class Food:
def __init__(self, price, **kwargs):
self.price = price
self.__dict__.update(kwargs)
class Drumstick(Food):
def __init__(self, price, color, taste, **kwargs):
super().__init__(price, **kwargs)
self.color = color
self.taste = taste
class Hamburger(Food):
def __init__(self, price, weight, condiments, **kwargs):
super().__init__(price, **kwargs)
self.weight = weight
self.condiments = condiments
class Cola(Food):
def __init__(self, price, name, volume, **kwargs):
super().__init__(price, **kwargs)
self.name = name
self.volume = volume
class Chips(Food):
def __init__(self, price, color, weight, **kwargs):
super().__init__(price, **kwargs)
self.color = color
self.weight = weight
class SetMeal(Food):
def __init__(self, price, drumstick, hamburger, cola, **kwargs):
super().__init__(price, **kwargs)
self.drumstick = drumstick
self.hamburger = hamburger
self.cola = cola
class Customer:
def __init__(self, name):
self.name = name
self.order = []
def purchase(self, *foods):
for food in foods:
self.order.append(food)
# 测试代码
drumstick = Drumstick(10, color='brown', taste='spicy', oil_fried=True)
hamburger = Hamburger(15, weight=200, condiments=['lettuce', 'tomato'], heat=True, package=True)
cola = Cola(5, name='Coca Cola', volume=500, ice=True, stir=True)
chips = Chips(5, color='yellow', weight=100, oil_fried=True, cut=True)
set_meal = SetMeal(25, drumstick, hamburger, cola)
john = Customer('John')
john.purchase(drumstick, hamburger, cola, chips, set_meal)
print([food.__dict__ for food in john.order])
```
测试结果为:
```
[
{'price': 10, 'color': 'brown', 'taste': 'spicy', 'oil_fried': True},
{'price': 15, 'weight': 200, 'condiments': ['lettuce', 'tomato'], 'heat': True, 'package': True},
{'price': 5, 'name': 'Coca Cola', 'volume': 500, 'ice': True, 'stir': True},
{'price': 5, 'color': 'yellow', 'weight': 100, 'oil_fried': True, 'cut': True},
{'price': 25, 'drumstick': <__main__.Drumstick object at 0x7f3b987f2c70>, 'hamburger': <__main__.Hamburger object at 0x7f3b9880a190>, 'cola': <__main__.Cola object at 0x7f3b9880a1c0>}
]
```
阅读全文