我继续为Item []购物车收到这些错误

时间:2017-11-27 18:54:05

标签: java

我继续收到错误: -

C:\Users\TopFlight\Desktop\Fall 2017\CSC\111\Chapter 7\ShoppingCart.java:7: error: cannot find symbol

    import Shopping.Item;
               ^
  symbol:   class Item

  location: package Shopping
C:\Users\TopFlight\Desktop\Fall 2017\CSC\111\Chapter 7\ShoppingCart.java:16: error: cannot find symbol

    Item[] cart; //Declare an instance variable cart to be an array of Items
    ^
  symbol:   class Item

  location: class ShoppingCart


C:\Users\TopFlight\Desktop\Fall 2017\CSC\111\Chapter 7\ShoppingCart.java:26: error: cannot find symbol

        cart = new Item[capacity];
                   ^
  symbol:   class Item

  location: class ShoppingCart

C:\Users\TopFlight\Desktop\Fall 2017\CSC\111\Chapter 7\ShoppingCart.java:34: error: cannot find symbol

        Item temp = new Item(itemName, price, quantity);
        ^
  symbol:   class Item

  location: class ShoppingCart

C:\Users\TopFlight\Desktop\Fall 2017\CSC\111\Chapter 7\ShoppingCart.java:34: error: cannot find symbol

        Item temp = new Item(itemName, price, quantity);
                        ^
  symbol:   class Item

  location: class ShoppingCart

5 errors


Tool completed with exit code 1

代码

package Shopping;
import Shopping.Item;
import java.text.NumberFormat;

public class ShoppingCart
{

    private int itemCount; // total number of items in the cart
    private double totalPrice; // total price of items in the cart
    private int capacity; // current cart capacity
    Item[] cart; //Declare an instance variable cart to be an array of Items
    // ---------------------------------------------------------
    // Creates an empty shopping cart with a capacity of 5 items.
    // ---------------------------------------------------------
    public ShoppingCart()
    {

        capacity = 5;
        itemCount = 0;
        totalPrice = 0.0;
        cart = new Item[capacity];
    }

    // -----------------------------------------------------
   // Adds an item to the shopping cart.
    // -----------------------------------------------------
    public void addToCart(String itemName, double price, int quantity)
    {
        Item temp = new Item(itemName, price, quantity);
        totalPrice += (price * quantity);
        itemCount += quantity;
        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\tUnit Price\tQuantity\tTotal\n";
        for (int i = 0; i < itemCount; i++)
        contents += cart[i].toString() + "\n";
        contents += "\nTotal Price: " + fmt.format(totalPrice);
        contents += "\n";
        return contents;
    }

   // -----------------------------------------------------
   // Increases the capacity of the shopping cart by 3
    // -----------------------------------------------------
    private void increaseSize()
    {
       capacity = capacity *3;
    }
}

1 个答案:

答案 0 :(得分:-1)

我不需要导入任何包。 谢谢你的调查。

import java.text.NumberFormat;

public class ShoppingCart
{
    private int itemCount; // total number of items in the cart
    private double totalPrice; // total price of items in the cart
    private int capacity; // current cart capacity
    Item[] cart; //Declare an instance variable cart to be an array of Items
    // ---------------------------------------------------------
    // Creates an empty shopping cart with a capacity of 5 items.
    // ---------------------------------------------------------

    public ShoppingCart()
    {

        capacity = 5;
        itemCount = 0;
        totalPrice = 0.0;
        cart = new Item[capacity];
    }

    // -----------------------------------------------------
    // Adds an item to the shopping cart.
    // -----------------------------------------------------
    public void addToCart(String itemName, double price, int quantity)
    {
        Item temp = new Item(itemName, price, quantity);
        totalPrice += (price * quantity);
        itemCount += quantity;
        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\tUnit Price\tQuantity\tTotal\n";
        for (int i = 0; i < itemCount; i++)
        contents += cart[i].toString() + "\n";
        contents += "\nTotal Price: " + fmt.format(totalPrice);
        contents += "\n";
        return contents;
    }

    // -----------------------------------------------------
    // Increases the capacity of the shopping cart by 3
    // -----------------------------------------------------
    private void increaseSize()
    {
        capacity = capacity *3;
    }
}