Java 我不断收到商品[]购物车的这些错误
我不断收到这些错误信息:-Java 我不断收到商品[]购物车的这些错误,java,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\
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;
}
}
套餐购物;
进口购物物品;
导入java.text.NumberFormat;
公共类购物车
{
private int itemCount;//购物车中的项目总数
private double totalPrice;//购物车中物品的总价
private int capacity;//当前购物车容量
Item[]cart;//将实例变量cart声明为项目数组
// ---------------------------------------------------------
//创建一个可容纳5件物品的空购物车。
// ---------------------------------------------------------
公共购物车()
{
容量=5;
itemCount=0;
总价=0.0;
购物车=新项目[容量];
}
// -----------------------------------------------------
//将项目添加到购物车。
// -----------------------------------------------------
public void addToCart(字符串itemName、双倍价格、整数数量)
{
项目温度=新项目(项目名称、价格、数量);
总价+=(价格*数量);
itemCount+=数量;
购物车[项目计数]=临时;
如果(itemCount==容量)
{
增加大小();
}
}
// -----------------------------------------------------
//返回购物车的内容以及
//摘要信息。
// -----------------------------------------------------
公共字符串toString()
{
NumberFormat fmt=NumberFormat.getCurrencyInstance();
String contents=“\n购物车\n”;
contents+=“\nItem\t\t价格\t质量\t总计\n”;
对于(int i=0;i
我不需要导入任何包。
谢谢你的调查
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;
}
}
导入java.text.NumberFormat;
公共类购物车
{
private int itemCount;//购物车中的项目总数
private double totalPrice;//购物车中物品的总价
private int capacity;//当前购物车容量
Item[]cart;//将实例变量cart声明为项目数组
// ---------------------------------------------------------
//创建一个可容纳5件物品的空购物车。
// ---------------------------------------------------------
公共购物车()
{
容量=5;
itemCount=0;
总价=0.0;
购物车=新项目[容量];
}
// -----------------------------------------------------
//将项目添加到购物车。
// -----------------------------------------------------
public void addToCart(字符串itemName、双倍价格、整数数量)
{
项目温度=新项目(项目名称、价格、数量);
总价+=(价格*数量);
itemCount+=数量;
购物车[项目计数]=临时;
如果(itemCount==容量)
{
增加大小();
}
}
// -----------------------------------------------------
//返回购物车的内容以及
//摘要信息。
// -----------------------------------------------------
公共字符串toString()
{
NumberFormat fmt=NumberFormat.getCurrencyInstance();
String contents=“\n购物车\n”;
contents+=“\nItem\t\t价格\t质量\t总计\n”;
对于(int i=0;i
看起来您缺少一个包含类项目的文件Item.java?Item类和ShoppingCart类是否在同一文件夹中?是的,它们在同一文件夹中