2017-05-09 50 views
-2

我有一个包含对象的ArrayList。每个对象都有3个值:字符串名称,双倍价格,int数量。如何编写方法,将对所有对象的双精度进行求和并打印结果。而且如果int数量> 1,价格将乘以数量。总结存储在ArrayList(Java)中的对象的所有double值

代码,我写了这么远:

产品类

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 class Simulation { 

private static Scanner scanner = new Scanner(System.in); 
private static ProductList myProductList = new ProductList("My list"); 

private static void addNewProduct() { 
    System.out.println("Enter new product name: "); 
    String name = scanner.nextLine(); 
    System.out.println("Enter new product price: "); 
    double price = scanner.nextDouble(); 
    System.out.println("Enter new product quantity"); 
    int quantity = scanner.nextInt(); 
    Product newProduct = Product.createProduct(name, price, quantity); 
    if (myProductList.addNewProduct(newProduct) == true) { 
     System.out.println("New product added: " + name + " | price: " + price + " | quantity: " + quantity); 
    } 
} 

private static void removeProduct() { 
    System.out.println("Enter product name: "); 
    String name = scanner.nextLine().toUpperCase(); 
    Product existingProduct = myProductList.queryProduct(name); 
    if (existingProduct == null) { 
     System.out.println("No such product"); 
     return; 
    } 
    if (myProductList.removeProduct(existingProduct)) { 
     System.out.println("Sucessfully deleted product: " + existingProduct.getName()); 
    } else { 
     System.out.println("Error deleting"); 
    } 

} 

private static void printActions() { 
    System.out.println("Avaiable actions"); 
    System.out.println("press: "); 
    System.out.println("0 - to shut down\n" + 
      "1 - to add new product\n" + 
      "2 - to remove product\n" + 
      "3 - to sum all products"); 
} 

private static void sumProducts(){ 
    myProductList.sumProducts(); 
} 


public static void main(String[] args) { 

    printActions(); 

    boolean quit = false; 
    while (!quit) 
     try { 
      System.out.println("\nEnter action: "); 
      int action = scanner.nextInt(); 
      scanner.nextLine(); 
      switch ((action)) { 
       case 0: 
        System.out.println("\nShutting down..."); 
        quit = true; 
        break; 
       case 1: 
        addNewProduct(); 
        break; 
       case 2: 
        removeProduct(); 
        break; 

      } 

     } catch (InputMismatchException e) { 
      System.out.println("Bad key pressed, only values form 0 to 2 accepted"); 
      scanner.nextLine(); 
     } 

} 

} 

在此先感谢

+2

这看起来更像是一个代码写入请求,而不是一个特定的适当问题。你的企图在哪里?你的尝试有什么问题? –

+0

'products.stream()。mapToDouble(p - > p.getPrice()* p.getQuantity())。sum()' – shmosel

+0

您已经编写了方法将它们汇总在'sumProducts()问题是如何打印总和?数量始终为0,您有时会使用常规名称调用findProduct(),有时使用大写名称。此外,您不需要使用C样式的循环来遍历对象列表;您可以使用增强型for-loop。 –

回答

0

每件产品的总和缺失乘以其数量。

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; 
} 
0

您可以使用Java 8

public double sumProducts() { 
    return newList.stream().mapToDouble(product -> product.getPrice() * product.getQuantity()).sum(); 
} 
0

做在一个行如果您使用double存储的价格,当您尝试添加和繁殖的值,你会得到不正确的答案。例如,0.1 + 0.20.3不一样double。如果要精确计算十进制数,则应使用BigDecimal类代替double。如果你不这样做,我可以保证你的程序有时会给出错误的答案。

所以你需要改变你的Product类如下。

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; 
} 
+0

感谢您的建议和分享您的知识。我一定会做出你所建议的改变。 –

相关问题