对存储在ArrayList(Java)中的对象的所有双精度值求和
我有一个包含对象的ArrayList。每个对象都有3个值:字符串名称、双倍价格、整数数量。如何编写将所有对象的两倍相加并打印结果的方法。而且,如果int quantity>1,则价格将乘以数量对存储在ArrayList(Java)中的对象的所有双精度值求和,java,arraylist,Java,Arraylist,我有一个包含对象的ArrayList。每个对象都有3个值:字符串名称、双倍价格、整数数量。如何编写将所有对象的两倍相加并打印结果的方法。而且,如果int quantity>1,则价格将乘以数量 public double sumProducts() { double sum = 0; for (int i = 0; i < newList.size(); i++) { Product product = newList.get(i); sum
public double sumProducts() {
double sum = 0;
for (int i = 0; i < newList.size(); i++) {
Product product = newList.get(i);
sum += product.getPrice() * product.getQuantity();
}
return sum;
}
到目前为止我编写的代码:
产品类别
public class Product {
private String name;
private double price;
private int quantity;
public Product(String name, double price, int quantity) {
this.name = name;
this.price = price;
}
public String getName() {
return name;
}
public double getPrice() {
return price;
}
public static Product createProduct(String name, double price, int quantity){
return new Product(name, price, quantity);
}
}
产品列表类
import java.util.ArrayList;
import java.util.List;
public class ProductList {
private String name;
List<Product> newList;
public ProductList(String name) {
this.name = name;
this.newList = new ArrayList<>();
}
public boolean addNewProduct(Product product) {
if (findProduct(product.getName()) >= 0) {
System.out.println("Product is already on the list");
return false;
}
newList.add(product);
return true;
}
public boolean removeProduct(Product product) {
if (findProduct(product.getName().toUpperCase()) < 0) {
System.out.println("Product not found");
return false;
}
newList.remove(product);
return true;
}
private int findProduct(String productName) {
for (int i = 0; i < newList.size(); i++) {
Product product = newList.get(i);
if (product.getName().equals(productName)) {
return i;
}
}
return -1;
}
public Product queryProduct(String name) {
int position = findProduct(name);
if (position >= 0) {
return this.newList.get(position);
}
return null;
}
public double sumProducts() {
double sum = 0;
for (int i = 0; i < newList.size(); i++) {
sum += newList.get(i).getPrice();
}
return sum;
}
/*public boolean listProducts(){};
public boolean updateProduct(){};
*/
}
提前感谢每种产品的总数乘以其数量
public double sumProducts() {
double sum = 0;
for (int i = 0; i < newList.size(); i++) {
Product product = newList.get(i);
sum += product.getPrice() * product.getQuantity();
}
return sum;
}
public-double-sumProducts(){
双和=0;
对于(int i=0;i
您可以使用Java 8在一行中完成
public double sumProducts() {
return newList.stream().mapToDouble(product -> product.getPrice() * product.getQuantity()).sum();
}
如果您使用
double
存储价格,则在尝试将值相加和相乘时,您将得到错误的答案。例如,0.1+0.2
与0.3
不同。如果您想要精确计算十进制数,应该使用BigDecimal
类代替double
。如果你不这样做,我可以保证你的程序有时会给出错误的答案
因此,您需要如下更改产品
类
public class Product {
private String name;
private BigDecimal price;
private int quantity;
public Product(String name, BigDecimal price, int quantity) {
this.name = name;
this.price = price;
}
public String getName() {
return name;
}
public BigDecimal getPrice() {
return price;
}
public static Product createProduct(String name, BigDecimal price, int quantity){
return new Product(name, price, quantity);
}
}
您还需要在调用此类方法的代码中进行相应的更改
完成后,可以使用BigDecimal
类的方法进行算术运算。它可能看起来像这样
public BigDecimal calculateTotalPrice() {
BigDecimal total = BigDecimal.ZERO;
for (Product product : newList) {
BigDecimal linePrice = product.getPrice().multiply(new BigDecimal(product.getQuantity()));
total = total.add(linePrice);
}
return total;
}
这看起来更像是一个代码编写请求,而不是一个特定的适当问题。你的尝试在哪里?您的尝试有什么问题?
products.stream().mapToDouble(p->p.getPrice()*p.getQuantity()).sum()
您已经在sumProducts()
中编写了求和方法,那么您的问题是如何打印出总和?数量始终为0,有时使用常规名称调用findProduct()
,有时使用大写名称。此外,您不需要使用C样式for循环来循环对象列表;您可以使用增强的for-loop。如果您想要精确计算,您确实意识到对于一笔钱来说,double
不是合适的数据类型,对吗?您不能将double
值相加或相乘,并期望它们的行为类似于小数。BigDecimal
课程更适合你。谢谢大家的回答,我会努力找出答案。谢谢你的建议和分享你的知识。我一定会给你建议的做些改变。