Advertisement
Guest User

Untitled

a guest
Jan 29th, 2015
158
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 0.98 KB | None | 0 0
  1. import java.io.BufferedReader;
  2. import java.io.BufferedWriter;
  3. import java.io.FileReader;
  4. import java.io.FileWriter;
  5. import java.util.ArrayList;
  6. import java.util.Collections;
  7. import java.util.List;
  8.  
  9. public class P09_ListOfProducts {
  10.  
  11.     public static void main(String[] args) {
  12.         ArrayList<Product> products = new ArrayList<Product>();
  13.         try {
  14.             BufferedReader reader = new BufferedReader(new FileReader("P09_InputProducts.txt"));
  15.             BufferedWriter writer = new BufferedWriter(new FileWriter("P09_OutputProducts.txt"));
  16.             String input;
  17.            
  18.             while ((input = reader.readLine()) != null) {
  19.                 String[] splited = input.split(" ");
  20.                 products.add(new Product(splited[0], Double.parseDouble(splited[1])));
  21.             }
  22.            
  23.             Collections.sort(products);
  24.            
  25.             for (Product product : products) {
  26.                 writer.write(product.getPrice() + " " + product.getName() + "\n");
  27.             }
  28.            
  29.             reader.close();
  30.             writer.close();
  31.         } catch (Exception e) {
  32.             System.err.println("Error");
  33.         }
  34.     }
  35.  
  36. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement