In: Computer Science
Add the following private attributes: String publisher
String title
String ISBN
String imageName double price
Create getter/setter methods for all data types
Create a constructor that takes in all attributes and sets them
Remove the default constructor
Override the toString() method and return a String that represents the book object that is
formatted nicely and contains all information (attributes)
In the main class:
Create a main method that throws FileNotFoundException
Import java.io libraries
Import java.util.Scanner
Create a loop to read the file
Add and ArrayList to your main program to hold your books be sure to import java.util.ArrayList
Within your loop (each iteration)
Read a line of data into variables for publisher, title, ISBN, image name and price.
Instantiate a "Book" object using the constructor you created sending in the
information read from the file.
Add the book into the array list
Once your loop is complete, use a for each loop to print each book (using the book’s toString() method) in the ArrayList to the console.
Book.java
public class Book {
   //private attributes
   private String publisher;
   private String title;
   private String ISBN;
   private String imageName;
   private double price;
   //getter methods
   //method that returns publisher
   public String getPublisher()
   {
       return publisher;
   }
   //method that returns title
   public String getTitle()
   {
       return title;
   }
   //method that returns ISBN
   public String getISBN()
   {
       return ISBN;
   }
   //method the returns imageName
   public String getImageName()
   {
       return imageName;
   }
   //method that returns price
   private double getPrice()
   {
       return price;
   }
   //setter methods
   //method that sets publisher
   public void setPublisher(String strPublisher)
   {
       this.publisher =
strPublisher;
   }
   //method that sets title
   public void setTitle(String strTitle)
   {
       this.title = strTitle;
   }
   //method that sets ISBN
   public void setISBN(String strISBN)
   {
       this.ISBN = strISBN;
   }
   //method the sets imageName
   public void setImageName(String strImageName)
   {
       this.imageName =
strImageName;
   }
   //method that sets price
   private void setPrice(double dblPrice)
   {
       this.price = dblPrice;
   }
   //constructor that takes in all attributes and sets
them
   public Book(String strPublisher, String strTitle,
String strISBN, String strImageName, double dblPrice)
   {
       setPublisher(strPublisher);
       setTitle(strTitle);
       setISBN(strISBN);
       setImageName(strImageName);
       setPrice(dblPrice);
   }
   //override toString method
   public String toString()
   {
       return "Publisher: " +
getPublisher() + "\tTitle: " + getTitle() + "\tISBN: " +
getISBN()
       + "\tImage Name: " + getImageName()
+ "\tPrice: $" + getPrice();
   }
}
Main.java
import java.io.*;
import java.util.Scanner;
import java.util.ArrayList;
public class Main {
   public static void main(String[] args) throws
NumberFormatException, IOException {
       //Create object to the scanner
class
       Scanner input= new
Scanner(System.in);
       //read the filename
       String fileName;
       System.out.print("Enter the name of
the file: ");
       fileName = input.nextLine();
       //create an arraylist to hold
books
       ArrayList<Book> listOfBooks =
new ArrayList<Book>();
       //create an object for
BufferedReader class to read the file
       BufferedReader reader = new
BufferedReader(new FileReader(fileName));
       //variable declarations
       String line;
       String publisher;
       String title;
       String ISBN;
       String imageName;
       double price;
       //run a loop to read the file until
the end of the file is reached
       while ((line = reader.readLine())
!= null) {
           //since we might
have names containing spaces i used ','(comma) as delimiter in the
text file
           String[] info =
line.split(",");
           //store book
information in their respective variables
           publisher =
info[0];
           title =
info[1];
           ISBN =
info[2];
           imageName =
info[3];
           price =
Double.parseDouble(info[4]);
           //create Book
object
           Book newBook =
new Book(publisher, title, ISBN, imageName, price);
           //add book to
the arraylist
          
listOfBooks.add(newBook);
       }
       //close the reader
       reader.close();
       //print the book array list using
for loop
       System.out.println("\nList Of Books
read: ");
       for(int i = 0; i <
listOfBooks.size(); i++)
          
System.out.println(listOfBooks.get(i).toString());
       input.close();
   }
}
Output:

Program Screenshots





Text file used:
