通过ArrayLists创建项目菜单

时间:2015-01-11 04:39:02

标签: java arraylist menu

所以我要做的就是使用ArrayLists创建食物菜单。我的任务是完成以下静态方法利润的定义,它输入MenuItems的ArrayList项并返回一个双精度数组。输出中的每个条目是相应元素项的价格和成本之间的差异。我必须使用静态方法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 =利润(item1); 有人可以帮我弄清楚出了什么问题吗?谢谢!

2 个答案:

答案 0 :(得分:3)

您使用大写字母“P。”定义函数profits

public static double[] Profits(ArrayList<MenuItem> items) 

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

double[] t = Profits( items1 );

答案 1 :(得分:-1)

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


package com.stackoverflow;

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 ) + " " );
}
}