我正在大学学习基础知识,希望能从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;
}
}
5条答案
按热度按时间5m1hhzi41#
创建订单
订单的成本由setorder方法设置(int reporderid、string repouter、string repinner、string repsauce)
将此方法更改为以下方法。
现在,您可以按如下顺序调用getcost()来获取成本。
要计算订单成本,请调用order.setorder();要获取订单成本,请调用order.getcost();
注意:不要使用==来比较字符串。始终使用equals()或equalsignorecase()。
v440hwme2#
getCost
方法是在order类中定义的,而不是在ShopCLI
班级。所以你的代码是:应改为
kokeuurv3#
我已经修改了shopcli类的代码
和格式
setOrder
方法也被修改了public void setOrder(String repOuter, String repInner, String repSauce)
vsaztqbk4#
在此行中:
…您没有指定要调用什么
getCost()
所以它在shopcli上调用它,因为这就是调用发生的地方。但shopcli没有getCost()
方法。您需要在订单上打电话:这是可行的,但是当您通过调用构造函数来创建order对象时,您不是在计算成本,而是在设置传入的内容。将构造函数更新为:
现在您的构造函数可以工作了,但是您必须删除调用它的参数,因此将ord.add行更改为:
如果我没弄错的话,那应该是我的工作。但是,您可能需要考虑创建一个名为calculatecost的私有助手方法,这样就不会在构造函数和setorder方法之间复制代码。
cnwbcb6i5#
必须从arraylist获取对象,然后执行以下操作:
这将返回0,因为您在构造函数中设置了成本0:
另外,将整数“sandwich”命名为“sandwich”,不带大写字母。否则,看起来你是说“三明治”课