Kotlin
Java

MP2: CSV to JSON
Java

Created By: Geoffrey Challen
/ Updated: 2021-11-10

Let's continue our work on the machine project! Today we'll begin the next project checkpoint, MP2. We'll install the test suites and begin work on our first test case.

MP2 represents a step up from MP1. Rather than making small changes, we'll be writing larger chunks of code. However! The code we ask you to write will mirror code that we've given you. So you can use our starter code as a guide.

Getting Started

For MP2 we'll be continuing on with the project that you started working on for MP0 and MP1. You'll need the environment that you set up during that checkpoint to proceed.

Before you can get to work on MP2 in earnest, we need to install the MP2 test suites. Before we continue, please commit your work.

Since you are using Java, download the MP2 tests here and move the file to the app/src/test/java/edu/illinois/cs/cs124/ay2021/mp/ directory in your machine project. For this checkpoint you'll also need to download some new data, and move preferences.csv to the app/src/main/resources/ directory in your machine project. You'll also want to reconfigure grade.yaml in the root directory of your project to request that we grade Checkpoint 2. We've completed this steps before, so if you need to review please examine the lessons for MP1.

Stubbing Out MP2

Like MP1, when we add MP2Test.java to our project our code will no longer compile. This is because the test suite expects us to have created some new methods and classes that don't exist yet. The task here is a bit more involved than it was for MP1, so let's go over the minimum we need to do to get our code to compile together:

Show how to stub out the code so that the test suites compile.

Loading Preference Data and Converting to JSON

We've provided you with new data about staff restaurant preferences in a file named preferences.csv. Here's the format of the data in the file:

Each line contains multiple Strings separated by commas. The first token is an ID that identifies the person. Subsequent IDs, of which there will be 1 or more, each identify a restaurant in our restaurant data set.

All of the IDs are Strings called universally-unique identifiers, or UUIDs for short. UUIDs are a common way of creating unique identifiers to identify a piece of data in a computer program. You can think of them as sort of like your student ID number—a unique identifier that exists only to uniquely identify some piece of data. We use them because the other properties of people and restaurants—things like their name—are not guaranteed to be unique.

Your next MP task is to load the CSV data from preferences.csv and convert it into JSON. Specifically, given this line in preferences.csv:

You should construct the following JSON string:

JSON Arrays

Before we examine the Android code you need to write today, let's take a minute to discuss how to create JSON objects using Jackson. In particular we'll look at how to create objects that contain arrays, which you'll need to do to complete today's checkpoint.

Note that you may get a deprecation warning in Android Studio when using put to add an ArrayNode to an ObjectNode. In this case you can replace the call to put with a call to replace, which will also work.

Server Load Preferences

Now let's return to the Android code and discuss how to complete loadPreferences. Note that this method is quite similar to—but not the same as—loadRestaurants. So we'll spend time walking through that method in detail, and then discuss any potential differences.

Discuss loadRestaurants and changes that need to be made to complete loadPreferences.

No Homework Today

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.

Today your goal should be to

  1. install the MP2 test suites
  2. get your code to compile, and
  3. complete the Server.loadPreferences method and convert the CSV file into JSON matching the data shape that the test suites expect.

If you get stuck, find us for help on the help site or forum.