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.
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.
The first method to fill in is the createBooks
method. This method should:
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).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
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 /
BookData
class, passing in the parsed information about the book into the BookData
constructor.addBook
method with the new BookData
object as the only argument.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:
find
method of the this.isbnToData
MyBinarySearchTreeNode
to search for the node that matches the isbn
parameter passed into the searchByISBN
method.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>()
.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:
searchByAuthor
method uses the this.authorToData
BSTsearchByTitle
method uses the this.titleToData
BSTsearchByPublisher
method uses the this.publisherToData
BSTString
parameter to each function is slightly differentReadMe
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
).
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.
Don’t forget to frequently save your progress to GitHub using the add
/commit
/push
commands with git.