/** * Class Link holds a Book object, as well as a count * of the number of books in the library, and two links. * One of the links points to the next author in alphabetical * order, while the second link points to the next book in * ISBN order. * @author David Thibodeau * @version 28-Aug-2002 */ public class Link { private int numberOfBooks; private Book linkBook; // used to hold Book data public Link nextAuthor, nextISBN; // used to track next links in lists /** * Constructor for objects of class Link * @param Book linkBook - reference to a Book object */ public Link(Book linkBook) { this.linkBook = linkBook; numberOfBooks = 1; } /** * Constructor for objects of class Link * @param Book linkBook - reference to a Book object * @param int numberOfBooks - number of books to add to library */ public Link(Book linkBook, int numberOfBooks) { this.linkBook = linkBook; this.numberOfBooks = numberOfBooks; } /** * addBooks method - adds the specified number of books * to the value of numberOfBooks * @param int numberOfBooks - number of books to add to library * @return void - no data returned */ public void addBooks(int numberOfBooks) { this.numberOfBooks += numberOfBooks; } // end addBooks(int numberOfBooks) method /** * getBookTitle method - this method calls the getBookTitle * method of the Book object to get the title of the book * @param void - no data required * @return String - title of the book is returned */ public String getBookTitle() { return linkBook.getBookTitle(); } // end getBookTitle method /** * getBookPublisher method - this method calls the * getBookPublisher method of the Book object to * get the publisher of the book * @param void - no data required * @return String - publisher of the book is returned */ public String getBookPublisher() { return linkBook.getBookPublisher(); } // end getBookPublisher method /** * getBookISBN method - this method calls the * getBookISBN method of the Book object to * get the ISBN of the book * @param void - no data required * @return String - ISBN of the book is returned */ public String getBookISBN() { return linkBook.getBookISBN(); } // end getBookISBN method /** * getBookSubject method - this method calls the * getBookSubject method of the Book object to * get the subject of the book * @param void - no data required * @return String - subject of the book is returned */ public String getBookSubject() { return linkBook.getBookSubject(); } // end getBookSubject method /** * getBookYear method - this method calls the * getBookYear method of the Book object to * get the subject of the book * @param void - no data required * @return int - year the book was printed is returned */ public int getBookYear() { return linkBook.getBookYear(); } // end getBookYear method /** * getBookEdition method - this method calls the * getBookEdition method of the Book object to * get the edition of the book * @param void - no data required * @return int - edition of the book is returned */ public int getBookEdition() { return linkBook.getBookEdition(); } // end getBookEdition method /** * checkAuthorLastName method - this method calls the * Book method of the same name and returns * a boolean value of true if the last name passed to * the method matches any of the book authors' last * names. Both the check String and the authors' last * names are converted to lowercase to prevent false * negatives due to differing capitalizations. * * @param String - the name to be checked * @return boolean - true if there is a match */ public boolean checkAuthorLastName(String matchName) { return linkBook.checkAuthorLastName( matchName ); } // end checkAuthorLastName(String matchName) method /** * getFirstAuthorLastName method - this method returns * the last name of the first author, which is used * for sorting purposes. * @param void - no data required * @return String - the first author's last name */ public String getFirstAuthorLastName() { return linkBook.getFirstAuthorLastName(); } // end getFirstAuthorLastName() method /** * checkAuthorNationality method - this method calls the * Book method of the same name and returns * a boolean value of true if the nationality passed to * the method matches any of the book authors' * nationalities. Both the check String and the authors' * nationalities are converted to lowercase to prevent false * negatives due to differing capitalizations. * * @param String - the nationality to be checked * @return boolean - true if there is a match */ public boolean checkAuthorNationality(String matchNationality) { return linkBook.checkAuthorNationality( matchNationality ); } // end checkAuthorNationality(String matchNationality) method /** * toString method - this method overrides the default * toString method and calls the Book object's * toString method * @param void - no data required * @return String - the books author(s) & book information */ public String toString() { String returnString = linkBook.toString(); returnString += "Number of Copies in the library = " + numberOfBooks + "\n"; return returnString; } // end toString method } // end Link Class ///////////////////////////////////////////////////////////////////// /** * Class Author holds the author's first & last names, * as well as their nationality. * * @author David Thibodeau * @version 27-Aug-2002 */ public class Author { // data variables private String firstName, lastName, nationality; /** * Constructor for objects of class Author * @param String firstName - First name of the author * @param String lastName - Last name of the author * @param String nationality - Country of author's birth */ public Author(String firstName, String lastName, String nationality) { // initialise instance variables this.firstName = firstName; this.lastName = lastName; this.nationality = nationality; } // end Author Constructor /** * getFirstName method - returns the author's first name * * @param void - no data required * @return String - author's first name */ public String getFirstName() { return firstName; } /** * getLastName method - returns the author's last name * * @param void - no data required * @return String - author's last name */ public String getLastName() { return lastName; } /** * getNationality method - returns the author's nationality * * @param void - no data required * @return String - author's nationality */ public String getNationality() { return nationality; } /** * toString method - this method overrides the inherited * toString method in order to give a more useful output * for the Author Class than the inherited method allows. * * @param void - no data required * @return String - Returns author data */ public String toString() { return "AUTHOR FIRST NAME: " + firstName + "\n" + "AUTHOR LAST NAME: " + lastName + "\n" + "NATIONALITY: " + nationality; } } // end Author Class /////////////////////////////////////////////////////////// /** * Class Book holds data about the book it refers to, * title, publisher, year, edition, ISBN, and subject * area. Book also has an author array which allows up * to 5 separate authors to be listed. * * @author David Thibodeau * @version 26-Aug-2002 */ public class Book { // instance variables - replace the example below with your own private Author[] authors = new Author[5]; private int numberOfAuthors; // holds # of authors in the authors array private Keyboard getInput; // use the keyboard class to get data // from user private String title, publisher, ISBN, subject; private int year, edition; /** * Constructor for objects of class Book */ public Book() { getBookData(); } /** * getBookData method - this private method is used to get all * the information on the book, incuding author data * * @param void - no data required * @return void - no data returned */ private void getBookData() { System.out.println( "Enter number of authors who wrote this book: "); numberOfAuthors = getInput.readInt(); for(int i = 0; i < numberOfAuthors; i++) { String firstName, lastName, nationality; System.out.println("Enter author #" + (i + 1) + "'s first name: "); firstName = getInput.readString(); System.out.println("Enter author #" + (i + 1) + "'s last name: "); lastName = getInput.readString(); System.out.println("Enter author #" + (i + 1) + "'s nationality: "); nationality = getInput.readString(); authors[i] = new Author(firstName, lastName, nationality); } // end for loop System.out.println("Enter the title of this book: "); title = getInput.readString(); System.out.println("Enter the publisher of this book: "); publisher = getInput.readString(); System.out.println("Enter the ISBN of this book: "); ISBN = getInput.readString(); System.out.println("Enter the subject area of this book: "); subject = getInput.readString(); System.out.println("Enter the year this book was printed: "); year = getInput.readInt(); System.out.println("Enter the this book's edition: "); edition = getInput.readInt(); } // end getBookData method /** * getBookTitle method - this method returns the * book's title in a String object * * @param void - no data required * @return String - the book's title */ public String getBookTitle() { return title; } /** * getBookPublisher method - this method returns the * book's publisher in a String object * * @param void - no data required * @return String - the book's publisher */ public String getBookPublisher() { return publisher; } /** * getBookISBN method - this method returns the * book's ISBN in a String object * * @param void - no data required * @return String - the book's ISBN */ public String getBookISBN() { return ISBN; } /** * getBookSubject method - this method returns the * book's subject in a String object * * @param void - no data required * @return String - the book's subject */ public String getBookSubject() { return subject; } /** * getBookYear method - this method returns the * book's year in an integer object * * @param void - no data required * @return int - the book's year */ public int getBookYear() { return year; } /** * getBookEdition method - this method returns the * book's edition in an integer object * * @param void - no data required * @return int - the book's edition */ public int getBookEdition() { return edition; } /** * checkAuthorLastName method - this method returns * a boolean value of true if the last name passed to * the method matches any of the book authors' last * names. Both the check String and the authors' last * names are converted to lowercase to prevent false * negatives due to differing capitalizations. * * @param String - the name to be checked * @return boolean - true if there is a match */ public boolean checkAuthorLastName(String matchName) { boolean wasFound = false; String temp; for(int i = 0; i < numberOfAuthors; i++) { matchName = matchName.toLowerCase(); temp = authors[i].getLastName(); temp = temp.toLowerCase(); if( temp.equals(matchName) ) { wasFound = true; i = numberOfAuthors; } else { wasFound = false; } } // end for return wasFound; } // end checkAuthorLastName(String matchName) method /** * getFirstAuthorLastName method - returns the first * author's last name for sorting purposes. * * @param void - no data required * @return String - the first author's last name */ public String getFirstAuthorLastName() { return authors[0].getLastName(); // gets the last name } // end getFirstAuthorLastName method /** * checkAuthorNationality method - this method returns * a boolean value of true if the nationality passed to * the method matches any of the book authors' * nationalities. Both the check String and the authors' * nationalities are converted to lowercase to prevent false * negatives due to differing capitalizations. * * @param String - the nationality to be checked * @return boolean - true if there is a match */ public boolean checkAuthorNationality(String matchNationality) { boolean wasFound = false; String temp; for(int i = 0; i < numberOfAuthors; i++) { matchNationality = matchNationality.toLowerCase(); temp = authors[i].getNationality(); temp = temp.toLowerCase(); if( temp.equals(matchNationality) ) { wasFound = true; i = numberOfAuthors; } else { wasFound = false; } } // end for return wasFound; } // end checkAuthorNationality(String matchNationality) method /** * toString method - this method overrides the default * toString method and makes it more useful for this * object. * @param void - no data required * @return String - the books author(s) & book information */ public String toString() { String returnString = ""; // used to hold data until complete // String is returned to the caller for(int i = 0; i < numberOfAuthors; i++) { returnString += authors[i].toString(); returnString += "\n"; // create blank line after every // author data } // end for loop returnString += "Title: " + title + "\n"; returnString += "Publisher: " + publisher + "\n"; returnString += "ISBN: " + ISBN + "\n"; returnString += "Subject: " + subject + "\n"; returnString += "Year Published: " + year + "\n"; returnString += "Edition: " + edition + "\n"; return returnString; } // end toString method } // end Book Class ///////////////////////////////////////////////////////////// /** * Class SortedList is used to manage a linked list of Link * objects. This class insertion, sorting and traversal * methods. * This class comes from the book "Data Structures & Algorithms * in Java" by Robert LaFore * @author Robert LaFore, modified by David Thibodeau * @version 28-Aug-2002 */ public class SortedList { // Links used to mark start of lists private Link firstAuthor, firstISBN; /** * Constructor for objects of class LinkList */ public SortedList() { firstAuthor = null; // lists are empty to start firstISBN = null; } /** * insertLink method - This method inserts links * * @param Book newBook - Book data to be added to the library * @param int numberOfBooks - the number of books added to the * library * @return void - no data returned */ public void insertLink(Book newBook, int numberOfBooks) { // create new link to insert (if necessary) // the next set of code is for the Author linked list Link newLink = new Link(newBook, numberOfBooks); Link previous = null; Link current = firstAuthor; if(isEmpty()) { firstAuthor = newLink; firstISBN = newLink; return; } // creates first links if this is the first insert while( current != null && (( newLink.getFirstAuthorLastName().compareToIgnoreCase(current.getFirstAuthorLastName() ) > 0) || ( newLink.getFirstAuthorLastName().compareToIgnoreCase(current.getFirstAuthorLastName() ) == 0))) { if( (newLink.getFirstAuthorLastName().compareToIgnoreCase( current.getFirstAuthorLastName() ) == 0) && ( newLink.getBookTitle().compareToIgnoreCase( current.getBookTitle() ) == 0 )) { current.addBooks( numberOfBooks ); return; } // if the titles match, books are the same; only need to add more books previous = current; current = current.nextAuthor; } if(previous == null) { firstAuthor = newLink; } else { previous.nextAuthor = newLink; } newLink.nextAuthor = current; // create new link to insert (if necessary) // the next set of code is for the ISBN linked list previous = null; current = firstISBN; while(current != null && newLink.getBookISBN().compareToIgnoreCase( current.getBookISBN() ) > 0) { previous = current; current = current.nextISBN; } if(previous == null) { firstISBN = newLink; } else { previous.nextISBN = newLink; } newLink.nextISBN = current; } // end insertLink /** * printSortedList method - this method traverses the book links based * on the sort order method selected by the user. The user can choose * between sorting by the first author of each book's last names, or * ISBN codes. * * @param int sortType - 1 if sorting by author, 2 if sorting by ISBN * @return void - no data returned */ public void printSortedList(int sortType) { Link current; // sort by author if(sortType == 1) { current = firstAuthor; while(current != null) { System.out.println( current.toString() ); current = current.nextAuthor; // advance to next in list } // end while } // end if else if(sortType == 2) { current = firstISBN; while(current != null) { System.out.println( current.toString() ); current = current.nextISBN; // advance to next in list } // end while } // end else if } // end printSortedList(int sortType) method /** * searchByAuthor method - this method searches for an author * passed to the method and prints out any links that have an * author with the same last name * @param String lastName - last name to use in search * @return void - all results are printed directly to the console */ public void searchByAuthor(String lastName) { Link current = firstAuthor; while(current != null) { if( current.checkAuthorLastName( lastName ) ) { System.out.println( current.toString() ); } current = current.nextAuthor; } // end while } // end searchByAuthor(String lastName) /** * searchByNationality method - this method searches for the * nationality passed to the method and prints out any links * that have an author with the same nationality * @param String nationality - nationality to use in search * @return void - all results are printed directly to the console */ public void searchByNationality(String nationality) { Link current = firstAuthor; while(current != null) { if( current.checkAuthorNationality( nationality ) ) { System.out.println( current.toString() ); } current = current.nextAuthor; } // end while } // end searchByNationality(String nationality) /** * searchByPublisher method - this method searches for the * publisher passed to the method and prints out any links * that have the same publisher * @param String publisher - publisher to use in search * @return void - all results are printed directly to the console */ public void searchByPublisher(String publisher) { Link current = firstAuthor; while(current != null) { if( current.getBookPublisher().compareToIgnoreCase( publisher ) == 0 ) { System.out.println( current.toString() ); } current = current.nextAuthor; } // end while } // end searchByPublisher(String publisher) method /** * searchBySubject method - this method searches for the * subject passed to the method and prints out any links * that have the same subject * @param String subject - subject to use in search * @return void - all results are printed directly to the console */ public void searchBySubject(String subject) { Link current = firstAuthor; while(current != null) { if( current.getBookSubject().compareToIgnoreCase( subject ) == 0 ) { System.out.println( current.toString() ); } current = current.nextAuthor; } // end while } // end searchBySubject(String subject) /** * searchByAuthorAndSubject method - this method searches for books * with selected subject and authors passed to the method and * prints out any links that have the same subject * @param String lastName - last name to use in search * @param String subject - subject to use in search * @return void - all results are printed directly to the console */ public void searchByAuthorAndSubject(String lastName, String subject) { Link current = firstAuthor; while(current != null) { if( ( current.getBookSubject().compareToIgnoreCase( subject ) == 0 ) && current.checkAuthorLastName( lastName ) ) { System.out.println( current.toString() ); } current = current.nextAuthor; } // end while } // end searchByAuthorAndSubject(String lastName, String subject) /** * addBook method - this method creates a new Book object and * inserts it into the linked list. * @param int numberOfBooks - number of books to add to the list * @return void - no data returned */ public void addBook(int numberOfBooks) { Book newBook = new Book(); this.insertLink( newBook, numberOfBooks ); } // end addBook(int numberOfBooks) method /** * isEmpty method - returns true if the list is empty * * @param void - no data required * @return boolean - true if the list is empty */ public boolean isEmpty() { return (firstAuthor == null); // if either link != null // the list isn't empty } // end isEmpty method } // end Class SortedList ////////////////////////////////////////////////////////////////// /** * Class LibraryApp uses the SortedList class to manage a * linked list of books and how many of each book * the library owns. This class also interacts with the user * through the console using the keyboard class. * * @author David Thibodeau * @version 28-Aug-2002 */ public class LibraryApp { /** * Constructor for objects of class Library */ public LibraryApp() { } /** * main method - this method instantiates a keyboard object * and a SortedList object and interacts with the user to * add books and display book lists, sorted by author and ISBN * * @param String[] args - these are ignored if passed * @return void - no data returned */ public static void main(String[] args) { Keyboard getInput = new Keyboard(); String keyInput = ""; SortedList libraryList = new SortedList(); while( keyInput.compareToIgnoreCase( "quit" ) != 0 ) { System.out.println( "Welcome to the library program.\n" + "Please choose from the following options:" ); System.out.println( "Type add-book to add a book to the library" ); System.out.println( "Type list-author to list books by author." ); System.out.println( "Type list-ISBN to list books by ISBN." ); System.out.println( "Type search-author to list books by a specific author" ); System.out.println( "Type search-nationality to list books by authors " + "of a specific nationality." ); System.out.println( "Type search-publisher to list books by a specific " + "publisher." ); System.out.println( "Type search-subject to list books on a specific " + "subject." ); System.out.println( "Type search-subject-author to list books by " + "a specific author on a specific subject." ); System.out.println( "Type quit to end the program." ); keyInput = getInput.readString(); if( keyInput.compareToIgnoreCase( "add-book" ) == 0) { System.out.println( "Type in the quantity of this book to add " + "to the library." ); int numberOfBooks = getInput.readInt(); libraryList.addBook( numberOfBooks ); } // end add-book else if( keyInput.compareToIgnoreCase( "list-author" ) == 0 ) { libraryList.printSortedList( 1 ); } else if( keyInput.compareToIgnoreCase( "list-ISBN" ) == 0 ) { libraryList.printSortedList( 2 ); } else if( keyInput.compareToIgnoreCase( "search-author" ) == 0 ) { System.out.println( "Type in the last name to search on:" ); String lastName = getInput.readString(); libraryList.searchByAuthor( lastName ); } else if( keyInput.compareToIgnoreCase( "search-nationality" ) == 0 ) { System.out.println( "Type in the nationality to search on:" ); String nationality = getInput.readString(); libraryList.searchByNationality( nationality ); } else if( keyInput.compareToIgnoreCase( "search-publisher" ) == 0 ) { System.out.println( "Type in the publisher to search on:" ); String publisher = getInput.readString(); libraryList.searchByPublisher( publisher ); } else if( keyInput.compareToIgnoreCase( "search-subject" ) == 0 ) { System.out.println( "Type in the subject to search on:" ); String subject = getInput.readString(); libraryList.searchBySubject( subject ); } else if( keyInput.compareToIgnoreCase( "search-subject-author" ) == 0 ) { System.out.println( "Type in the subject to search on:" ); String subject = getInput.readString(); System.out.println( "Type in the author's last name to search on:" ); String lastName = getInput.readString(); libraryList.searchByAuthorAndSubject( lastName, subject ); } else if( keyInput.compareToIgnoreCase( "quit" ) == 0 ) { System.out.println( "Program ending." ); } else { System.out.println( "You have entered an invalid command, " + "please try again." ); } } // end while loop } } // end LibraryApp Class