目录
题目要求:
编写程序实现匹萨制作。需求说明编写程序,接收用户输入的信息,可供选择的比萨有:培根匹萨和水果匹萨。
根据输入信息产生具体的匹萨对象程序运行控制台效果如下:
请选择想要制作的匹萨: 1.培根萨 2水果萨:1
请输入培根的克数: 23
请输入匹萨的大小:12
请输入匹萨的价格: 103
名称:培根匹萨
价格: 103元
大小: 12寸
培根克数:25g
请选择想要制作的匹萨:1.培根匹萨 2水果匹萨:2请输入你想要加入的水果: 榴莲,芒果,草莓
请输入匹萨的大小: 10
请输入匹萨的价格: 76
名称:水果匹萨
价格: 76元
大小: 10寸
配料水果:榴莲,芒果,草莓
功能实现:
Pizza父类:
package com.msb.make_pizza;
public class Pizza {
private String name;
private int price;
private int size;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getPrice() {
return price;
}
public void setPrice(int price) {
this.price = price;
}
public int getSize() {
return size;
}
public void setSize(int size) {
this.size = size;
}
public Pizza() {
}
public Pizza(String name, int price, int size) {
this.name = name;
this.price = price;
this.size = size;
}
}
FruitPizza 和 BaconicPizza子类:
FruitPizza类
package com.msb.make_pizza;
import java.util.Scanner;
public class FruitPizza extends Pizza implements Put {
private String[] fruitName = new String[10];
public String[] getFruitName() {
return fruitName;
}
public void setFruitName(String[] fruitName) {
this.fruitName = fruitName;
}
public FruitPizza() {
}
public FruitPizza(String name, int price, int size, String[] fruitName) {
super(name, price, size);
this.fruitName = fruitName;
}
@Override
public void input() {
System.out.println("请输入你想要加入的水果:");
Scanner sc = new Scanner(System.in);
String fruitName1 = sc.next();
String[] fruitName = new String[]{fruitName1};
this.fruitName=fruitName; //或者 this.setFruitName(fruitName);
System.out.println("请输入披萨的大小:");
setSize(sc.nextInt());
System.out.println("请输入披萨的价格:");
setPrice(sc.nextInt());
}
@Override
public void output() {
System.out.print("名称:");
System.out.println("水果披萨");
System.out.print("价格:");
System.out.println(this.getPrice());
System.out.print("大小:");
System.out.println(this.getSize());
System.out.print("配料水果:");
// System.out.println(Arrays.toString(this.getFruitName()));
for (int i = 0; i < this.getFruitName().length-1; i++) {
System.out.print(this.getFruitName()[i]+", ");
}
System.out.print(this.getFruitName()[getFruitName().length-1]);
}
}
BaconicPizza类
package com.msb.make_pizza;
import java.util.Scanner;
public class BaconicPizza extends Pizza implements Put {
private int ke;
public int getKe() {
return ke;
}
public void setKe() {
this.ke = ke;
}
public BaconicPizza() {
}
public BaconicPizza(String name, int price, int size, int ke) {
super(name, price, size);
this.ke = ke;
}
public void input() {
System.out.println("请输入培根的克数:");
Scanner sc = new Scanner(System.in);
this.ke=sc.nextInt();
System.out.println("请输入披萨的大小:");
setSize(sc.nextInt());
System.out.println("请输入披萨的价格:");
setPrice(sc.nextInt());
}
public void output() {
System.out.print("名称:");
System.out.println("培根披萨");
System.out.print("价格:");
System.out.println(this.getPrice());
System.out.print("大小:");
System.out.println(this.getSize());
System.out.print("培根克数:");
System.out.println(this.getKe());
}
}
Computer类:
package com.msb.make_pizza;
import java.util.Scanner;
public class Computer {
public void input(Put put){
put.input();
}
public void output(Put put){
put.output();
}
public Put salePizza(){
Scanner sc = new Scanner(System.in);
System.out.println("请输入你想要加入的水果:1.水果披萨 2.培根披萨");
int choose = sc.nextInt();
Put pizza =null;
while (true) {
if (choose == 1) {
pizza = new FruitPizza();
break;
} else if (choose == 2) {
pizza = new BaconicPizza();
break;
} else {
System.out.println("输入格式错误,请重新输入你想要加入的水果:1.水果披萨 2.培根披萨");
choose = sc.nextInt();
}
}
return pizza;
}
}
Put接口:
package com.msb.make_pizza;
public interface Put {
public abstract void input();
public abstract void output();
}
Test类:
package com.msb.make_pizza;
public class Test {
public static void main(String[] args) {
Computer computer = new Computer();
Put pizza = computer.salePizza();
computer.input(pizza);
computer.output(pizza);
}
}
控制台结果:
情况1:
情况2:
情况3: