为什么我会得到错误“类型的方法未定义”?

w8f9ii69  于 2021-06-20  发布在  Mysql
关注(0)|答案(5)|浏览(469)

我正在大学学习基础知识,希望能从eclipse中得到一些帮助:“没有为shopcli类型定义getcost()方法”&

"Exception in thread "main" java.lang.Error: Unresolved compilation problem:
    The method getCost() is undefined for the type ShopCLI
    at components.ShopCLI.main(ShopCLI.java:39)

这是我的密码

public class ShopCLI {

    public static void main(String[] args) {

        ArrayList<Order> ord = new ArrayList<>();

        System.out.println("Welcome to Sandwich Shop CLI V1!");
        System.out.println("Please Choose and Option by Typing the Appropriate Number from the List");
        System.out.println("1.New Order");

        Scanner sc = new Scanner(System.in);
        int choice = sc.nextInt();

        System.out.println("Please Choose an Outer From the List: ");
        System.out.println("Press 1 to Continue or 2 to Exit");
        int Sandwich = sc.nextInt();

        System.out.println("Outer Options are Bun, Bread or Brioche");

        String inputOuter = sc.next();

        System.out.println("Inner Options are Ham, Cheese or Cucumber");

        String inputInner = sc.next();

        System.out.println("Sauce Options are Mayo, Butter or Marmite");

        String inputSauce = sc.next();

        if (Sandwich == 1){
            ord.add(new Order(1, inputOuter, inputInner, inputSauce, 0));
            System.out.println("You Made a " + inputInner + " with " + inputSauce + " Sandwich on " + inputOuter);
            System.out.println("This Will Cost " + getCost());
        }
        else if (Sandwich == 2){
            System.out.println("Exited.");
        }

    }

}

public class Sandwich {

    //Fields
    ArrayList<Sandwich> sandwich = new ArrayList<>();
    String outer;
    String inner;
    String sauce;

    //Constructor
    public Sandwich(String outer, String inner, String sauce){
        this.outer = outer;
        this.inner = inner;
        this.sauce = sauce;
    }

    //Methods

    public String getOuter(){
        return outer;
    }

    public String getInner(){
        return inner;
    }

    public String getSauce(){
        return sauce;
    }

    public void setOuter(String repOuter){
        outer = repOuter;
    }

    public void setInner(String repInner){
        inner = repInner;
    }

    public void setSauce(String repSauce){
        sauce = repSauce;
    }

    public void createSandwich(String outer, String inner, String sauce){
        sandwich.add(new Sandwich(outer, inner, sauce));
    }

    public void setSandwich(String repOuter, String repInner, String repSauce){
        outer = repOuter;
        inner = repInner;
        sauce = repSauce;
    }

    public void resetOrder(){
        sandwich.removeAll(sandwich);
    }
}

public class Order extends Sandwich {

    //Fields
    int OrderId;
    double Cost;

    //Constructor
    public Order(int OrderId, String outer, String inner, String sauce, int Cost) {
        super(outer, inner, sauce);
        this.OrderId = OrderId;
        this.Cost = Cost;

    }

    //Methods

    public int getOrderId(){
        return this.OrderId;
    }

    public double getCost(){
        return this.Cost;
    }

    public void setOrderId(int repOrderID){
        this.OrderId = repOrderID;
    }

    public void overrideCost(int Cost){
        this.Cost = Cost;
    }

    public void setOrder(int repOrderId, String repOuter, String repInner, String repSauce){
        this.OrderId = repOrderId;
        this.outer = repOuter;
        this.inner = repInner;
        this.sauce = repSauce;

        double calcCost;
        double outerCost = 0;
        double innerCost = 0;
        double sauceCost = 0;

        //Outer Cost
        if(repOuter == "Bun")
        {
            outerCost = 0.5;
        }
        else if(repOuter == "Bread")
        {
            outerCost = 0.25;
        }
        else if(repOuter == "Brioche")
        {
            outerCost = 0.75;
        }
        else
        {
            System.out.println("Invalid Bread Type");
        }

        //Inner cost
        if(repInner == "Ham")
        {
            innerCost = 0.5;
        }
        else if(repInner == "Cheese")
        {
            innerCost = 0.25;
        }
        else if(repInner == "Cucumber")
        {
            innerCost = 0.75;
        }
        else
        {
            System.out.println("Invalid Filling Type");
        }

        //Sauce Cost
        if(repSauce == "Mayo")
        {
            sauceCost = 0.5;
        }
        else if(repSauce == "Butter")
        {
            sauceCost = 0.25;
        }
        else if(repSauce == "Marmite")
        {
            sauceCost = 0.75;
        }
        else
        {
            System.out.println("Invalid Sauce Type");
        }

        calcCost = outerCost + innerCost + sauceCost;
        this.Cost = calcCost;

    }

}
5m1hhzi4

5m1hhzi41#

创建订单

Order order = new Order(1, inputOuter, inputInner, inputSauce, 0);
ord.add(order) // Add the order to your list.

订单的成本由setorder方法设置(int reporderid、string repouter、string repinner、string repsauce)
将此方法更改为以下方法。

public void setOrder() {

    double calcCost;
    double outerCost = 0;
    double innerCost = 0;
    double sauceCost = 0;

    // Outer Cost
    if (outer.equalsIgnoreCase("Bun")) {
        outerCost = 0.5;
    } else if (outer.equalsIgnoreCase("Bread")) {
        outerCost = 0.25;
    } else if (outer.equalsIgnoreCase("Brioche")) {
        outerCost = 0.75;
    } else {
        System.out.println("Invalid Bread Type");
    }

    // Inner cost
    if (inner.equalsIgnoreCase("Ham")) {
        innerCost = 0.5;
    } else if (inner.equalsIgnoreCase("Cheese")) {
        innerCost = 0.25;
    } else if (inner.equalsIgnoreCase("Cucumber")) {
        innerCost = 0.75;
    } else {
        System.out.println("Invalid Filling Type");
    }

    // Sauce Cost
    if (sauce.equalsIgnoreCase("Mayo")) {
        sauceCost = 0.5;
    } else if (sauce.equalsIgnoreCase("Butter")) {
        sauceCost = 0.25;
    } else if (sauce.equalsIgnoreCase("Marmite")) {
        sauceCost = 0.75;
    } else {
        System.out.println("Invalid Sauce Type");
    }

    calcCost = outerCost + innerCost + sauceCost;
    this.Cost = calcCost;
}

现在,您可以按如下顺序调用getcost()来获取成本。
要计算订单成本,请调用order.setorder();要获取订单成本,请调用order.getcost();
注意:不要使用==来比较字符串。始终使用equals()或equalsignorecase()。

v440hwme

v440hwme2#

getCost 方法是在order类中定义的,而不是在 ShopCLI 班级。所以你的代码是:

ord.add(new Order(1, inputOuter, inputInner, inputSauce, 0));
System.out.println("You Made a " + inputInner + " with " + inputSauce + " Sandwich on " + inputOuter);
System.out.println("This Will Cost " + getCost());

应改为

Order order = new Order(1, inputOuter, inputInner, inputSauce, 0);
ord.add(order);
System.out.println("You Made a " + inputInner + " with " + inputSauce + " Sandwich on " + inputOuter);
System.out.println("This Will Cost " + order.getCost());
                                       ^^^^^
kokeuurv

kokeuurv3#

我已经修改了shopcli类的代码

package tester;

import java.util.ArrayList;
import java.util.Scanner;

public class ShopCLI {

public static void main(String[] args) {

   ArrayList<Order> ord = new ArrayList<>();
   int orderNumber =1;
   System.out.println("Welcome to Sandwich Shop CLI V1!");
   System.out.println("start order");

   Scanner sc = new Scanner(System.in);
  while(true){
   System.out.println("Press 1 for new order or 2 to Exit");
   int choice = sc.nextInt();

   if (choice == 1){
     System.out.println("Enter outer Options:Outer Options are Bun, Bread or Brioche");

      String inputOuter = sc.next();

      System.out.println("Enter inner Options:Inner Options are Ham, Cheese or Cucumber");

      String inputInner = sc.next();

      System.out.println("Enter sauce Options:Sauce Options are Mayo, Butter or Marmite");

      String inputSauce = sc.next();
     Order order1 = new Order(orderNumber, inputOuter, inputInner, inputSauce, 0);
     order1.setOrder(inputOuter, inputInner, inputSauce);
       System.out.println("You Made a " + inputInner + " with " + inputSauce + " Sandwich on " + inputOuter);
       System.out.println("This Will Cost " + order1.getCost());
       ord.add(order1);
   }
   else if (choice == 2){
       System.out.println("Exited.");
       System.exit(1);
   }
  }
 }

}

和格式 setOrder 方法也被修改了 public void setOrder(String repOuter, String repInner, String repSauce)

vsaztqbk

vsaztqbk4#

在此行中:

System.out.println("This Will Cost " + getCost());

…您没有指定要调用什么 getCost() 所以它在shopcli上调用它,因为这就是调用发生的地方。但shopcli没有 getCost() 方法。您需要在订单上打电话:

System.out.println("This Will Cost " + ord.get(0).getCost());

这是可行的,但是当您通过调用构造函数来创建order对象时,您不是在计算成本,而是在设置传入的内容。将构造函数更新为:

//Constructor
    public Order(int OrderId, String outer, String inner, String sauce) {
        super(outer, inner, sauce);
        this.OrderId = OrderId;

        double calcCost;
        double outerCost = 0;
        double innerCost = 0;
        double sauceCost = 0;

        //Outer Cost
        if(outer == "Bun")
        {
            outerCost = 0.5;
        }
        else if(outer == "Bread")
        {
            outerCost = 0.25;
        }
        else if(outer == "Brioche")
        {
            outerCost = 0.75;
        }
        else
        {
            System.out.println("Invalid Bread Type");
        }

        //Inner cost
        if(inner == "Ham")
        {
            innerCost = 0.5;
        }
        else if(inner == "Cheese")
        {
            innerCost = 0.25;
        }
        else if(inner == "Cucumber")
        {
            innerCost = 0.75;
        }
        else
        {
            System.out.println("Invalid Filling Type");
        }

        //Sauce Cost
        if(sauce == "Mayo")
        {
            sauceCost = 0.5;
        }
        else if(sauce == "Butter")
        {
            sauceCost = 0.25;
        }
        else if(sauce == "Marmite")
        {
            sauceCost = 0.75;
        }
        else
        {
            System.out.println("Invalid Sauce Type");
        }

        calcCost = outerCost + innerCost + sauceCost;
        this.Cost = calcCost;

    }

现在您的构造函数可以工作了,但是您必须删除调用它的参数,因此将ord.add行更改为:

ord.add(new Order(1, inputOuter, inputInner, inputSauce));

如果我没弄错的话,那应该是我的工作。但是,您可能需要考虑创建一个名为calculatecost的私有助手方法,这样就不会在构造函数和setorder方法之间复制代码。

cnwbcb6i

cnwbcb6i5#

必须从arraylist获取对象,然后执行以下操作:

//get obj,then void
System.out.println("This Will Cost " + ord.get(choise).getCost());

这将返回0,因为您在构造函数中设置了成本0:

ord.add(new Order(1, inputOuter, inputInner, inputSauce, 0));

另外,将整数“sandwich”命名为“sandwich”,不带大写字母。否则,看起来你是说“三明治”课

相关问题