[英]How do I print every object in an arraylist and correctly count their quantity?
我有一個類,它采用由名稱、價格和數量確定的項目數組列表,並且如果將多個相同項目添加到數組列表中,則應該打印每個唯一項目並相應地增加數量。
我當前的代碼有兩個問題:首先,它只打印數組列表中的最后一項。 其次,它返回的打印項目數量不正確。
package shop;
import java.text.DecimalFormat;
import java.util.ArrayList;
public class ShoppingCart {
static ArrayList<Product> cart;
public ShoppingCart() {
ShoppingCart.cart = new ArrayList<Product>();
}
@Override
public String toString() {
DecimalFormat format = new DecimalFormat ("#.00");
int quantity = 0;
double price = 0;
String name = null;
double total = 0;
for (Product p: cart) {
quantity = p.getQuantity();
price = p.getPrice();
name = p.getName();
total = p.getTotalPrice();
}
return quantity + " * GBP " + format.format(price) + " " + name + " = GBP " + format.format(total);
}
public void add(Product p) {
if(cart.size() > 0) {
for (int i = 0; i < cart.size(); i++) {
if(cart.get(i).getName().equals(p.getName())
&& cart.get(i).getPrice() == p.getPrice()) {
cart.get(i).setQuantity(cart.get(i).getQuantity() + p.getQuantity());
}else {
cart.add(p);
}
}
}else {
cart.add(p);
}
}
public static void main(String[] args) {
ShoppingCart newCart = new ShoppingCart();
Product apple, apples, milk, caulk, ice, snakes;
apple = new Product("Apples (4 pack)", 1.20, 1);
apples = new Product("Apples (4 pack)", 1.20, 2);
milk = new Product("Milk (1l)", 0.75, 1);
caulk = new Product("Caulk (1l)", 6.84, 1);
ice = new Product("Ice (1kg)", 4.30, 1);
snakes = new Product("Snake (5m)", 32.0, 1);
newCart.add(apple);
newCart.add(apple);
newCart.add(apple);
newCart.add(apple);
newCart.add(caulk);
newCart.add(milk);
System.out.println(newCart);
}
}
輸出是
4 * GBP .75 Milk (1l) = GBP 3.00
我猜我的toString()
和add()
方法出了問題,但我不知道是什么。
例如,您需要實現Product.toString()
如下:
@Override public String toString() { DecimalFormat format = new DecimalFormat("#.00"); return String.format("%d * GBP %5s %15s= GBP %5s", quantity, format.format(price), name, format.format(price * quantity)); }
而ShoppingCart.toString()
將使用每個Product
Product.toString()
:
@Override public String toString() { double total = 0; StringBuilder sb = new StringBuilder(); for (Product p : cart) { sb.append(p.toString()).append("\\n"); total += p.getTotalPrice(); } sb.append(String.format("%s%33.2f", "Total :", total)); return sb.toString(); }
最后你會得到:
8 * GBP 1,20 Apples (4 pack)= GBP 9,60 2 * GBP 6,84 Caulk (1l)= GBP 13,68 4 * GBP ,75 Milk (1l)= GBP 3,00 4 * GBP ,75 Milk (1l)= GBP 3,00 Total : 29,28
和現在一樣,當您設置新quantity
它會影響列表中引用的初始對象,您需要在列表中添加一個副本,同時更改循環:當您找到相同的產品時,更改數量然后返回,並且**僅在循環結束時*如果您還沒有找到要添加的產品,則需要等待檢查所有現有產品:
public void add(Product p) {
if (cart.size() > 0) {
for (Product product : cart) {
if (product.getName().equals(p.getName()) && product.getPrice() == p.getPrice()) {
product.setQuantity(product.getQuantity() + p.getQuantity());
return;
}
}
cart.add(new Product(p));
} else {
cart.add(new Product(p));
}
}
在Product
類中,復制構造函數:
public Product(Product p) {
this.quantity = p.quantity;
this.price = p.price;
this.name = p.name;
}
另外,不要將列表設為靜態,每個購物車都有自己的列表
private ArrayList<Product> cart;
public ShoppingCart() {
cart = new ArrayList<>();
}
在toString()
方法中,您有一個 for 循環,但您只是將最后一項數據保留在循環中。 你應該像這樣更正它:
String name = "";
for (Product p: cart) {
quantity += p.getQuantity();
price += p.getPrice();
name += p.getName();
total += p.getTotalPrice();
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.