In: Computer Science
Question 1
Download the files Book.java and BookInfo.txt from Content->Chapter 12 Quiz Files. Create a BookDriver.java class with a main method. In the main method, create an ArrayList of Book objects, read in the information from BookInfo.txt and create Book objects to populate the ArrayList. After all data from the file is read in and the Book objects added to the ArrayList- print the contents of the ArrayList. Paste your BookDriver.java text (CtrlC to copy, CtrlV to paste) into the open space before. You should not change Book.java or BookInfo.txt.
Given info below :
/**
*
* Book
*
* @author bbrown25
*
* A class to represent information about a Book
*
**/
public class Book
{
private String title;
private String author;
/**
* No arg constructor
*/
public Book()
{
title = "not set";
author = "not set";
}
/**
* @param title
* @param author
*/
public Book(String title, String author)
{
super();
setTitle( title);
setAuthor(author);
}
/**
* @return the title
*/
public String getTitle()
{
return title;
}
/**
* @param title the title to set
*/
public void setTitle(String title)
{
if (title.length() > 0)
{
this.title =
title;
}
else
{
this.title =
"not set";
}
}
/**
* @return the author
*/
public String getAuthor()
{
return author;
}
/**
* @param author the author to set
*/
public void setAuthor(String author)
{
if (author.length() > 0)
{
this.author =
author;
}
else
{
this.author =
"not set";
}
}
@Override
public String toString()
{
return "Book [title=" + title + ",
author=" + author + "]";
}
}
BookInfo.txt
War and Peace
Leo Tolstoy
Pride and Prejudice
Jane Austen
Gone With the Wind
Margaret Mitchell
Code Solution:
//imports required in the program
import java.io.FileReader;
import java.io.BufferedReader;
import java.io.File;
import java.io.IOException;
import java.util.ArrayList;
//BookDriver class
public class BookDriver {
public static void main(String[] args) {
// instantiating an ArrayList of type Book
ArrayList<Book> books = new ArrayList<>();
//try catch block is used to handle any I/O errors
try {
//Setting up the FileReader and BufferedReader to read the BookInfo.txt file
FileReader fileReader = new FileReader(new File("BookInfo.txt"));
BufferedReader bufferedreader = new BufferedReader(fileReader);
//Variables to store the current line read from file and the book name
String strLine,currentBook = "";
//a counter variable to note the index of current line being read
int i = 1;
//while loop to read the line
while ((strLine = bufferedreader.readLine()) != null) {
//analysing the BookInfo.txt, we can see that 1st line has book name
//next has the author and this way it goes on.
//So, our logic says that if the line number is odd, store the value of book title.
if(i%2!=0)
currentBook = strLine;
//else if the current line has author name, add the book to the arraylsit using constructor.
//currentBook holds the value of book title and strLine(current line being read) has the author name
else
books.add(new Book(currentBook,strLine));
//increment i everytime a line is read
i+=1;
}
//closing the input stream
fileReader.close();
}
//catch block
catch (IOException e) {
e.printStackTrace();
}
//printing the contents of arraylist
System.out.println(books);
}
}
Screenshot for reference:
Output:
We read the file using bufferedReader provided by Java. We formulate the logic using the fact that every odd number line contains Book title and even number line contains the author name. Whenever the line is odd, we store the book title in currentBook variable. When it is even, we add the book in the ArrayList setting the title as currentBook and the author as the current read line stored in strLine variable.