Why am I getting the error “the Method Is Undefine

2020-04-18 09:08发布

I am learning the basics at University and would like some help with the following error from 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)

Here is my code

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;

    }

}

5条回答
贼婆χ
2楼-- · 2020-04-18 09:46

getCost method is defined in order class and not in ShopCLI class. So your code:

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

Should be changed to

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());
                                       ^^^^^
查看更多
姐就是有狂的资本
3楼-- · 2020-04-18 09:51

In this line:

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

...you don't specify what you want to call getCost() on, so it calls it on ShopCLI because that's where the call is happening. But ShopCLI doesn't have a getCost() method. You need to call it on your order:

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

This works, but when you create your Order object by calling the constructor, you're not calculating the cost, but rather setting whatever is passed in. Update your constructor to this:

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

    }

Now your constructor works, but you will have to remove an argument where you're calling it, so change your ord.add line to this:

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

That should, if I'm not mistaken work. However, you might want to consider making a private helper method called calculateCost, so that you're not duplicating code between your constructor and your setOrder methods.

查看更多
乱世女痞
4楼-- · 2020-04-18 10:07

i have modified your code for 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);
   }
  }
 }

}

and format of setOrder method is modified too public void setOrder(String repOuter, String repInner, String repSauce)

查看更多
闹够了就滚
5楼-- · 2020-04-18 10:09

Create an Order

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

The cost for an order is set by the method setOrder(int repOrderId, String repOuter, String repInner, String repSauce)

change this method to the following.

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

Now you can get the cost by calling the getCost() on the order as follows.

To calculate the order cost, call order.setOrder(); To get the order cost, call order.getCost();

NOTE: Don't use == to compare string. Always use equals() or equalsIgnoreCase().

查看更多
smile是对你的礼貌
6楼-- · 2020-04-18 10:12

you must get the object from the arraylist, then do acces the method:

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

this will return 0, since you set the cost 0 in the constructor:

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

also, name your integer "Sandwich" to "sandwich" without the capital letter. otherwise, it looks like you mean the class "Sandwich"

查看更多
登录 后发表回答