Test Files are belowSolutionimport javaioFile import javaioP
Test Files are below
Solution
import java.io.File;
import java.io.PrintStream;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.Scanner;
class Book implements Comparable<Book>{
private final String title;
private final List<String>authorList;
// constructor to initialize the fields
public Book(String title,final ArrayList<String>theAuthors){
if(title==null || theAuthors==null || title.length()==0 || theAuthors.size()==0){
throw new IllegalArgumentException(); // throwing the exception when argument is not valid
}
this.title=title;
this.authorList=new ArrayList<>();
for (String author : theAuthors) {
authorList.add(author);
}
}
// compareTo method used for sorting
@Override
public int compareTo(Book book) {
//if titles are equal we sort on the basis of first author
if(this.title.equalsIgnoreCase(book.getTitle())){
return this.authorList.get(0).compareToIgnoreCase(book.getAuthorList().get(0));
}
return this.title.compareToIgnoreCase(book.getTitle());
}
public String getTitle() {
return title;
}
public List<String> getAuthorList() {
return authorList;
}
// method to get the book title and author detail
@Override
public String toString() {
String bookinfo= \"\\\"\" + this.title + \"\\\",\"+\" by \";
for(String author:authorList){
bookinfo+=author+\",\";
}
bookinfo=bookinfo.substring(0, bookinfo.lastIndexOf(\",\"));
return bookinfo;
}
// method to compare the equality between two book object on the basis of title and author
@Override
public boolean equals(Object obj) {
if (obj == this) return true;
if (!(obj instanceof Book)) {
return false;
}
Book book = (Book) obj;
boolean isEqual=true;
if(this.title.equalsIgnoreCase(book.getTitle())){
if(this.authorList.size()==book.getAuthorList().size()){
for(String author:this.authorList){
if(!book.authorList.contains(author)){
isEqual=false;
}
}
}else{
isEqual=false;
}
return isEqual;
}
return false;
}
}
class Library{
private List<Book>bookList;
// constructor to initialize the fields
public Library(){
bookList=new ArrayList<>();
}
// method to add the book
public boolean add(final Book theBook){
return this.bookList.add(theBook);
}
//method to find the books by title
public ArrayList<Book>findTitles(final String theTitle){
ArrayList<Book>books=new ArrayList<>();
if(bookList.size()>0){
for(Book book:bookList){
if(book.getTitle().equalsIgnoreCase(theTitle) ){
books.add(book);
}
}
}
return books;
}
// method to sort the list
public void sort(){
Collections.sort(this.bookList);
}
//method return the details of the book
@Override
public String toString() {
String books=\"\";
if(bookList.size()>0){
for(Book book:bookList){
books+=book.toString()+\"\ \ \";
}
}
return books;
}
}
public class LibraryDriver {
public static void main(String[] args) {
Scanner inputFile=null;
PrintStream outputFile=null;
try{
inputFile=new Scanner(new File(\"D:\\\\LibraryIn1.txt\"));
outputFile=new PrintStream(new File(\"D:\\\\LibraryOut.txt\"));
}catch(Exception e){
System.out.println(\"Difficulties opening the file! \"+e);
System.exit(1);
}
ArrayList<String>authors=new ArrayList<>();
ArrayList<Book>books=new ArrayList<>();
while(inputFile.hasNext()){
String title=inputFile.nextLine();
Book book = new Book(title, getAuthors(inputFile.nextLine()));
books.add(book);
}
Library library=new Library();
for(Book book:books){
library.add(book);
}
outputFile.println(library);
library.sort();
outputFile.println(\"Prints All Sorted Book List\");
outputFile.println(library);
List<Book>booksFound=library.findTitles(\"The Hobbit\");
outputFile.println(\"By title \\\"The Hobbit\\\"\");
for(Book book:booksFound){
outputFile.println(book);
}
}
//method to get the name of the authors in a list
public static ArrayList<String> getAuthors(String theAuthors){
ArrayList<String>authors=new ArrayList<>();
String [] authorArray=theAuthors.split(\",\");// splitting the name on the basis of delimiter ,
for(String name:authorArray){
if(name.indexOf(\"*\")!=-1){
String []fullname=name.split(\"\\\\*\");//splitting the first name and last name of the author on delimiter *
authors.add(fullname[0]+\" \"+fullname[1]);
}else{
authors.add(name);
}
}
return authors;
}
}
----------------------------input---------------------------
The Hobbit
Tolkien, J.R.R
The Bluff
A christmas Carol
Dickens, charles
Dougan, Thomas*peters, Paul*Elliot, Sam
The Hobbit
Jay
------------------------------output(LibraryOut.txt)--------------------------------
\"The Hobbit\", by Tolkien, J.R.R
\"The Bluff\", by A christmas Carol
\"Dickens, charles\", by Dougan, Thomas peters, Paul Elliot, Sam
\"The Hobbit\", by Jay
Sorted books
\"Dickens, charles\", by Dougan, Thomas peters, Paul Elliot, Sam
\"The Bluff\", by A christmas Carol
\"The Hobbit\", by Jay
\"The Hobbit\", by Tolkien, J.R.R
By title \"The Hobbit\"
\"The Hobbit\", by Jay
\"The Hobbit\", by Tolkien, J.R.R



