通过 ArrayLists 创建项目菜单

Creating a Menu of Items via ArrayLists

所以我想做的是用 ArrayLists 创建一个食品菜单。我的任务是完成静态方法 profits 的以下定义,该方法输入 MenuItems 的 ArrayList 项和 returns 双精度数组。输出中的每个条目都是相应项目元素的价格和成本之间的差值。我必须使用静态方法 roundMoney 处理每个涉及金钱的计算结果。

    public class MenuItem 
    { 
      private String myName; 

      private double myPrice, 
                     myCost; 

      private int    myCode; 

      private boolean myAvailability; 

      public MenuItem( String name, double price, double cost, int code, boolean available ) 
      { 
        myName = name; 
        myPrice = price; 
        myCost = cost; 
        myCode = code; 
        myAvailability = available; 
      } 

      public String getName() { return myName; } 
      public double getPrice() { return myPrice; } 
      public double getCost() { return myCost; } 
      public int getCode() { return myCode; } 
      public boolean available() { return myAvailability; } 


      public String menuString() 
        { 
            return getName() + " ($" + getPrice() + ")"; 
        } 


      public static double roundMoney( double amount ) 
      { 
        return (int)(100 * amount + 0.5) / 100.0; 
      } 

      public static String printAmount( double d ) 
      { 
        String s = "" + d; 
        int k = s.indexOf( "." ); 
        if ( k < 0 ) 
          return s + ".00"; 
        if ( k + 1 == s.length() ) 
          return s + "00"; 
        if ( k + 2 == s.length() ) 
          return s + "0"; 
        else 
          return s; 
      } 
    } 
       //***********************************************************************
    public static double[] Profits(ArrayList<MenuItem> items) 
    {
        double[] profits = new double[items.size()];
        for (int i = 0; i < profits.length; i++)
         {
            profits[i] = roundMoney (items.get(i).getPrice() - items.get(i).getCost()); 
         }
        return profits;
    }
     //***********************************************************************
public static void main( String[] args )
{  
    ArrayList<MenuItem> items1 = new ArrayList<MenuItem>();
    items1.add( new MenuItem("Stir Fry",5.43,0.45,1,true) );
    items1.add( new MenuItem("Nachos",3.49,0.15,0,false) );
    items1.add( new MenuItem("Mud Pie",6.50,1.25,2,true) );
    items1.add( new MenuItem("Jerk Chicken",8.99,3.20,1,false) );

    double[] t = profits( items1 );

    for ( double d : t )
      System.out.print( printAmount( d ) + " " );
}

我的预期结果是:4.98 3.34 5.25 5.79,但我一直收到错误:TC1.java:30:错误:找不到符号

double[] t = 利润( items1 ); 有人可以帮我弄清楚出了什么问题吗?谢谢!

您用大写 "P."

定义函数 profits
public static double[] Profits(ArrayList<MenuItem> items) 

更改错误行以匹配函数名称:

double[] t = Profits( items1 );

MenuItem.java 存在 sytanx 问题 尝试 运行 下面的程序。


package com.Whosebug;

import java.util.ArrayList;

public class MenuItem 
{ 
  private String myName; 

  private double myPrice, 
                 myCost; 

  private int    myCode; 

  private boolean myAvailability; 

  public MenuItem( String name, double price, double cost, int code, boolean available ) 
  { 
    myName = name; 
    myPrice = price; 
    myCost = cost; 
    myCode = code; 
    myAvailability = available; 
  } 

  public String getName() { return myName; } 
  public double getPrice() { return myPrice; } 
  public double getCost() { return myCost; } 
  public int getCode() { return myCode; } 
  public boolean available() { return myAvailability; } 


  public String menuString() 
    { 
        return getName() + " ($" + getPrice() + ")"; 
    } 


  public static double roundMoney( double amount ) 
  { 
    return (int)(100 * amount + 0.5) / 100.0; 
  } 

  public static String printAmount( double d ) 
  { 
    String s = "" + d; 
    int k = s.indexOf( "." ); 
    if ( k < 0 ) 
      return s + ".00"; 
    if ( k + 1 == s.length() ) 
      return s + "00"; 
    if ( k + 2 == s.length() ) 
      return s + "0"; 
    else 
      return s; 
  } 

   //***********************************************************************
public static double[] profits(ArrayList<MenuItem> items) 
{
    double[] profits = new double[items.size()];
    for (int i = 0; i < profits.length; i++)
     {
        profits[i] = roundMoney (items.get(i).getPrice() - items.get(i).getCost()); 
     }
    return profits;
}
 //***********************************************************************
public static void main( String[] args )
{  
ArrayList<MenuItem> items1 = new ArrayList<MenuItem>();
items1.add( new MenuItem("Stir Fry",5.43,0.45,1,true) );
items1.add( new MenuItem("Nachos",3.49,0.15,0,false) );
items1.add( new MenuItem("Mud Pie",6.50,1.25,2,true) );
items1.add( new MenuItem("Jerk Chicken",8.99,3.20,1,false) );

double[] t = profits( items1 );

for ( double d : t )
  System.out.print( printAmount( d ) + " " );
}
}