在本练习中,您将完成一个将购物车实现为项目数组的类。文件项。java包含名为Item的类的定义,该类对人们将购买的商品进行建模。项目有名称、价格和数量(购买的数量)。文件购物车。java将购物车实现为一个项目对象数组。
>
编写一个模拟购物的程序。该程序应该有一个循环,只要用户想购物就可以继续。每次通过循环读取用户想要添加到购物车的商品的名称、价格和数量。将项目添加到购物车后,应打印购物车内容。循环后打印“请付款…”包含购物车中商品总价的消息。
package Shopping;
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;
}
}
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 for an array of Item
// ---------------------------------------------------------
// 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)
{
if (itemCount > 5)
{
System.out.println("Now the shopping cart is full.");
}
else
{
addToCart(itemName, price, quantity);
totalPrice = totalPrice + (price * quantity);
}
itemCount = itemCount+1;
}
// -----------------------------------------------------
// 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;
}
}
线程“main”java中出现异常。郎在购物。购物车。addToCart(ShoppingCart.java:39)是我一直收到的错误
在同一个方法中,您在“else”语句中不断调用addToCart()
方法,但它什么也没做(只是不断调用同一个代码块,然后以堆栈溢出错误终止)。我很确定您是想将创建的项目添加到数组中:
else {
cart[itemCount] = new Item(itemName, price, quantity);
totalPrice += (price * quantity);
itemCount++; // Same thing as itemCount = itemCount + 1
}
您还需要修改addToCart()中的“if”条件,因为现在它试图将项目放置在数组槽中,但数组索引停止在容量-1。要修复它:
if(itemCount > capacity - 1){