Book Search Engine

Application Overview

In this part of the lab, we will use our MyBinarySearchTreeNode<T> implementation of the binary search tree data structure to support a search engine for looking up books in the GoodReads data provided in the GoodReadsData.txt file in your GitHub repository.

A GUI for our search engine has been provided for you in the BookSearchFrame class. If you run the main method in this class, you will see something like the following image:

In the GUI, you can select different categories of book searches using the dropdown menu in the top left, and you can search for something by typing in a search String in the textbox in the top middle/right. Note: because we are going to be looking the items up in a BST, the searches will have to exactly match what is in the GoodReadsData.txt file. You can also click on the blue links to perform the specified search… at least once you implement the required methods.

Application Instructions

Open the BookFinder.java file in Visual Studio Code. This class is responsible for maintaining four BSTs that sort data about books by (1) ISBN (this.isbnToData), (2) title (this.titleToData), (3) author (this.authorToData), and (4) publisher (this.publisherToData). Much of the class is already filled in, including the instance variables, a constructor, and an addBook method that adds data to the four BSTs.

Your assignment is to fill in the five methods at the bottom of the class.

Reading Book Data from File

The first method to fill in is the createBooks method. This method should:

  1. Use a Scanner object to read in all of the lines of the File at the path passed in as a parameter to the method (refer back to Warmup 6 and Warmup 7 in Lab 1 for a reminder of how to use the Scanner class).
  2. Each line represents a different book from GoodReads. The information about each book is stored in five comma-separated fields: ISBN, authors, title, publisher, and GoodRead’s rating. You should parse this line into those five pieces of information.

Hint

For example, the line:

0446676500,Martin Luther King Jr./Clayborne Carson,The Autobiography of Martin Luther King  Jr.,Grand Central Publishing,4.35

represents the data

  • ISBN: 0446676500
  • Authors: Martin Luther King Jr. and Clayborne Carson
  • Title: The Autobiography of Martin Luther King Jr.
  • Publisher: Grand Central Publishing
  • Rating: 4.35

Note that in the author field, authors are separated by a / character.

To parse this line, the method split(String splitCharacter) from the String class could be helpful:

String[] fields = line.split(","); // splits by comma
String[] authors = field.split("/"); // splits by /
  1. For each book, create a new instance of the BookData class, passing in the parsed information about the book into the BookData constructor.
  2. Call the already implemented addBook method with the new BookData object as the only argument.

Looking Up Books from the Binary Search Trees

The final four methods searchByISBN, searchByAuthor, searchByTitle, searchByPublisher all take a single search String (entered by the user in the GUI) and should return a list of books that match that String in the corresponding BST.

For instance, the searchByISBN method should do the following:

  1. Call the find method of the this.isbnToData MyBinarySearchTreeNode to search for the node that matches the isbn parameter passed into the searchByISBN method.
  2. If the node returned in Step 1 is null, then the ISBN searched for by the user is not in the tree, and thus was not in the GoodReads data. Return an empty new ArrayList<BookData>().
  3. If the node returned in Step 1 is not null, then we have data for the user’s search! We can get the list of matching books by using the code (where node is the variable storing the node found in Step 1):
return node.getItem().getBooks();

The pseudocode for the other three methods are exactly the same, except:

  • The searchByAuthor method uses the this.authorToData BST
  • The searchByTitle method uses the this.titleToData BST
  • The searchByPublisher method uses the this.publisherToData BST
  • The variable of the String parameter to each function is slightly different

ReadMe

You might have noticed that the type of object stored in the BSTs in this application are BookLookup objects, instead of the BookData objects that you created in the createBooks method.

A BookLookup object allows us to store multiple BookData objects for the same search String. This is important because the same author might have written multiple books, the same publisher likely published multiple books, and several books might even share the same name (ISBNs are guaranteed to be unique, though).

Also, you probably noticed that we are passing a String into the find method of our BSTs instead of a BookLookup object. This is why our find method from Part 4 takes an Object parameter instead of a T parameter. If you are interested, take a look at the compareTo method of the BookLookup class. You will see that it can be compared to either another BookLookup object or a String object and returns 0 whenever the lookup instance variable matches the object passed into the function (including a user’s search String).

Running The Application

Once you’ve implemented these methods, you should able to run your GUI by using the following command in the terminal:

java BookSearchFrame

and perform some searches. I found it interesting to search for an author and see who they have published with. For example, try out an author search for Terry Pratchett.

Committing Your Progress

Don’t forget to frequently save your progress to GitHub using the add/commit/push commands with git.