代码实现:
1、父类--Beverage.java
public class Beverage {
private String name;//名称
private int price;//价格
private int size;//大小
private String remark;//备注
private int num;//购买数量
//private int sumprice;//总价
private int unitprice;//单价
private String sizecup;//杯子
//方法
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 String getRemark() {
return remark;
}
public void setRemark(String remark) {
this.remark = remark;
}
public int getNum() {
return num;
}
public void setNum(int num) {
this.num = num;
}
public int getUnitprice() {
return unitprice;
}
public void setUnitprice(int unitprice) {
this.unitprice = unitprice;
}
public String getSizecup() {
return sizecup;
}
public void setSizecup(String sizecup) {
this.sizecup = sizecup;
}
public String showbeverage(){
return "订单信息:\n"+"您购买了"+name+":"+remark+"\n购买的"+sizecup+"\n购买数量为"+num+"杯\n"+"总价为:"+(num*unitprice);
}
//构造器
public Beverage() {
}
public Beverage(String name, String remark, int num, int unitprice, String sizecup) {
this.name = name;
this.remark = remark;
this.num = num;
this.unitprice = unitprice;
this.sizecup = sizecup;
}
}
2、子类--可乐类--Coke.java
public class Coke extends Beverage {
public Coke() {
}
public Coke(String name, String remark, int num, int unitprice, String sizecup) {
super(name, remark, num, unitprice, sizecup);
}
}
3、子类--咖啡类--Coffee.java
public class Coffee extends Beverage {
public Coffee() {
}
public Coffee(String name, String remark, int num, int unitprice, String sizecup) {
super(name, remark, num, unitprice, sizecup);
}
}
4、子类--奶茶类--MilkTea.java
public class MilkTea extends Beverage {
public MilkTea() {
}
public MilkTea(String name, String remark, int num, int unitprice, String sizecup) {
super(name, remark, num, unitprice, sizecup);
}
}
5、工厂类--BeverageStore.java
public class BeverageStore {
public static Beverage buy(int choice){
int unitprice;
String sizecup;
Scanner sc=new Scanner(System.in);
Beverage bv=null;
switch(choice){
case 1:{
System.out.println("请录入你需要的可乐信息:");
System.out.println("备注可口可乐还是百事可乐,是否加冰:");
String remark=sc.next();
System.out.println("选择(1.中杯 2.大杯 3.超大杯):");
int size=sc.nextInt();
if(size==1){
unitprice=6;
sizecup="中杯";
}else if(size==2){
unitprice=7;
sizecup="大杯";
}else{
unitprice=8;
sizecup="超大杯";
}
System.out.println("输入购买数量:");
int num=sc.nextInt();
Coke ck=new Coke("可乐",remark,num,unitprice,sizecup);
bv=ck;
}
break;
case 2:{
System.out.println("请录入你需要的咖啡信息:");
System.out.println("备注加奶、加冰、加糖情况:");
String remark=sc.next();
System.out.println("选择(1.中杯 2.大杯 3.超大杯):");
int size=sc.nextInt();
if(size==1){
unitprice=6;
sizecup="中杯";
}else if(size==2){
unitprice=7;
sizecup="大杯";
}else{
unitprice=8;
sizecup="超大杯";
}
System.out.println("输入购买数量:");
int num=sc.nextInt();
Coffee cf=new Coffee("咖啡",remark,num,unitprice,sizecup);
bv=cf;
}
break;
case 3:{
System.out.println("请录入你需要的奶茶信息:");
System.out.println("备注是否添加红豆、椰果、布丁、珍珠:");
String remark=sc.next();
System.out.println("选择(1.中杯 2.大杯 3.超大杯):");
int size=sc.nextInt();
if(size==1){
unitprice=6;
sizecup="中杯";
}else if(size==2){
unitprice=7;
sizecup="大杯";
}else{
unitprice=8;
sizecup="超大杯";
}
System.out.println("输入购买数量:");
int num=sc.nextInt();
MilkTea mt=new MilkTea("奶茶",remark,num,unitprice,sizecup);
bv=mt;
}
break;
default:
System.out.println("输入错误!");
break;
}
return bv;
}
}
6、测试类--Test.java
public class Test {
//这是一个main方法,是整个程序的入口
public static void main(String[] args) {
Scanner sc=new Scanner(System.in);
System.out.println("请输入要购买的饮品(1.可乐 2.咖啡 3.奶茶):");
int choice=sc.nextInt();
BeverageStore bs=new BeverageStore();
Beverage br=bs.buy(choice);
System.out.println(br.showbeverage());
}
}
运行结果: