- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我正在为一个类做一个 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;
除非 quantity
是 0
,这意味着 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/
我必须为 addToCart 方法创建代码才能将项目添加到 itemList。我在这里查看了其他 ShoppingCart 问题,但我没有看到一个会增加商品数量并检查商品名称以查看其是否在数组中的问题
我有一个表格,其中每行/项目都有一个 addtocart 按钮 $( $table ).delegate(".Addtocart", "click", function() {
我正在为一个类做一个 Java 应用程序,需要帮助解决运行该应用程序后出现的错误。 该应用程序应该允许用户将商品添加到购物车(商品名称、价格、数量)。用户描述的商品会添加到购物车数组中,添加成功后会打
我正在尝试制作一个组件,但这是说我没有定义“addToCart”。我已经在同一组件中定义了它,但是我仍然遇到相同的错误吗? Vue.component("solid-back",{ templ
我是一名优秀的程序员,十分优秀!