AX001-HU 2022-02-09 15:29 采纳率: 100%
浏览 26
已结题

自助饮品消费系统 工厂类(?)

问题遇到的现象和发生背景

“prpject_饮料”中的“咖啡(java.lang.String,int,int,java.lang.String)”。Coffee“不能应用于“(java.lang.String,java.lang.String,int,java.lang.String)”
'Coffee(java.lang.String, int, int, java.lang.String)' in 'prpject_beverage.Coffee' cannot be applied to '(java.lang.String, java.lang.String, int, java.lang.String)'

操作环境、软件版本等信息

IDEA

package prpject_beverage;

public class Beverage {
    private String name;//名称
    private  int size;//规格
    private int price;//价格

    public int getSize() {
        return size;
    }

    public void setSize(int size) {
        this.size = size;
    }

    public int getPrice() {
        return price;
    }

    public void setPrice(int price) {
        this.price = price;
    }
    public String showBeverage(){
        return "选择的饮料:"+name+"\n饮料规格:"+size+"\n"+"价格是:"+price+"元";
    }

    public Beverage() {
    }

    public Beverage(String name, int size, int price) {
        this.name = name;
        this.size = size;
        this.price = price;
    }
}
package prpject_beverage;

public class Coffee extends Beverage{
 private String remark;

    public String getRemark() {
        return remark;
    }

    public void setRemark(String remark) {
        this.remark = remark;
    }

    public Coffee() {
    }

    public Coffee(String name, int size, int price, String remark) {
        super(name, size, price);
        this.remark = remark;
    }

    @Override
    public String showBeverage() {
        return super.showBeverage()+remark;
    }
}

package prpject_beverage;

public class Coke extends Beverage{
private String remark;

    public String getRemark() {
        return remark;
    }

    public void setRemark(String remark) {
        this.remark = remark;
    }

    public Coke() {
    }

    public Coke(String name, int size, int price, String remark) {
        super(name, size, price);
        this.remark = remark;
    }

    @Override
    public String showBeverage() {
        return super.showBeverage()+"选择的可乐是"+remark;
    }
}
package prpject_beverage;

public class Tea extends Beverage {
    private String remark;

    public String getRemark() {
        return remark;
    }

    public void setRemark(String remark) {
        this.remark = remark;
    }

    public Tea() {
    }

    public Tea(String name, int size, int price, String remark) {
        super(name, size, price);
        this.remark = remark;
    }

    @Override
    public String showBeverage() {
        return super.showBeverage()+remark;
    }
}
package prpject_beverage;

import java.util.Scanner;

public class Factory {
  public static Beverage getBeverage(int choice){
      Scanner sc = new Scanner(System.in);
      Beverage b= null;
      switch (choice){
          case 1:{
              System.out.println("输入咖啡的规格");
              String size=sc.next();
              System.out.println("输入咖啡的备注");
              String remark= sc.next();
              System.out.println("输入咖啡的价格");
              int price = sc.nextInt();
              Coffee c = new Coffee("咖啡", size,price,remark);
              b=c;
          }
          break;
          case 2:{
              System.out.println("输入可乐的名称");
              String name= sc.nextLine();
              System.out.println("输入可乐的规格");
              String size = sc.next();
              System.out.println("输入可乐的价格");
              int price = sc.nextInt();
              System.out.println("输入可乐的备注");
              String remark= sc.next();
              Coke co = new Coke("可乐",size,price,remark);
              b=co;
          }
          break;
          case 3:{
              System.out.println("输入奶茶的名称");
              String name= sc.nextLine();
              System.out.println("输入奶茶的规格");
              String size= sc.next();
              System.out.println("输入奶茶价格");
              int price = sc.nextInt();
              System.out.println("输入奶茶备注");
              String remark = sc.next();
              Tea t = new Tea("奶茶",size,price,remark);
              b=t;
          }
      }
   return b;
  }
}
package prpject_beverage;

import java.util.Scanner;

public class Test {
    public static void main(String[] args) {
        Scanner sc= new Scanner(System.in);
        System.out.println("选择尼需要的饮料(1、 咖啡  2、可乐  3、奶茶)");
        int choice = sc.nextInt();//选择
//        工厂提取
        Beverage beverage = Factory.getBeverage(choice);
        System.out.println(beverage.showBeverage());

    }
}

尝试过的解决方法
我想要达到的结果
  • 写回答

3条回答 默认 最新

  • CSDN专家-sinJack 2022-02-09 15:37
    关注
    public Coffee(String name, int size, int price, String remark) {
            super(name, size, price);
            this.remark = remark;
        }
    

    定义的构造方法行参类型和实参类型不一致。
    size修改为int类型。
    int size=sc.nextInt();

    
    case 1:{
                  System.out.println("输入咖啡的规格");
                  int size=sc.nextInt();
                  System.out.println("输入咖啡的备注");
                  String remark= sc.next();
                  System.out.println("输入咖啡的价格");
                  int price = sc.nextInt();
                  Coffee c = new Coffee("咖啡", size,price,remark);
                  b=c;
              }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论 编辑记录
查看更多回答(2条)

报告相同问题?

问题事件

  • 系统已结题 2月17日
  • 已采纳回答 2月9日
  • 创建了问题 2月9日

悬赏问题

  • ¥15 ansys fluent计算闪退
  • ¥15 有关wireshark抓包的问题
  • ¥15 需要写计算过程,不要写代码,求解答,数据都在图上
  • ¥15 向数据表用newid方式插入GUID问题
  • ¥15 multisim电路设计
  • ¥20 用keil,写代码解决两个问题,用库函数
  • ¥50 ID中开关量采样信号通道、以及程序流程的设计
  • ¥15 U-Mamba/nnunetv2固定随机数种子
  • ¥15 vba使用jmail发送邮件正文里面怎么加图片
  • ¥15 vb6.0如何向数据库中添加自动生成的字段数据。