根据下面描述用Java来编写程序,程序包括披萨类,培根披萨类,水果披萨类,披萨工厂类,测试类。请选择您想制作的披萨:1培根2水果水果 请输入水果的配料: :芒果,草莓,榴莲芒果,草莓请输入披萨大小: 00 请输入披萨价格: 100 披萨名称:水果披萨披萨价格:8 披萨大小:100 水果配料: 芒果,草莓 请选择您想制作的披萨:1培根2水果培根请输入培根的克数: 20 请输入披萨大小: 10 请输入披萨价格: 150 披萨名称:培根披萨披萨价格: 150 披萨大小:10 培根克数:20
时间: 2023-06-12 10:06:31 浏览: 164
披萨类:
```java
public abstract class Pizza {
protected String name;
protected int size;
protected double price;
public String getName() {
return name;
}
public int getSize() {
return size;
}
public double getPrice() {
return price;
}
public abstract void prepare();
}
```
培根披萨类:
```java
public class BaconPizza extends Pizza {
private int baconWeight;
public BaconPizza(int size, double price, int baconWeight) {
this.name = "Bacon Pizza";
this.size = size;
this.price = price;
this.baconWeight = baconWeight;
}
public int getBaconWeight() {
return baconWeight;
}
@Override
public void prepare() {
System.out.println("Preparing " + name + " with " + baconWeight + "g bacon, size " + size + ", and price $" + price);
}
}
```
水果披萨类:
```java
import java.util.List;
public class FruitPizza extends Pizza {
private List<String> fruits;
public FruitPizza(int size, double price, List<String> fruits) {
this.name = "Fruit Pizza";
this.size = size;
this.price = price;
this.fruits = fruits;
}
public List<String> getFruits() {
return fruits;
}
@Override
public void prepare() {
System.out.println("Preparing " + name + " with " + fruits + ", size " + size + ", and price $" + price);
}
}
```
披萨工厂类:
```java
import java.util.Arrays;
import java.util.List;
import java.util.Scanner;
public class PizzaFactory {
private static final Scanner scanner = new Scanner(System.in);
public static Pizza createPizza() {
System.out.println("请选择您想制作的披萨: 1.培根披萨 2.水果披萨");
int choice = scanner.nextInt();
switch (choice) {
case 1:
System.out.println("请输入培根的克数:");
int baconWeight = scanner.nextInt();
System.out.println("请输入披萨大小:");
int size1 = scanner.nextInt();
System.out.println("请输入披萨价格:");
double price1 = scanner.nextDouble();
return new BaconPizza(size1, price1, baconWeight);
case 2:
System.out.println("请输入水果的配料,以英文逗号分隔:");
scanner.nextLine();
String fruitsStr = scanner.nextLine();
List<String> fruits = Arrays.asList(fruitsStr.split(","));
System.out.println("请输入披萨大小:");
int size2 = scanner.nextInt();
System.out.println("请输入披萨价格:");
double price2 = scanner.nextDouble();
return new FruitPizza(size2, price2, fruits);
default:
throw new IllegalArgumentException("Invalid pizza choice");
}
}
}
```
测试类:
```java
public class PizzaTest {
public static void main(String[] args) {
Pizza pizza = PizzaFactory.createPizza();
pizza.prepare();
System.out.println("披萨名称: " + pizza.getName());
System.out.println("披萨价格: $" + pizza.getPrice());
System.out.println("披萨大小: " + pizza.getSize());
if (pizza instanceof BaconPizza) {
System.out.println("培根克数: " + ((BaconPizza) pizza).getBaconWeight());
} else {
System.out.println("水果配料: " + ((FruitPizza) pizza).getFruits());
}
}
}
```
运行结果示例:
```
请选择您想制作的披萨: 1.培根披萨 2.水果披萨
2
请输入水果的配料,以英文逗号分隔:
芒果,草莓
请输入披萨大小:
100
请输入披萨价格:
8
Preparing Fruit Pizza with [芒果, 草莓], size 100, and price $8.0
披萨名称: Fruit Pizza
披萨价格: $8.0
披萨大小: 100
水果配料: [芒果, 草莓]
```
阅读全文