0

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

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

4 回答 4

2

ShoppingCartShopping类文件中导入类。

import shoppingcart.ShoppingCart;
于 2013-06-07T05:41:06.790 回答
0

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

于 2013-06-07T05:42:57.347 回答
0

你必须在你的 Shopping 类中导入 ShoppingCart。

以下

package goshopping;

在你的购物类中添加这个。

import shoppingcart.ShoppingCart;
于 2013-06-07T05:43:16.810 回答
0

您将需要导入购物车包

import shoppingcart.ShoppingCart;
于 2013-06-07T05:43:53.863 回答