Advertisement
Guest User

Main class

a guest
May 21st, 2014
95
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 1.38 KB | None | 0 0
  1. import java.io.File;
  2. import java.io.FileNotFoundException;
  3. import java.io.PrintWriter;
  4. import java.math.BigDecimal;
  5. import java.text.DecimalFormat;
  6. import java.util.Locale;
  7. import java.util.Scanner;
  8. import java.util.TreeSet;
  9.  
  10. import com.sun.org.apache.bcel.internal.generic.NEW;
  11.  
  12.  
  13.  
  14. public class Main {
  15.  
  16.     public static void main(String[] args) {
  17.         Locale.setDefault(Locale.ROOT);
  18.         double totalSum = 0;
  19.         TreeSet<Product> products = new TreeSet<Product>();
  20.         try {
  21.             Scanner sc = new Scanner(new File("Products.txt"));
  22.             while (sc.hasNext()) {
  23.                 products.add(new Product(sc.next(), sc.nextDouble()));
  24.             }
  25.             sc.close();
  26.         } catch (FileNotFoundException e) {
  27.             e.printStackTrace();
  28.         }
  29.        
  30.         try {
  31.             Scanner sc = new Scanner(new File("Order.txt"));
  32.             while (sc.hasNext()) {
  33.                 double quantity = sc.nextDouble();
  34.                 String productName = sc.next();
  35.                 for (Product product : products) {
  36.                     if (product.equals(new Product(productName, 0))) {
  37.                         totalSum += quantity * product.getPrice();
  38.                     }
  39.                 }
  40.             }
  41.             sc.close();
  42.         } catch (FileNotFoundException e) {
  43.             e.printStackTrace();
  44.         }
  45.        
  46.         try {
  47.             PrintWriter pw = new PrintWriter(new File("Output.txt"));
  48.             DecimalFormat formatter = new DecimalFormat("#0.00");
  49.             pw.println(formatter.format(totalSum));
  50.             pw.close();
  51.         } catch (FileNotFoundException e) {
  52.             e.printStackTrace();
  53.         }
  54.        
  55.     }
  56.  
  57. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement