为什么我收到错误 "the Method Is Undefined for the type"?

Why am I getting the error "the Method Is Undefined for the type"?

我正在大学学习基础知识,希望对 Eclipse 中的以下错误提供一些帮助:"The method getCost() is undefined for the type ShopCLI" &

"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;

    }

}

这一行:

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 方法之间重复代码。

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

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());
                                       ^^^^^

你必须从数组列表中获取对象,然后访问方法:

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

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

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

另外,将您的整数命名为 "Sandwich" 到 "sandwich",不带大写字母。否则,看起来你的意思是 class "Sandwich"

创建订单

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().

我已经为 class 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)