Answered Essay: Activity 3: Adding and Using Methods The printBooks method prints out the title, author, and ISBN in a formatted mann

Activity 3: Adding and Using Methods The printBooks method prints out the title, author, and ISBN in a formatted manner one per line. In this activity, you will modify it to output additional information: the year of its publication and its age (the number of years since its publication date). Instructions Modify the printBooks method in the LibraryDemo class to output the publication year of each book in another column. Note that the Book class offers a getPublishDate() method already implemented for you To add the age column, you will need to add a new method to the Book class that returns the number of years between the publish date and today. You may find the following code snippet useful (it utilizes the Joda Time library already imported into the project): int years new Period(this.publishDate, DateTime . now.)).getYears.): 1. 2. The class Period is also present in java.util package. Make sure to import the one from the joda time library (org.joda.time.Period)

Below is the code of LibraryDemo class code..I have highlight the code of printBooks method part for you.

package unl.cse.library;

import java.io.File;

import java.io.FileNotFoundException;

import java.util.List;

import java.util.Scanner;

public class LibraryDemo {

private final Library lib;

public LibraryDemo() {

this.lib = new Library();

loadFile();

}

 

private void loadFile() {

Scanner s = null;

try {

s = new Scanner(new File(“data/books.txt”));

} catch (FileNotFoundException e) {

e.printStackTrace();

}

while(s.hasNext()) {

String line = s.nextLine();

String tokens[] = line.split(“,”);

String title = tokens[0];

String auth[] = tokens[1].split(” “);

Author author = new Author();

author.firstName = auth[0];

author.lastName = auth[1];

String isbn = tokens[2];

String publishDate = tokens[3];

Book b = new Book();

b.setTitle(title);

b.setAuthor(author);

b.setISBN(isbn);

b.setPublishDate(publishDate);

lib.addBook(b);

}

}

/**

* Method that searches for a book.

*/

private void searchBookInterface() {

System.out.println(“Please enter a Search Option:n (1) Search By Title (2) Search By Author (3) Keyword Search”);

Scanner scanner = new Scanner(System.in);

int userChoice = scanner.nextInt();

System.out.print(“Enter your search term: “);

String query = scanner.next();

 

switch (userChoice) {

case 1:

printBooks(this.lib.titleSearch(query));

break;

case 2:

printBooks(this.lib.authorSearch(query));

break;

case 3:

printBooks(this.lib.keywordSearch(query));

break;

default:

break;

}

return;

}

 

private void printBooks(List<Book> books) {

System.out.print(“n”);

System.out.println(String.format(“%-50s %-20s %-15s”, “TITLE”, “AUTHOR”, “ISBN”));

for (Book b : books) {

String formattedAuthor = null;

if(b.getAuthor() != null)

formattedAuthor = b.getAuthor().lastName + “, ” + b.getAuthor().lastName;

String line = String.format(“%-50s %-20s %-15s”, b.getTitle(), formattedAuthor, b.getISBN());

System.out.println(line);

}

System.out.print(“nn”);

}

/**

* Method that adds a book.

*/

private void addBookInterface() {

//change this function

Scanner scanner = new Scanner(System.in);

System.out.println(“Please enter the details of the book you want to add to the library”);

System.out.println(“Enter the title of the book: “);

String title = scanner.nextLine();

System.out.println(“Enter the first name of the author: “);

String firstName = scanner.nextLine();

System.out.println(“Enter the last name of the author: “);

String lastName = scanner.nextLine();

System.out.println(“Enter the ISBN of the book: “);

String isbn = scanner.nextLine();

System.out.println(“Enter the publication date (YYYY-MM-DD)”);

String publishDate = scanner.nextLine();

Author author = new Author();

author.firstName = firstName;

author.lastName = lastName;

Book b = new Book();

b.setTitle(title);

b.setAuthor(author);

b.setISBN(isbn);

b.setPublishDate(publishDate);

this.lib.addBook(b);

return;

}

/**

* Method that acts as the interface to the library software.

*/

public void libraryInterface() {

int userChoice = 0;

while (userChoice != 4) {

System.out.println(“Welcome to the Arcadia Library.”);

System.out.print(“Please enter a choice: (1) Add a book, (2) Find a book, (3) Print Collection (4) Exit:”);

Scanner scanner = new Scanner(System.in);

userChoice = scanner.nextInt();

switch (userChoice) {

case 1:

this.addBookInterface();

break;

case 2:

this.searchBookInterface();

break;

case 3:

printBooks(this.lib.getCollection());

break;

default:

break;

}

}//end of while

System.out.println(“Thank You for Using Arcadia Library !”);

return;

}

/**

* Main method

* @param args the command line arguments

*/

public static void main(String[] args) {

LibraryDemo demo = new LibraryDemo();

demo.libraryInterface();

}

}

Below is the book class code in case you needed it.

package unl.cse.library;

import org.joda.time.DateTime;

public class Book {

private String title;

private String isbn;

private Author author;

private DateTime publishDate;

/**

* Getter method for author

* @return

*/

public Author getAuthor() {

return null;

}

/**

* Setter method for authors

* @param author

*/

public void setAuthor(Author author) {

this.author = author;

}

/**

* Getter method for call number.

* @return

*/

public String getISBN() {

return null;

}

/**

* Setter method for call number.

* @param callNumber

*/

public void setISBN(String isbn) {

this.isbn = isbn;

}

/**

* Getter method for title

* @return

*/

public String getTitle() {

return null;

}

/**

* Setter method for title

* @param title

*/

public void setTitle(String title) {

this.title = title;

}

public String getPublishDate() {

return this.publishDate.toString(“YYYY”);

}

 

public void setPublishDate(String date) {

this.publishDate = DateTime.parse(date);

}

 

}

And this is the books.txt test case.

War and Peace,Leo Tolstoy,978-0199232765,1869

Data Structures & Problem Solving Using Java,Mark Weiss,0-321-54140-5,2011-03-10

The Naked & The Dead,Norman Mailer,978-0312265052,1948

Barbary Shore,Norman Mailer,0375700390,1951

Discrete Mathematics and Its Applications,Kenneth Rosen,9780073229720,1998-12-11

The Adventures of Tom Sawyer,Mark Twain,9780143039563,1876

Adventures of Huckleberry Finn,Mark Twain,,1885

American Gods,Neil Gaiman,0-380-97365-0,2002-04-30

The Colour of Magic,Terry Pratchett,0-86140-324-X,1983

Mort,Terry Pratchett,0-575-04171-4,1987

The Hitchhiker’s Guide to the Galaxy,Douglas Adams,978-0345391803,1979

Dirk Gently’s Holistic Detective Agency,Douglas Adams,0-671-69267-4,1987-05

Activity 3: Adding and Using Methods The printBooks method prints out the title, author, and ISBN in a formatted manner one per line. In this activity, you will modify it to output additional information: the year of its publication and its age (the number of years since its publication date). Instructions Modify the printBooks method in the LibraryDemo class to output the publication year of each book in another column. Note that the Book class offers a getPublishDate() method already implemented for you To add the “age” column, you will need to add a new method to the Book class that returns the number of years between the publish date and today. You may find the following code snippet useful (it utilizes the Joda Time library already imported into the project): int years new Period(this.publishDate, DateTime . now.)).getYears.): 1. 2. The class Period is also present in java.util package. Make sure to import the one from the joda time library (org.joda.time.Period)

Expert Answer

 

/****************************LibraryDemo.java************************************/

package unl.cse.library;

import java.io.File;

import java.io.FileNotFoundException;

import java.util.List;

import java.util.Scanner;

import org.joda.time.DateTime;

import org.joda.time.Period;

public class LibraryDemo {

private final Library lib;

public LibraryDemo() {

this.lib = new Library();

loadFile();

}

private void loadFile() {

Scanner s = null;

try {

s = new Scanner(new File(“book.txt”));

} catch (FileNotFoundException e) {

e.printStackTrace();

}

while (s.hasNext()) {

String line = s.nextLine();

String tokens[] = line.split(“,”);

String title = tokens[0];

String auth[] = tokens[1].split(” “);

Author author = new Author();

author.setFirstName(auth[0]);

author.setLastName(auth[1]);

String isbn = tokens[2];

String publishDate = tokens[3];

Book b = new Book();

b.setTitle(title);

b.setAuthor(author);

b.setISBN(isbn);

b.setPublishDate(publishDate);

lib.addBook(b,false);

}

}

/**

* Method that searches for a book.

*/

private void searchBookInterface() {

System.out

.println(“Please enter a Search Option:n (1) Search By Title (2) Search By Author (3) Keyword Search”);

Scanner scanner = new Scanner(System.in);

int userChoice = scanner.nextInt();

System.out.print(“Enter your search term: “);

scanner.nextLine();

String query = scanner.nextLine();

switch (userChoice) {

case 1:

printBooks(this.lib.titleSearch(query));

break;

case 2:

printBooks(this.lib.authorSearch(query));

break;

case 3:

printBooks(this.lib.keywordSearch(query));

break;

default:

break;

}

return;

}

private void printBooks(List<Book> books) {

System.out.print(“n”);

System.out.println(String.format(“%-50s %-20s %-15s %-20s %-15s”, “TITLE”, “AUTHOR”, “ISBN”,”Publication Year”,”Age”));

for (Book b : books) {

String formattedAuthor = null;

int years = new Period(new DateTime(b.getPublishDate()),DateTime.now()).getYears();

if (b.getAuthor() != null)

formattedAuthor = b.getAuthor().getFirstName() + “, ” + b.getAuthor().getLastName();

String line = String.format(“%-50s %-20s %-15s %-20s %-15s”, b.getTitle(), formattedAuthor, b.getISBN(),b.getPublishDate(),years);

System.out.println(line);

}

System.out.print(“nn”);

}

/**

* Method that adds a book.

*/

private void addBookInterface() {

// change this function

Scanner scanner = new Scanner(System.in);

System.out.println(“Please enter the details of the book you want to add to the library”);

System.out.println(“Enter the title of the book: “);

String title = scanner.nextLine();

System.out.println(“Enter the first name of the author: “);

String firstName = scanner.nextLine();

System.out.println(“Enter the last name of the author: “);

String lastName = scanner.nextLine();

System.out.println(“Enter the ISBN of the book: “);

String isbn = scanner.nextLine();

System.out.println(“Enter the publication date (YYYY-MM-DD)”);

String publishDate = scanner.nextLine();

Author author = new Author();

author.setFirstName(firstName);

author.setLastName(lastName);

Book b = new Book();

b.setTitle(title);

b.setAuthor(author);

b.setISBN(isbn);

b.setPublishDate(publishDate);

this.lib.addBook(b,true);

return;

}

/**

* Method that acts as the interface to the library software.

*/

public void libraryInterface() {

int userChoice = 0;

while (userChoice != 4) {

System.out.println(“Welcome to the Arcadia Library.”);

System.out.print(“Please enter a choice: (1) Add a book, (2) Find a book, (3) Print Collection (4) Exit:”);

Scanner scanner = new Scanner(System.in);

userChoice = scanner.nextInt();

switch (userChoice) {

case 1:

this.addBookInterface();

break;

case 2:

this.searchBookInterface();

break;

case 3:

printBooks(this.lib.getCollection());

break;

default:

break;

}

} // end of while

System.out.println(“Thank You for Using Arcadia Library !”);

return;

}

/**

* Main method

*

* @param args

* the command line arguments

*/

public static void main(String[] args) {

LibraryDemo demo = new LibraryDemo();

demo.libraryInterface();

}

}

/***************************************Library.java*****************************/

package unl.cse.library;

import java.io.FileWriter;

import java.util.ArrayList;

import java.util.Iterator;

import java.util.List;

/**

* The Class Library.

*/

public class Library {

private List<Book> bookList = new ArrayList<>();

/**

* Adds the book.

*

* @param b

* the b

*/

public void addBook(Book b, boolean flag) {

bookList.add(b);

if (flag) {

try (FileWriter fileWriter = new FileWriter(“book.txt”, flag)) {

String content = buildWriteContent(b);

fileWriter.write(content);

} catch (Exception e) {

e.printStackTrace();

}

}

}

private String buildWriteContent(Book b) {

return b.getTitle() + “,” + b.getAuthor().getFirstName() + ” ” + b.getAuthor().getLastName() + “,” + b.getISBN()

+ “,” + b.getPublishDate();

}

public List<Book> titleSearch(String query) {

List<Book> result = new ArrayList<>();

Iterator<Book> iterator = bookList.iterator();

while (iterator.hasNext()) {

Book book = iterator.next();

String title = book.getTitle();

if (title.equals(query.trim())) {

result.add(book);

}

}

return result;

}

public List<Book> authorSearch(String query) {

List<Book> result = new ArrayList<>();

Iterator<Book> iterator = bookList.iterator();

while (iterator.hasNext()) {

Book book = iterator.next();

String name = book.getAuthor().getFirstName() + ” ” + book.getAuthor().getLastName();

if (name.equals(query)) {

result.add(book);

}

}

return result;

}

public List<Book> keywordSearch(String query) {

List<Book> result = new ArrayList<>();

Iterator<Book> iterator = bookList.iterator();

while (iterator.hasNext()) {

Book book = iterator.next();

String name = book.getAuthor().getFirstName() + ” ” + book.getAuthor().getLastName();

if (name.contains(query) || book.getTitle().contains(query) || book.getISBN().contains(query)) {

result.add(book);

}

}

return result;

}

public List<Book> getCollection() {

return this.bookList;

}

}

/***************************************Book.java*****************************/

package unl.cse.library;

import org.joda.time.DateTime;

/**

* The Class Book.

*/

public class Book {

/** The title. */

private String title;

/** The isbn. */

private String isbn;

/** The author. */

private Author author;

/** The publish date. */

private DateTime publishDate;

/**

* Getter method for author.

*

* @return the author

*/

public Author getAuthor() {

return this.author;

}

/**

* Setter method for authors.

*

* @param author the new author

*/

public void setAuthor(Author author) {

this.author = author;

}

/**

* Getter method for call number.

*

* @return the isbn

*/

public String getISBN() {

return this.isbn;

}

/**

* Setter method for call number.

*

* @param isbn the new isbn

*/

public void setISBN(String isbn) {

this.isbn = isbn;

}

/**

* Getter method for title.

*

* @return the title

*/

public String getTitle() {

return this.title;

}

/**

* Setter method for title.

*

* @param title the new title

*/

public void setTitle(String title) {

this.title = title;

}

/**

* Gets the publish date.

*

* @return the publish date

*/

public String getPublishDate() {

return this.publishDate.toString(“YYYY”);

}

/**

* Sets the publish date.

*

* @param date the new publish date

*/

public void setPublishDate(String date) {

this.publishDate = DateTime.parse(date);

}

/**

* Gets the isbn.

*

* @return the isbn

*/

public String getIsbn() {

return isbn;

}

/**

* Sets the isbn.

*

* @param isbn the isbn to set

*/

public void setIsbn(String isbn) {

this.isbn = isbn;

}

/**

* Sets the publish date.

*

* @param publishDate the publishDate to set

*/

public void setPublishDate(DateTime publishDate) {

this.publishDate = publishDate;

}

}

/****************************************output***************************************/

Welcome to the Arcadia Library.
Please enter a choice: (1) Add a book, (2) Find a book, (3) Print Collection (4) Exit:2
Please enter a Search Option:
(1) Search By Title (2) Search By Author (3) Keyword Search
1
Enter your search term: War and Peace

TITLE AUTHOR ISBN Publication Year Age
War and Peace Leo, Tolstoy 978-0199232765 1869 148

Welcome to the Arcadia Library.
Please enter a choice: (1) Add a book, (2) Find a book, (3) Print Collection (4) Exit:3

TITLE AUTHOR ISBN Publication Year Age
War and Peace Leo, Tolstoy 978-0199232765 1869 148
Data Structures & Problem Solving Using Java Mark, Weiss 0-321-54140-5 2011 6
The Naked & The Dead Norman, Mailer 978-0312265052 1948 69
Barbary Shore Norman, Mailer 0375700390 1951 66
Discrete Mathematics and Its Applications Kenneth, Rosen 9780073229720 1998 19
The Adventures of Tom Sawyer Mark, Twain 9780143039563 1876 141
Adventures of Huckleberry Finn Mark, Twain 1885 132
American Gods Neil, Gaiman 0-380-97365-0 2002 15
The Colour of Magic Terry, Pratchett 0-86140-324-X 1983 34
Mort Terry, Pratchett 0-575-04171-4 1987 30

Welcome to the Arcadia Library.
Please enter a choice: (1) Add a book, (2) Find a book, (3) Print Collection (4) Exit:2
Please enter a Search Option:
(1) Search By Title (2) Search By Author (3) Keyword Search
2
Enter your search term: Mark Twain

TITLE AUTHOR ISBN Publication Year Age
The Adventures of Tom Sawyer Mark, Twain 9780143039563 1876 141
Adventures of Huckleberry Finn Mark, Twain 1885 132

Welcome to the Arcadia Library.
Please enter a choice: (1) Add a book, (2) Find a book, (3) Print Collection (4) Exit:2
Please enter a Search Option:
(1) Search By Title (2) Search By Author (3) Keyword Search
3
Enter your search term: Adventures

TITLE AUTHOR ISBN Publication Year Age
The Adventures of Tom Sawyer Mark, Twain 9780143039563 1876 141
Adventures of Huckleberry Finn Mark, Twain 1885 132

Welcome to the Arcadia Library.
Please enter a choice: (1) Add a book, (2) Find a book, (3) Print Collection (4) Exit:1
Please enter the details of the book you want to add to the library
Enter the title of the book:
Head First Java
Enter the first name of the author:
Bert
Enter the last name of the author:
Bates
Enter the ISBN of the book:
ISBN-9788173666025
Enter the publication date (YYYY-MM-DD)
2003
Welcome to the Arcadia Library.
Please enter a choice: (1) Add a book, (2) Find a book, (3) Print Collection (4) Exit:3

TITLE AUTHOR ISBN Publication Year Age
War and Peace Leo, Tolstoy 978-0199232765 1869 148
Data Structures & Problem Solving Using Java Mark, Weiss 0-321-54140-5 2011 6
The Naked & The Dead Norman, Mailer 978-0312265052 1948 69
Barbary Shore Norman, Mailer 0375700390 1951 66
Discrete Mathematics and Its Applications Kenneth, Rosen 9780073229720 1998 19
The Adventures of Tom Sawyer Mark, Twain 9780143039563 1876 141
Adventures of Huckleberry Finn Mark, Twain 1885 132
American Gods Neil, Gaiman 0-380-97365-0 2002 15
The Colour of Magic Terry, Pratchett 0-86140-324-X 1983 34
Mort Terry, Pratchett 0-575-04171-4 1987 30
Head First Java Bert, Bates ISBN-9788173666025 2003 14

Welcome to the Arcadia Library.
Please enter a choice: (1) Add a book, (2) Find a book, (3) Print Collection (4) Exit:4
Thank You for Using Arcadia Library !

/***************************************book.txt***********************************/

War and Peace,Leo Tolstoy,978-0199232765,1869
Data Structures & Problem Solving Using Java,Mark Weiss,0-321-54140-5,2011-03-10
The Naked & The Dead,Norman Mailer,978-0312265052,1948
Barbary Shore,Norman Mailer,0375700390,1951
Discrete Mathematics and Its Applications,Kenneth Rosen,9780073229720,1998-12-11
The Adventures of Tom Sawyer,Mark Twain,9780143039563,1876
Adventures of Huckleberry Finn,Mark Twain,,1885
American Gods,Neil Gaiman,0-380-97365-0,2002-04-30
The Colour of Magic,Terry Pratchett,0-86140-324-X,1983
Mort,Terry Pratchett,0-575-04171-4,1987
Head First Java,Bert Bates,ISBN-9788173666025,2003

Buy Essay
Calculate your paper price
Pages (550 words)
Approximate price: -

Help Me Write My Essay - Reasons:

Best Online Essay Writing Service

We strive to give our customers the best online essay writing experience. We Make sure essays are submitted on time and all the instructions are followed.

Our Writers are Experienced and Professional

Our essay writing service is founded on professional writers who are on stand by to help you any time.

Free Revision Fo all Essays

Sometimes you may require our writers to add on a point to make your essay as customised as possible, we will give you unlimited times to do this. And we will do it for free.

Timely Essay(s)

We understand the frustrations that comes with late essays and our writers are extra careful to not violate this term. Our support team is always engauging our writers to help you have your essay ahead of time.

Customised Essays &100% Confidential

Our Online writing Service has zero torelance for plagiarised papers. We have plagiarism checking tool that generate plagiarism reports just to make sure you are satisfied.

24/7 Customer Support

Our agents are ready to help you around the clock. Please feel free to reach out and enquire about anything.

Try it now!

Calculate the price of your order

Total price:
$0.00

How it works?

Follow these simple steps to get your paper done

Place your order

Fill in the order form and provide all details of your assignment.

Proceed with the payment

Choose the payment system that suits you most.

Receive the final file

Once your paper is ready, we will email it to you.

HOW OUR ONLINE ESSAY WRITING SERVICE WORKS

Let us write that nagging essay.

STEP 1

Submit Your Essay/Homework Instructions

By clicking on the "PLACE ORDER" button, tell us your requires. Be precise for an accurate customised essay. You may also upload any reading materials where applicable.

STEP 2

Pick A & Writer

Our ordering form will provide you with a list of writers and their feedbacks. At step 2, its time select a writer. Our online agents are on stand by to help you just in case.

STEP 3

Editing (OUR PART)

At this stage, our editor will go through your essay and make sure your writer did meet all the instructions.

STEP 4

Receive your Paper

After Editing, your paper will be sent to you via email.