在本练习中,您将完成一个实现购物车作为项目数组的类。文件Item.java包含名为Item的类的定义,该类对要购买的项进行建模。商品具有名称,价格和数量(购买数量)。 ShoppingCart.java文件将购物车实现为Item对象的数组。
通过执行以下操作完成ShoppingCart类:将实例变量cart声明为Items数组,并将构造函数中的cart实例化为包含容量Items的数组。湾填写increaseSize方法的代码。您的代码应与文本清单7.8中的代码类似,但不是将大小加倍,只需将其增加3个元素即可。 C。填写addToCart方法的代码。此方法应将项目添加到购物车并更新totalPrice实例变量(请注意此变量会考虑数量)。 d。编译你的课程。
编写一个模拟购物的程序。只要用户想购物,程序就应该有一个循环。每次循环读取用户想要添加到购物车的商品的名称,价格和数量。将商品添加到购物车后,应打印购物车内容。在循环之后打印“请付款...”消息,其中包含购物车中商品的总价。
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.lang.StackOverflowError中的异常 在Shopping.ShoppingCart.addToCart(ShoppingCart.java:39) 是我收到的错误
答案 0 :(得分:0)
您一直在调用“{1}}方法”#else;&#39;在同一方法中的语句,它没有做任何事情(只是一直调用相同的代码块,然后以addToCart()
终止)。我非常确定您要将创建的StackOverflowError
添加到数组中:
Item
您还需要修改&#39; if&#39; else {
cart[itemCount] = new Item(itemName, price, quantity);
totalPrice += (price * quantity);
itemCount++; // Same thing as itemCount = itemCount + 1
}
中的条件,因为现在它会将项目放在数组槽addToCart()
中,但您的数组索引会停在capacity
。解决它:
capacity - 1