错误,我不知道为什么?

时间:2013-06-07 05:40:06

标签: java shopping-cart

这是我的主要方法。出于某种原因,它找不到我的购物车课,并且给我一个错误。

package goshopping;

import java.util.Scanner;

public class Shopping 
{
    static final Scanner scanner = new Scanner(System.in);
    public static void main (String[] args)
    {
        ShoppingCart cart=new ShoppingCart();
        String name;
        double price;
        int quantity;
        String shopMore;
        do
        {
            System.out.print("Please enter the name of item: ");
            name=scanner.nextLine();
       System.out.print("Please enter the price of the item: ");
       price=scanner.nextDouble();
       scanner.nextLine();
       System.out.print("Please enter the quantity of the item: ");
       quantity=scanner.nextInt();
       scanner.nextLine();
       cart.addToCart(name, price, quantity);
       System.out.println(cart.toString());
       System.out.print("Shop some more? Enter Y for yes or N for no ");
       shopMore=scanner.nextLine();
  }
  while(shopMore.charAt(0)=='Y'||shopMore.charAt(0)=='y');
   }
}

这是我的第二个类ShoppingCart.java,它说它找不到变量,但我已经实例化了它们......

package shoppingcart;

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

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
    private Item[] cart;

// -----------------------------------------------------------
// 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)
{
    cart[itemCount]=new Item(itemName,price,quantity);
    totalPrice+=price;
    itemCount++;
    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()
{
    Item[] temp=new Item[capacity+3];
    System.arraycopy(cart, 0, temp, 0, capacity);
    cart=temp;

}
}

这是我的第三个名为Item.java的类这个类很好没有错误

package 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;
}
}

4 个答案:

答案 0 :(得分:2)

Import ShoppingCart类文件中的类Shopping

import shoppingcart.ShoppingCart;

答案 1 :(得分:0)

如果您使用的是Eclipse,只需Ctrl + Shift + O

答案 2 :(得分:0)

你必须在购物类中导入ShoppingCart。

下面

package goshopping;

在您的购物类中添加此内容。

import shoppingcart.ShoppingCart;

答案 3 :(得分:0)

您需要导入shoppingcart包

import shoppingcart.ShoppingCart;