了解数组项目

时间:2014-11-06 03:44:12

标签: java arrays list class

我有一个项目要求如下: 在本练习中,您将完成一个实现购物车作为项目数组的类。文件Item.java包含名为Item的类的定义,该类对要购买的项进行建模。商品具有名称,价格和数量(购买数量)。 ShoppingCart.java文件将购物车实现为Item对象的数组。

我总共有三个代码已经提供给我们并且我已经完成了它们: 我相信这些是课程:

// ***************************************************************
//   Item.java
//   Represents an item in a shopping cart.
// ***************************************************************

import java.text.NumberFormat;

public class Item
{
   private String name;
   private double price;
   private int quantity;


   // -------------------------------------------------------

   //  Create a new item with the given attributes.

   // -------------------------------------------------------

   public Item (String itemName, double itemPrice, int numPurchased)
   {
      name = itemName;
      price = itemPrice;
      quantity = numPurchased;
   }

   // -------------------------------------------------------
   //   Return a string with the information about the item
   // -------------------------------------------------------

   public String toString()
   {
      NumberFormat fmt = NumberFormat.getCurrencyInstance();

  return (name + "\t" + fmt.format(price) + "\t" + quantity + "\t"+ fmt.format(price*quantity));
   } 

   // -------------------------------------------------
   //   Returns the unit price of the item
   // -------------------------------------------------
   public double getPrice()
   {
      return price;
   }


   // -------------------------------------------------
   //   Returns the name of the item
   // -------------------------------------------------
   public String getName()
   {
      return name;
   }

    // -------------------------------------------------
    //   Returns the quantity of the item
    // -------------------------------------------------
    public int getQuantity()
    {
      return quantity;
    }

    // **********************************************************************
    //   ShoppingCart.java
    //   Represents a shopping cart as an array of items
    // **********************************************************************


   public class ShoppingCart
{
   private int itemCount;  //total number of items in the cart
   private double totalPrice;  //total prive of items in the cart
   private Item[] cart;
   private int capacity;

   // -----------------------------------------------------------
   //  Creates an empty shopping cart with a capacity of 5 items.
   // -----------------------------------------------------------
   public ShoppingCart()
   {
      cart = new Item[capacity];
      capacity = 5;
      itemCount = 0;
      totalPrice = 0.0;

   {

   // -------------------------------------------------------
   //  Adds an item to the shopping cart.  

   // Check to make sure there is room in the cart first

   // -------------------------------------------------------

   public void addToCart(String itemName, double price, int quantity)
   {
      Item temp = new Item(itemName, price, quantity);
      totalPrice += (price * quantity);
      itemCount += 1;
      cart[itemCount] = temp;
     if (itemCount==capacity)
  {
     increaseSize();
  }
   }

   // -------------------------------------------------------
   //  Returns the contents of the cart together with
   //  summary information.
   // -------------------------------------------------------
   public String toString()
   {
  NumberFormat fmt = NumberFormat.getCurrencyInstance();

      String contents = "\nShopping Cart\n";
      contents += "\nItem\t\Unit Price\tQuantity\tTotal\n";

       for (int i = 0; i < itemCount; i++)
      contents += cart[i] + "\n";
      contents += "\nTotal Price: " + fmt.format(totalPrice);
      contents += "\n";

      return contents;
    }

     // ---------------------------------------------------------
     //  Increases the capacity of the shopping cart by 3
     // ---------------------------------------------------------
     private void increaseSize()
     {
     Item[] temp = new Item[capacity+3];
     for(int i=0; i < capacity; i++)
     {
        tempt[i] = cart[i];
     }
     cart = temp;
     temp = null;
     capacity = cart.length;
  }

}

这是主要的:

//***************************************************************
//   Shop.java
//   Uses the Item class to create items and add them to a shopping
//   cart stored in a ShoppingCart class
// ***************************************************************
import java.util.Scanner;


import java.util.ArrayList;
public class Shop
{    

public static void main (String[] args)

{

  ArrayList<Item> cart = new ArrayList<Item>();


  Item item;

  String itemName;

  double itemPrice;

  int quantity;



  Scanner scan = new Scanner(System.in);

  String keepShopping = "y";
  ShoppingCart cart = new ShoppingCart();

  do

    {

     System.out.print("Enter the name of the item: ");
     itemName = scan.next();

     System.out.print("Enter the unit price: ");
     itemPrice = scan.nextDouble();

     System.out.print("Enter the quantity: ");
     quantity = scan.nextInt();
         // *** add this item to the cart by passing itemName, itemPrce, and quantity to the addToCart method.
     cart.addToCart(itemName, itemPrice, quantity);

         // *** print the contents of the cart object using the toString method of the ShoppingCart class.

     System.out.println(cart);

     System.out.print("Continue shopping (y/n)?");
     keepShopping = scan.next();


  }

      while (keepShopping.equals("y"));
      // *** print the final total of the grocery list with a “Please pay ...” in front of the toalPrice. 
   }
}

我的问题是我是否正确安排了代码?因为该类没有编译我得到非法启动错误,我遇到了主要问题。 任何帮助表示赞赏。

班级错误:

 ----jGRASP exec: javac -g Item.java
Item.java:95: error: illegal start of expression
public void addToCart(String itemName, double price, int quantity)
      ^
Item.java:95: error: illegal start of expression
public void addToCart(String itemName, double price, int quantity)
      ^
Item.java:95: error: ';' expected
public void addToCart(String itemName, double price, int quantity)
                    ^
Item.java:95: error: <identifier> expected
public void addToCart(String itemName, double price, int quantity)
                                     ^
Item.java:95: error: not a statement
public void addToCart(String itemName, double price, int quantity)
                                             ^
Item.java:95: error: ';' expected
public void addToCart(String itemName, double price, int quantity)
                                                  ^
Item.java:95: error: ';' expected
public void addToCart(String itemName, double price, int quantity)
                                                                ^
Item.java:111: error: illegal start of expression
public String toString()
^
Item.java:111: error: ';' expected
public String toString()
                     ^
Item.java:116: error: illegal escape character
  contents += "\nItem\t\Unit Price\tQuantity\tTotal\n";
                        ^
Item.java:130: error: illegal start of expression
 private void increaseSize()
 ^
Item.java:130: error: illegal start of expression
 private void increaseSize()
         ^
Item.java:130: error: ';' expected
 private void increaseSize()
                          ^
Item.java:147: error: reached end of file while parsing
}
 ^
14 errors

 ----jGRASP wedge: exit code for process is 1.
 ----jGRASP: operation complete.

主要方法错误:

 ----jGRASP exec: javac -g Shop.java
Item.java:95: error: illegal start of expression
public void addToCart(String itemName, double price, int quantity)
^
Item.java:95: error: illegal start of expression
public void addToCart(String itemName, double price, int quantity)
      ^
Item.java:95: error: ';' expected
public void addToCart(String itemName, double price, int quantity)
                    ^
Item.java:95: error: <identifier> expected
public void addToCart(String itemName, double price, int quantity)
                                     ^
Item.java:95: error: not a statement
public void addToCart(String itemName, double price, int quantity)
                                             ^
Item.java:95: error: ';' expected
public void addToCart(String itemName, double price, int quantity)
                                                  ^
Item.java:95: error: ';' expected
public void addToCart(String itemName, double price, int quantity)
                                                                ^
Item.java:111: error: illegal start of expression
public String toString()
^
Item.java:111: error: ';' expected
public String toString()
                     ^
Item.java:116: error: illegal escape character
  contents += "\nItem\t\Unit Price\tQuantity\tTotal\n";
                        ^
Item.java:130: error: illegal start of expression
 private void increaseSize()
 ^
Item.java:130: error: illegal start of expression
 private void increaseSize()
         ^
Item.java:130: error: ';' expected
 private void increaseSize()
                          ^
Item.java:147: error: reached end of file while parsing
 }
  ^
ShoppingCart.java:34: error: illegal start of expression
public void addToCart(String itemName, double price, int quantity)
^
ShoppingCart.java:34: error: illegal start of expression
public void addToCart(String itemName, double price, int quantity)
      ^
ShoppingCart.java:34: error: ';' expected
public void addToCart(String itemName, double price, int quantity)
                    ^
ShoppingCart.java:34: error: <identifier> expected
public void addToCart(String itemName, double price, int quantity)
                                     ^
ShoppingCart.java:34: error: not a statement
public void addToCart(String itemName, double price, int quantity)
                                             ^
ShoppingCart.java:34: error: ';' expected
public void addToCart(String itemName, double price, int quantity)
                                                  ^
ShoppingCart.java:34: error: ';' expected
public void addToCart(String itemName, double price, int quantity)
                                                                ^
ShoppingCart.java:50: error: illegal start of expression
public String toString()
^
ShoppingCart.java:50: error: ';' expected
public String toString()
                     ^
ShoppingCart.java:55: error: illegal escape character
  contents += "\nItem\t\Unit Price\tQuantity\tTotal\n";
                        ^
ShoppingCart.java:69: error: illegal start of expression
 private void increaseSize()
 ^
ShoppingCart.java:69: error: illegal start of expression
 private void increaseSize()
         ^
ShoppingCart.java:69: error: ';' expected
 private void increaseSize()
                          ^
ShoppingCart.java:82: error: reached end of file while parsing
}
^
Shop.java:33: error: variable cart is already defined in method main(String[])
  ShoppingCart cart = new ShoppingCart();
               ^
Shop.java:48: error: cannot find symbol
     cart.addToCart(itemName, itemPrice, quantity);
         ^
   symbol:   method addToCart(String,double,int)
   location: variable cart of type ArrayList<Item> 
   Item.java:103: error: cannot find symbol
     increaseSize();
     ^
   symbol:   method increaseSize()
   location: class Item.ShoppingCart
   Item.java:124: error: incompatible types: unexpected return value
   return contents;
         ^
   Item.java:135: error: cannot find symbol
        tempt[i] = cart[i];
        ^
   symbol:   variable tempt
   location: class Item.ShoppingCart
   ShoppingCart.java:36: error: cannot find symbol
      Item temp = new Item(itemName, price, quantity);
                                 ^
   symbol:   variable price
   location: class ShoppingCart
   ShoppingCart.java:37: error: cannot find symbol
  totalPrice += (price * quantity);
                 ^
   symbol:   variable price
   location: class ShoppingCart
    ShoppingCart.java:42: error: cannot find symbol
     increaseSize();
     ^
   symbol:   method increaseSize()
   location: class ShoppingCart
   ShoppingCart.java:63: error: incompatible types: unexpected return value
   return contents;
         ^
    ShoppingCart.java:74: error: cannot find symbol
        tempt[i] = cart[i];
        ^
   symbol:   variable tempt
   location: class ShoppingCart
   38 errors

----jGRASP wedge: exit code for process is 1.
----jGRASP: operation complete.

3 个答案:

答案 0 :(得分:0)

关于我的意见的几点建议:

ShoppingCart应该只保存Item数组对象。迭代Item数组对象以获得总价格,数量和容量。构造函数只能保留为接受项目数组。

将商品添加到购物车后,您可以调用购物车中定义的方法作为示例totalPrice,totalQuantity来获取项目所需的详细信息。

您可以参考以下链接: http://www.journaldev.com/1754/strategy-design-pattern-in-java-example-tutorial

答案 1 :(得分:0)

正如其中一条评论中所述,我看到的最大问题之一是你使用'cart'两次:一次作为ArrayList,一次作为ShoppingCart。这样做会使应用程序混淆,因为它不知道从哪里开始。

当您说'cart.addToCart(itemName,itemPrice,quantity);'时,应用程序将不会立即知道您是否正在尝试将信息添加到ArrayList或ShoppingCart。

另外,我发现你的方法存在一些问题。

在你的“toString()”方法中,我会重命名它。 “toString()”是一个允许您将某些变量转换为字符串的代码,它是一个已在Java中定义的代码。这就是为什么它会告诉你一个';'是期待;因为,通常是“thisVariable.toString();”。这有意义吗?

另外,仍然在你的“toString()”类中,我看到你输入了:

“contents + =”\ nItem \ t \ Unit Price \ tQuantity \ tTotal \ n“;”。

这应该改为:

“contents + =”\ nItem \ tUnit Price \ tQuantity \ tTotal \ n“;”。

你在这行中有一个额外的反斜杠。我认为应该解决这个问题。

答案 2 :(得分:0)

   public ShoppingCart()
   {
      cart = new Item[capacity];
      capacity = 5;
      itemCount = 0;
      totalPrice = 0.0;

您在ShoppingCart的构造函数上缺少结束括号'}'。它应该如下。

   public ShoppingCart()
   {
      cart = new Item[capacity];
      capacity = 5;
      itemCount = 0;
      totalPrice = 0.0;
   }