Let's make more progress on MP1! Today we'll complete a search method that we'll later us to enable search in our restaurant view screen. We'll also get some practice with Jackson, the JSON serialization library used in our app.
Let's pick up our discussion of serialization from yesterday and examine how we can use JSON in our Java programs. This is one of the many things that you should never do yourself! There are many great libraries that support JSON in Java programs.
We're going to be using a library called Jackson to assist us with serialization. Next, let's look at how we can use Jackson to serialize and deserialize our Java objects!
Let's start with serialization. Here's how to use Jackson to automatically generate JSON from our Java objects:
Now that we have a string, let's look at how we can deserialize that
String and convert it back into a Java object.
The following homework problem is only for practice, but it may help you understand how to complete later parts of the MP.
Now let's return our attention to the project and work on the next test case.
In this case, we have a helper test case set up to handle first.
It will expose some issues with the way that we are parsing the restaurant CSV file, and with our
Restaurant class design.
Let's fix those together first.
Now that we're loading the right data, we can complete our
This is quite similar to a homework problem that we might assign!
Let's review the problem description and discuss how to structure our algorithm together.
As a more complete description of your search method, you should write a static method
search that accepts a
List<Restaurant> and a search
If the search
String exactly matches a cuisine value, you should return only all of the restaurants with that cuisine value.
Otherwise you should return all
Restaurants where either the name or cuisine contain the search
All comparisons should be case-insensitive, so both "PiZzA" and "pIzZa" match the cuisine "pizza" or the name "Pizza Palace".
You should also remove surrounding whitespace from the search
If the search
String is blank or empty, you should return a copy of the entire list of restaurants.
If either input is
null, throw an
As a reminder, on lessons where we focus on the machine project we will not assign a homework problem! However, the lesson will usually focus on helping you complete a particular part of the MP test suite, and so we encourage you to spend time on that in lieu of a homework problem.