如何打印数组列表中的每个对象并正确计算它们的数量?
How do I print every object in an arraylist and correctly count their quantity?
我有一个 class,它采用由名称、价格和数量确定的项目列表,并且应该打印每个唯一项目并相应地增加数量,如果添加了多个相同的项目数组列表。
我当前的代码有 2 个问题:首先,它只打印数组列表中的最后一项。其次,它 returns 印刷品的数量不正确。
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()
方法出了点问题,但我不知道是什么。
在 toString()
方法中,您有一个 for 循环,但您只是将最后一项数据保留在循环中。你应该这样更正它:
String name = "";
for (Product p: cart) {
quantity += p.getQuantity();
price += p.getPrice();
name += p.getName();
total += p.getTotalPrice();
}
您需要实现 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
时,它会影响列表中引用的初始对象,您需要在列表中添加一个副本,同时更改循环:当您找到相同的产品,然后更改数量 return,**仅在循环结束时*如果您没有找到要添加的产品,您需要等待检查所有现有产品:
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
class中,一个复制构造函数:
public Product(Product p) {
this.quantity = p.quantity;
this.price = p.price;
this.name = p.name;
}
还有,不要把列表做成静态的,每个shoppingCart都有自己的列表
private ArrayList<Product> cart;
public ShoppingCart() {
cart = new ArrayList<>();
}
我有一个 class,它采用由名称、价格和数量确定的项目列表,并且应该打印每个唯一项目并相应地增加数量,如果添加了多个相同的项目数组列表。
我当前的代码有 2 个问题:首先,它只打印数组列表中的最后一项。其次,它 returns 印刷品的数量不正确。
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()
方法出了点问题,但我不知道是什么。
在 toString()
方法中,您有一个 for 循环,但您只是将最后一项数据保留在循环中。你应该这样更正它:
String name = "";
for (Product p: cart) {
quantity += p.getQuantity();
price += p.getPrice();
name += p.getName();
total += p.getTotalPrice();
}
您需要实现
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
时,它会影响列表中引用的初始对象,您需要在列表中添加一个副本,同时更改循环:当您找到相同的产品,然后更改数量 return,**仅在循环结束时*如果您没有找到要添加的产品,您需要等待检查所有现有产品:
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
class中,一个复制构造函数:
public Product(Product p) {
this.quantity = p.quantity;
this.price = p.price;
this.name = p.name;
}
还有,不要把列表做成静态的,每个shoppingCart都有自己的列表
private ArrayList<Product> cart;
public ShoppingCart() {
cart = new ArrayList<>();
}