具体实现代码如下:
披萨类---》父类
package smo.msh.demoLast;
/**
* @Auther:zhangjj
* @Date:2021/3/25 - 03-25-9:29
* @Description:smo.msh.demoLast
* @version:1.0
*/
/**项目需求:接收用户需求,选择需要制造的匹萨,可供选择的有培根和水果匹萨
* */
//披萨类---》父类
public /*abstract*/ class Pizza {
//属性:名称,价格,大小
String name;
double price;
int size;
//方法:展示
/*abstract void Display();*/
public String showP(){
if(name==null||price==0||size==0){
return null;
}
return "\n匹萨价格(元):" + price +
"元\n匹萨尺寸(寸):" + size+"寸\n"
;
}
//构造器
public Pizza() {
}
public Pizza( double price, int size) {
//this.name = name;
this.price = price;
this.size = size;
}
/* public Pizza(String name) {
this.name = name;
}*/
}
子类---》水果披萨
package smo.msh.demoLast;
/**
* @Auther:zhangjj
* @Date:2021/3/25 - 03-25-9:31
* @Description:smo.msh.demoLast
* @version:1.0
*/
public class Fruits extends Pizza{
//属性:水果
String Durian, mango, strawberry;
String type,type1,type2,type3;
//方法:展示
public String show(){
String ssay = null;
this.name = "水果匹萨";
showFruits();
if(this.strawberry!=null){
this.price = (size+20)*9;
ssay =type ;
}
else if(this.mango!=null){
this.price = (size+16)*9;
ssay =showFruits(Durian,mango);;
}
else if(this.Durian!=null){
this.price = (size+12)*9;
ssay =showFruits(Durian); ;
}
/*if(this.strawberry!=null){
this.price = (size+20)*9;
}
else if(this.mango!=null){
this.price = (size+16)*9;
type = mango+","+Durian;
}
else if(this.Durian!=null){
this.price = (size+12)*9;
type = Durian;
}
*/
return "匹萨名称:"+this.name+super.showP()+"添加配料:"+ssay+"\n";
/* return "匹萨名称:"+this.name+super.showP()+"添加配料:"+type+"\n";*/
}
public String showFruits(){
return type = Durian+","+strawberry+","+mango;
}
public String showFruits(String Durian){
return type1 = Durian;
}
public String showFruits(String Durian,String mango){
/*String Fruit;*/
return type2= Durian+","+mango;
}
public String showFruits(String Durian,String mango,String strawberry){
/*String Fruit;*/
return type3 = Durian+","+strawberry+","+mango;
}
//构造器
public Fruits() {
this.name = "水果匹萨";
}
public Fruits(String durian) {
Durian = durian;
}
public Fruits(String durian, String mango) {
Durian = durian;
this.mango = mango;
}
public Fruits(String durian, String mango, String strawberry) {
Durian = durian;
this.mango = mango;
this.strawberry = strawberry;
}
}
子类---》培根匹萨
package smo.msh.demoLast;
/**
* @Auther:zhangjj
* @Date:2021/3/25 - 03-25-9:30
* @Description:smo.msh.demoLast
* @version:1.0
*/
public class Bacon extends Pizza{
int weight;
@Override
public String toString() {
this.price = (weight+size)*6;
return "匹萨名称:"+this.name+super.showP(/*price,size*/)+"培根克数(克):"+this.weight+"克\n";
}
public Bacon() {
this.name="培根匹萨";
}
public Bacon(int weight) {
this.weight = weight;
}
public Bacon(double price, int size, int weight) {
/* this.name="培根匹萨";*/
super(price,size);
this.weight = weight;
}
}
测试类---》main函数
package smo.msh.demoLast;
import java.util.Scanner;
/**
* @Auther:zhangjj
* @Date:2021/3/25 - 03-25-20:49
* @Description:smo.msh.demoLast
* @version:1.0
*/
public class test1 {
public static void main(String[] args) {
while (true){
int choose;
System.out.println("\n\t选择你想要的匹萨种类:1.培根匹萨\t2.水果匹萨\n\t\t\t\t\t\t\t0.退出\t");
Scanner input = new Scanner(System.in);
choose = input.nextInt();
switch (choose){
case 1:{
Bacon one = new Bacon();
System.out.println("请输入培根的克数:");
one.weight = input.nextInt();
System.out.println("请输入披萨的大小:");
one.size = input.nextInt();
System.out.println(one);
break;
}
case 2:{
System.out.println("请输入披萨的大小(寸):");
int Pizza_size = input.nextInt();
System.out.println("你想要选择几种配料?");
int choose2 = input.nextInt();
String ss[] =new String[choose2];
for(int i=0;i<choose2;++i){
System.out.println("请输入你想要的第"+(i+1)+"中匹萨配料种类:");
Scanner in = new Scanner(System.in);
String s = in.nextLine();
ss[i]=s;
}
switch (choose2){
case 1:{
Fruits one1 = new Fruits(ss[0]);
one1.size=Pizza_size;
System.out.println(one1.show());
break;
}
case 2:{
Fruits one2 = new Fruits(ss[0],ss[1]);
one2.size=Pizza_size;
System.out.println(one2.show());
break;
}
case 3:{
Fruits one3 = new Fruits(ss[0],ss[1],ss[2]);
one3.size=Pizza_size;
System.out.println(one3.show());
break;
}
}
break;
}
case 0:return;
default:{
System.out.println("是你不喜欢这两种吗,但是只有这两种请重新选择");
}break;
}
}
}
}