Advertisement
coasterka

TextFileWithComments

May 17th, 2014
248
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 4.29 KB | None | 0 0
  1. public class TextFileOperations {
  2.    
  3.     public static void main(String[] args){
  4.         //creating a ReadFile object to execute methods on it
  5.         ReadFile rf = new ReadFile();
  6.        
  7.         //method openFile() from ReadFile.java
  8.         //opens the current file "Input.txt" from the project directory
  9.         rf.openFile();
  10.        
  11.         //method redWriteFile() from ReadFile.java
  12.         //reads lines from "Input.txt"
  13.         //writes sorted lines to "Output.txt"
  14.         rf.readWriteFile();
  15.        
  16.         //method closeFile() from ReadFile.java
  17.         //closes the used resources
  18.         rf.closeFile();
  19.     }
  20. }
  21.  
  22. ___________________________________________
  23.  
  24. import java.io.File;
  25. import java.io.FileNotFoundException;
  26. import java.io.IOException;
  27. import java.text.DecimalFormat;
  28. import java.util.ArrayList;
  29. import java.util.Collections;
  30. import java.util.Formatter;
  31. import java.util.List;
  32. import java.util.Scanner;
  33.  
  34. public class ReadFile{
  35.        
  36.     private Scanner scan;
  37.    
  38.     //openFile() checks if there is a file named "Input.txt"
  39.     // and if there is - opens it  from the project directory
  40.     public void openFile(){
  41.         try{
  42.                 scan = new Scanner(new File("Input.txt"));
  43.         }
  44.         catch(FileNotFoundException ex){
  45.                 System.err.println("File not found!");
  46.         }
  47.     }
  48.        
  49.    //the most important method in the program
  50.     //reads from file
  51.     //sorts the product prices
  52.     //writes to new file
  53.     public void readWriteFile(){
  54.        
  55.         //two lists to hold each product's name and price
  56.         List<Double> priceList = new ArrayList<Double>();
  57.         List<String> productList = new ArrayList<String>();
  58.         String product = "";
  59.         Formatter formatter = null;
  60.         DecimalFormat df = new DecimalFormat("#.00");
  61.        
  62.         //a Product object which is used as a temporary object to holf info
  63.         Product currentProduct = new Product();
  64.        
  65.         //executes while the scanner reaches the end of the file
  66.         //every line is in the format
  67.         //[productName] [productPrice]
  68.         while(scan.hasNext()){
  69.             product = scan.next();
  70.             double price = Double.parseDouble(scan.next());
  71.             priceList.add(price);
  72.             productList.add(product);
  73.            
  74.             //as the program is using Lists and not Arrays, we are unnable to use
  75.             //Arrays.sort(arrayName)
  76.             Collections.sort(priceList);
  77.         }
  78.        
  79.         //this is unnecesary step, it is only used for your comfort, thus you are
  80.         //able to see what is going on with the products - in the console
  81.         for (int i = 0; i < priceList.size(); i++) {
  82.             System.out.printf("%s %s\n", df.format(priceList.get(i)), productList.get(i));
  83.         }
  84.         try{
  85.             formatter = new Formatter("Output.txt");
  86.             for (int i = 0; i < priceList.size(); i++) {
  87.                
  88.                 //setting name and price for each product from the above two lists
  89.                 currentProduct.setProductName(productList.get(i));
  90.                 currentProduct.setProductPrice(df.format(priceList.get(i)));
  91.                
  92.                 //writing the new product info in the newly created file "Output.txt"
  93.                 formatter.format("%s %s\r\n", currentProduct.getProductPrice(), currentProduct.getProductName());
  94.             }
  95.         }
  96.         catch(IOException ioex){
  97.                 System.err.println("IO Exception occured.");
  98.         }
  99.        
  100.         //closing the used resources from Formatter class
  101.         finally{
  102.                 formatter.close();
  103.         }
  104.     }
  105.        
  106.     public void closeFile(){
  107.        
  108.         //closing the used resources from Scanner class
  109.         scan.close();
  110.     }
  111. }
  112.  
  113. ________________________________________________________
  114.  
  115. public class Product{
  116.     private String productName;
  117.     private String productPrice;
  118.    
  119.     //generating product name and product price getters and setters
  120.     public String getProductName() {
  121.         return productName;
  122.     }
  123.    
  124.     public void setProductName(String productName) {
  125.         if (productName == null) {
  126.             throw new IllegalArgumentException("Illegal argument exception.");         
  127.         }
  128.         this.productName = productName;
  129.     }
  130.    
  131.     public String getProductPrice() {
  132.         return productPrice;
  133.     }
  134.    
  135.     public void setProductPrice(String string) {
  136.         this.productPrice = string;
  137.     }  
  138. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement