svetlozar_kirkov

List of Products (Homework)

Jan 24th, 2015
278
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 2.52 KB | None | 0 0
  1. import java.io.File;
  2. import java.io.FileNotFoundException;
  3. import java.io.PrintWriter;
  4. import java.io.UnsupportedEncodingException;
  5. import java.math.BigDecimal;
  6. import java.util.ArrayList;
  7. import java.util.Collections;
  8. import java.util.Comparator;
  9. import java.util.Scanner;
  10.  
  11.  
  12. public class Problem9_ListOfProducts {
  13.    
  14.     public static class PriceSorter implements Comparator<Product>{ // class for sorting "product" objects by price
  15.  
  16.         public int compare(Product one, Product another){  // the method
  17.             int returnVal = 0;
  18.  
  19.             if(one.getPrice().compareTo(another.getPrice())<0){
  20.                 returnVal =  -1;
  21.             }else if(one.getPrice().compareTo(another.getPrice())>0){
  22.                 returnVal =  1;
  23.             }else if(one.getPrice() == another.getPrice()){
  24.                 returnVal =  0;
  25.             }
  26.             return returnVal;
  27.             }
  28.         }
  29.  
  30.     public static class Product { //creating object "Product"
  31.         private String name;  // creating property "name"
  32.         private BigDecimal price; //creating property "price"
  33.        
  34.         public Product(String name, BigDecimal price){ //constructor with parameters
  35.             this.name=name;
  36.             this.price=price;
  37.         }
  38.         public String getName(){ //method to return the name of the product
  39.             return name;
  40.         }
  41.         public BigDecimal getPrice(){ //method to return the price of the product
  42.             return price;
  43.         }
  44.     }
  45.  
  46.     public static void main(String[] args) throws UnsupportedEncodingException {
  47.         try {
  48.             Scanner scanner = new Scanner(new File("Input.txt")); //opening the input file
  49.             ArrayList<Product> products = new ArrayList<Product>(); //list to keep the products from input file
  50.            
  51.             while (scanner.hasNextLine()){ //checking if the file has more lines
  52.                 Product temp = new Product(scanner.next(),scanner.nextBigDecimal()); //creating temporary product
  53.                 products.add(temp); //adding the temporary product to the list
  54.             }
  55.            
  56.             PrintWriter writer = new PrintWriter("Output.txt", "UTF-8"); //writing the output file
  57.             Collections.sort(products,new PriceSorter()); //using PriceSorter class to sort the list of products by price
  58.            
  59.             for (Product prod:products){   // iterate through each product from the list (now sorted)
  60.                 writer.printf("%s %s",prod.price,prod.name); //print the product values
  61.                 writer.println(); //add a new line
  62.             }
  63.             writer.close(); //closing the file
  64.            
  65.         } catch (FileNotFoundException e) { //catching the exception if the file does not exist
  66.             System.out.println("Error: file not found!"); //printing error message
  67.             System.exit(1); //program exit
  68.         }
  69.     }
  70. }
Advertisement
Add Comment
Please, Sign In to add comment