Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Here is the CSV info The Goldfinch Donna Tartt 2014 The Orphan Master\'s Son Ada

ID: 638844 • Letter: H

Question

Here is the CSV info

The Goldfinch Donna Tartt 2014 The Orphan Master's Son Adam Johnson 2013 No Pulitzer prize for fiction was awarded in 2012 A Visit from the Goon Squad Jennifer Egan 2011 Tinkers Paul Harding 2010 Olive Kitteridge Elizabeth Strout 2009 The Brief Wondrous Life of Oscar Wao Junot Diaz 2008 The Road Cormac McCarthy 2007 March Geraldine Brooks 2006 Gilead Marilynne Robinson 2005 The Known World Edward P. Jones 2004 Middlesex Jeffrey Eugenides 2003 Empire Falls Richard Russo 2002 The Amazing Adventures of Kavalier & Clay Michael Chabon 2001 Interpreter of Maladies Jhumpa Lahiri 2000 The Hours Michael Cunningham 1999

Explanation / Answer

package com.book;

import java.io.BufferedReader;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.Scanner;


class Book implements Comparable<Book>{
    private String title;
    private String author;
    private int year;
  
   public String getTitle() {
       return title;
   }
   public String getAuthor() {
       return author;
   }
   public int getYear() {
       return year;
   }
   public Book(String title,String author,int year) {
           this.title=title;
           this.author=author;
           this.year=year;  
   }
  
   //return the list of books read from the csv
   public static List<Book> getList(String csvFileName) {
      
       return readCsv(csvFileName);
       
    
   }
  
   //to read csv file
   private static List<Book> readCsv(String csvFileName) {
           List<Book>bookList=null;
            if(csvFileName!=null && csvFileName.length()>0){
              BufferedReader br = null;
              String line = "";
              String cvsSplitBy = ",";
             
              try {
                 br = new BufferedReader(new FileReader("/"+csvFileName));
                   bookList=new ArrayList<Book>();
                 
                  while ((line = br.readLine()) != null) {
                   
                           // use comma as separator
                      String[] bookInfo = line.split(cvsSplitBy);
                      if(!isWrongPattern(bookInfo)){
                          bookList.add(new Book(bookInfo[0], bookInfo[1], Integer.parseInt(bookInfo[2])));
                      }else{
                          StringBuilder errBldr=new StringBuilder();
                          errBldr.append("Problem reading in ");
                          for(int i=0;i<bookInfo.length;i++){
                              errBldr.append(""+bookInfo[i]+"");
                              if(i<bookInfo.length-1){
                                  errBldr.append(",");
                              }
                          }
                         
                          System.err.println(errBldr);  
                         
                      }
                     
         
                  }
             
         
         
              } catch (FileNotFoundException e) {
                  System.out.println(e.getMessage());
              } catch (IOException e) {
                  e.printStackTrace();
              } finally {
                  if (br != null) {
                      try {
                          br.close();
                      } catch (IOException e) {
                          e.printStackTrace();
                      }
                  }
              }
         
             
             }else{
              System.out.println("Please enter valid csv file name");
             }
          
            return bookList;
          
          }
        
   //to determine whether the pattern in csv is wrong or not
          private static boolean isWrongPattern(String[] bookInfo){
              boolean isInValid=false;
              if(bookInfo.length!=3){
                  isInValid=true;
              
              }else if(!isInteger(bookInfo[2])){
                  isInValid=true;
              }
              return isInValid;
          }
        
          //to check whether the year is in integer format or not
          private static boolean isInteger(String s) {
                try {
                    Integer.parseInt(s);
                } catch(NumberFormatException e) {
                    return false;
                }
                // only got here if we didn't return false
                return true;
           }
  
        
   @Override
   public String toString() {
      
       return this.title+" by "+this.author+" ("+this.year+")";
   }
  
    //to sort on the title of the book
   @Override
   public int compareTo(Book book) {
       return this.getTitle().compareTo(book.getTitle());
   }
  
  
  
}


public class BookApp {
  
  
      public static void main(String[]args){
          Scanner sc=new Scanner(System.in);
          System.out.println("Please enter the name of your csv file");
          List<Book>bookList=Book.getList(sc.next());
      
          System.out.println("Number of books read in : "+bookList.size()+" ");
          System.out.println("Sorted book list :");
          Collections.sort(bookList);
      
          for(Book book:bookList){
              System.out.println(book);
          }
          System.out.println(" "+"Reverse order :");
          Collections.reverse(bookList);
      
          for(Book book:bookList){
              System.out.println(book);
          }
      
       sc.close();
      }
    
   
}