gpt4 book ai didi

java - 购物车 Java 应用程序 (addToCart)

转载 作者:行者123 更新时间:2023-11-29 05:38:47 26 4
gpt4 key购买 nike

我正在为一个类做一个 Java 应用程序,需要帮助解决运行该应用程序后出现的错误。

该应用程序应该允许用户将商品添加到购物车(商品名称、价格、数量)。用户描述的商品会添加到购物车数组中,添加成功后会打印出来。

购物车最初的最大容量为 5 件商品,但如果购物车大小 > 5,它将通过 increaseSize 方法增加 + 3。

如下所述,我认为 addToCart 方法并未将用户描述的商品添加到购物车。这就是它在 toString 方法中为空值的原因。

所以,我基本上需要有关 addToCart 方法的帮助。如果我错了,请纠正我。谢谢。

运行该应用程序的输出如下:

run:
Enter the name of the item: a
Enter the unit price: 2
Enter the quantity: 2
Exception in thread "main" java.lang.NullPointerException
at shop.ShoppingCart.toString(ShoppingCart.java:62)
at java.lang.String.valueOf(String.java:2854)
at java.io.PrintStream.println(PrintStream.java:821)
at shop.Shop.main(Shop.java:49)
Java Result: 1

下面是 ShoppingCart 类。

toString 方法应该不会产生任何错误,但我认为问题出在 addToCart 方法中。

// **********************************************************************
// ShoppingCart.java
//
// Represents a shopping cart as an array of items
// **********************************************************************
import java.text.NumberFormat;

public class ShoppingCart
{

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

// -----------------------------------------------------------
// Creates an empty shopping cart with a capacity of 5 items.
// -----------------------------------------------------------
public ShoppingCart()
{

capacity = 5;
cart = new Item[capacity];
itemCount = 0;
totalPrice = 0.0;
}

// -------------------------------------------------------
// 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()
{
Item[] temp = new Item[capacity+3];
for(int i=0; i < capacity; i++)
{
temp[i] = cart[i];
}
cart = temp;
temp = null;
capacity = cart.length;
}
}

下面是主商店。

此时未使用 ArrayList,但会在更正 toString 错误后使用。

package shop;

// ***************************************************************
// Shop.java
//
// Uses the Item class to create items and add them to a shopping
// cart stored in an ArrayList.
// ***************************************************************

import java.util.ArrayList;
import java.util.Scanner;

public class Shop
{
public static void main (String[] args)
{
ArrayList<Item> cart = new ArrayList<Item>();

Item item;
String itemName;
double itemPrice;
int quantity;

Scanner scan = new Scanner(System.in);

String keepShopping = "y";
ShoppingCart cart1 = new ShoppingCart();
do
{
System.out.print ("Enter the name of the item: ");
itemName = scan.next();

System.out.print ("Enter the unit price: ");
itemPrice = scan.nextDouble();

System.out.print ("Enter the quantity: ");
quantity = scan.nextInt();

// *** create a new item and add it to the cart
cart1.addToCart(itemName, itemPrice, quantity);



// *** print the contents of the cart object using println
System.out.println(cart1);

System.out.print ("Continue shopping (y/n)? ");
keepShopping = scan.next();
}
while (keepShopping.equals("y"));

}
}

下面是项目类:

package shop;

// ***************************************************************
// 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;
}
}

最佳答案

我认为问题是由 addToCart() 中的这两行引起的:

itemCount += quantity;
cart[itemCount] = temp;

除非 quantity0,这意味着 cart[0] 永远不会包含任何东西。事实上,无论 quantity 的值是多少,您都会在 cart 中留下那么多空白(例如,如果 quantity 为 2,cart [0]cart[1] 将为空)。

由于 Item 已经包含了数量,我相信你打算这样做:

cart[itemCount] = temp;
itemCount += 1;

这样,第一个项目将放入 cart[0],第二个项目将放入 cart[1],依此类推。

另一条小建议:如果您将任何对象与字符串连接,则无需对该对象调用 toString()。 Java 会自动调用 String.valueOf()在对象上,这避免了 NullPointerException,因为它返回的是 String "null"。在这种情况下,它可能会帮助您调试问题,因为您会注意到输出中出现了空字符串。您必须将代码更改为以下内容:

for (int i = 0; i < itemCount; i++)
contents += cart[i] + "\n";

关于java - 购物车 Java 应用程序 (addToCart),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18473130/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com