Advertisement
tr00per92

ListOfProducts

May 17th, 2014
506
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 0.84 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.Collections;
  6. import java.util.Locale;
  7. import java.util.Scanner;
  8.  
  9. public class _09_ListOfProducts {
  10.  
  11.     public static void main(String[] args) throws Exception {
  12.         Locale.setDefault(Locale.ROOT);
  13.         ArrayList<Product> products = new ArrayList<Product>();
  14.         try (Scanner input = new Scanner(new FileReader("Input.txt"))) {           
  15.             while (input.hasNextLine()) {
  16.                 products.add(new Product(input.next(), input.nextDouble()));
  17.             }              
  18.         }
  19.         Collections.sort(products);
  20.         try (BufferedWriter output = new BufferedWriter(new FileWriter("Output.txt"))) {
  21.             for (Product product : products) {
  22.                 output.write(String.format("%.2f %s", product.getPrice(), product.getName()));
  23.                 output.newLine();
  24.             }
  25.         }
  26.     }
  27. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement