有以下甜品:拉木苏(Iiramisu)、之士蛋糕(CheeseCake)和三明治(Sandwich); 以及如下饮料:红茶(Black tea,)、咖啡(Coffee)和橙汁(Orange Juice)。 将甜品和饮料分别定义为列表,并进行搭配,要求甜品和饮料可以随意搭配。请问,可以搭配出哪些套餐供客户选择?请依次打印输出各种套餐。
时间: 2023-12-10 09:37:29 浏览: 178
以下是甜品和饮料的列表:
```python
sweets = ['Iiramisu', 'CheeseCake', 'Sandwich']
drinks = ['Black tea', 'Coffee', 'Orange Juice']
```
我们可以使用两个 for 循环来遍历甜品和饮料的所有组合,并输出套餐:
```python
for sweet in sweets:
for drink in drinks:
print(sweet, "+", drink)
```
输出结果如下:
```
Iiramisu + Black tea
Iiramisu + Coffee
Iiramisu + Orange Juice
CheeseCake + Black tea
CheeseCake + Coffee
CheeseCake + Orange Juice
Sandwich + Black tea
Sandwich + Coffee
Sandwich + Orange Juice
```
因此,一共可以搭配出 9 种不同的套餐供客户选择。
相关问题
有以下甜品:拉木苏(Iiramisu)、之士蛋糕(CheeseCake)和三明治(Sandwich); 以及如下饮料:红茶(Black tea,)、咖啡(Coffee)和橙汁(Orange Juice)。 将甜品和饮料分别定义为列表,并进行搭配,要求甜品和饮料可以随意搭
配,最后输出所有的搭配组合。
代码如下:
```python
sweets = ['Iiramisu', 'CheeseCake', 'Sandwich']
drinks = ['Black tea', 'Coffee', 'Orange Juice']
for sweet in sweets:
for drink in drinks:
print(sweet + ' with ' + drink)
```
输出结果如下:
```
Iiramisu with Black tea
Iiramisu with Coffee
Iiramisu with Orange Juice
CheeseCake with Black tea
CheeseCake with Coffee
CheeseCake with Orange Juice
Sandwich with Black tea
Sandwich with Coffee
Sandwich with Orange Juice
```
这样就可以输出所有甜品和饮料的搭配组合了。
使用三种设计模式的蛋糕店 代码 java
以下是使用三种设计模式的蛋糕店的 Java 代码示例:
1. 工厂模式
```
// 定义蛋糕接口
interface Cake {
void make();
}
// 实现蛋糕接口的具体类
class CheeseCake implements Cake {
public void make() {
System.out.println("制作芝士蛋糕");
}
}
class ChocolateCake implements Cake {
public void make() {
System.out.println("制作巧克力蛋糕");
}
}
// 定义蛋糕工厂
class CakeFactory {
public Cake createCake(String type) {
if (type.equals("cheese")) {
return new CheeseCake();
} else if (type.equals("chocolate")) {
return new ChocolateCake();
} else {
return null;
}
}
}
// 蛋糕店
class CakeStore {
private CakeFactory factory;
public CakeStore(CakeFactory factory) {
this.factory = factory;
}
public void orderCake(String type) {
Cake cake = factory.createCake(type);
cake.make();
}
}
// 调用
public class Main {
public static void main(String[] args) {
CakeFactory factory = new CakeFactory();
CakeStore store = new CakeStore(factory);
store.orderCake("cheese");
store.orderCake("chocolate");
}
}
```
2. 观察者模式
```
// 定义观察者接口
interface Observer {
void update();
}
// 实现观察者接口的具体类
class Customer implements Observer {
private String name;
public Customer(String name) {
this.name = name;
}
public void update() {
System.out.println(name + "收到了新蛋糕的通知");
}
}
// 定义主题接口
interface Subject {
void registerObserver(Observer observer);
void removeObserver(Observer observer);
void notifyObservers();
}
// 实现主题接口的具体类
class CakeShop implements Subject {
private List<Observer> observers;
public CakeShop() {
observers = new ArrayList<>();
}
public void registerObserver(Observer observer) {
observers.add(observer);
}
public void removeObserver(Observer observer) {
observers.remove(observer);
}
public void notifyObservers() {
for (Observer observer : observers) {
observer.update();
}
}
public void newCake() {
System.out.println("蛋糕店有新蛋糕了");
notifyObservers();
}
}
// 调用
public class Main {
public static void main(String[] args) {
CakeShop shop = new CakeShop();
Observer customer1 = new Customer("张三");
Observer customer2 = new Customer("李四");
shop.registerObserver(customer1);
shop.registerObserver(customer2);
shop.newCake();
}
}
```
3. 单例模式
```
// 单例模式
class CakeMaker {
private static CakeMaker instance;
private CakeMaker() {}
public static CakeMaker getInstance() {
if (instance == null) {
instance = new CakeMaker();
}
return instance;
}
public void makeCake() {
System.out.println("制作蛋糕");
}
}
// 调用
public class Main {
public static void main(String[] args) {
CakeMaker maker = CakeMaker.getInstance();
maker.makeCake();
}
}
```
阅读全文