我试图调试它,但我不知道我丢失了什么代码。介意检查我?我想将项目名称,价格和数量添加到ITEM[]
数组中,但是当我尝试打印时,它不会返回任何内容
item.java
import java.text.NumberFormat;
public class Item {
private String name;
private double price;
private int quantity;
public Item(String itemName, double itemPrice, int numPurchased) {
name = itemName;
price = itemPrice;
quantity = numPurchased;
}
public String toString() {
NumberFormat fmt = NumberFormat.getCurrencyInstance();
return (name + "\t" + fmt.format(price) + "\t" + quantity + "\t"
+ fmt.format(price * quantity));
}
public double getPrice() {
return price;
}
public String getName() {
return name;
}
public int getQuantity() {
return quantity;
}
}
ShoppingCart.java
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
private Item[] cart = new Item[capacity];
// -----------------------------------------------------------
// Creates an empty shopping cart with a capacity of 5 items.
// -----------------------------------------------------------
public ShoppingCart()
{
capacity = 5;
itemCount = 0;
totalPrice = 0.0;
}
// -------------------------------------------------------
// Adds an item to the shopping cart.
// -------------------------------------------------------
public void addToCart(String itemName, double price, int quantity)
{if(itemCount==cart.length)
increaseSize();
else{
cart[itemCount]=new Item(itemName, price, quantity);
totalPrice += (totalPrice * quantity);
itemCount++;
}
}
// -------------------------------------------------------
// 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[cart.length + 3];
for (int num = 0; num < cart.length; num++)
{
temp[num] = cart[num];
cart = temp;
}
}
}
Shopping1.java
import java.util.Scanner;
public class Shopping1 {
public static void main(String[] args)
{
String answer="y";
ShoppingCart cart = new ShoppingCart();
while(answer=="y")
{
System.out.println("Enter the Name, Price, and Quantity of Item:");
Scanner scan = new Scanner (System.in);
String name = scan.next();
double price = scan.nextDouble();
int quantity = scan.nextInt();
cart.addToCart(name, price, quantity);
String printout = cart.toString();
System.out.println(printout);
System.out.println("Do you want to continue? y/n");
Scanner ans = new Scanner (System.in);
answer = ans.next();
}
System.out.println("thank you !");
}
}
我的输出是:
输入物品的名称,价格和数量: yuvin
3
2
Shopping Cart
Item Unit Price Quantity Total
Total Price: MYR0.00
你想继续吗? Y / N
ñ
谢谢!
虽然我期待一些我刚刚插入的数组的返回
答案 0 :(得分:0)
此行中totalPrice始终为0:
totalPrice += (totalPrice * quantity);
我想你想要:
totalPrice + =(price * quantity);
答案 1 :(得分:0)
行
private Item[] cart = new Item[capacity];
在构造函数之前执行,因此当时容量将为零,并且您将获得一个没有元素空间的数组...
//编辑:
此外,addToCart方法存在缺陷......
if(itemCount == cart.length)为true,您要添加的项目将被丢弃,因为您只增加了数组大小...您应该增加数组大小然后添加项目... < / p>
答案 2 :(得分:0)
当您实例化购物车时:
private Item[] cart = new Item[capacity];
容量为空。 (设置 NullPointerException )。当你去实例化它时,你需要确保容量除了null之外还有某种类型的值。如何将购物车的尺寸设为0,因此您无法实际添加任何内容。
考虑使用ArrayList,因此大小是动态的?
答案 3 :(得分:0)
您应该单独测试每个方法,并确保它符合您的预期,而不是尝试将程序调试为单个质量。例如,这种方法:
private void increaseSize()
{
Item[] temp = new Item[cart.length + 3];
for (int num = 0; num < cart.length; num++)
{
temp[num] = cart[num];
cart = temp;
}
}
没有做你想做的事情:它会增加cart
的大小,是的,但它也会将cart
的每个元素设置为null
,{{1}除外}。如果你测试过这种方法,你会看到这个问题。