Advertisement
tr00per92

OrderOfProducts

May 17th, 2014
431
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 1.06 KB | None | 0 0
  1. import java.io.BufferedWriter;
  2. import java.io.FileReader;
  3. import java.io.FileWriter;
  4. import java.util.ArrayList;
  5. import java.util.Locale;
  6. import java.util.Scanner;
  7.  
  8. public class _10_OrderOfProducts {
  9.  
  10.     public static void main(String[] args) throws Exception {
  11.         Locale.setDefault(Locale.ROOT);
  12.         ArrayList<Product> products = new ArrayList<Product>();
  13.         try (Scanner input = new Scanner(new FileReader("Products.txt"))) {        
  14.             while (input.hasNextLine()) {
  15.                 products.add(new Product(input.next(), input.nextDouble()));
  16.             }              
  17.         }
  18.         double result = 0;
  19.         try (Scanner input = new Scanner(new FileReader("Order.txt"))) {           
  20.             while (input.hasNextLine()) {
  21.                 double quantity = input.nextDouble();
  22.                 String currentProduct = input.next();
  23.                 for (Product product : products) {
  24.                     if (product.getName().equals(currentProduct)) {
  25.                         result += quantity * product.getPrice();
  26.                     }
  27.                 }
  28.             }              
  29.         }
  30.         try (BufferedWriter output = new BufferedWriter(new FileWriter("Output.txt"))) {
  31.             output.write(String.format("%.2f", result));
  32.         }
  33.     }
  34. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement