beyondgrader.com Logo
DemoBrowseAboutTeamLogin

Course CSV to Courses

Geoffrey Challen // 2020.10.0

Up until now we've used public constructors to create instances of our classes. But there are other patterns for enabling class creation, and this problem explores one. Specifically, we're going to show how to deserialize a CSV String into instances of a custom class.

Create a class named Course. Course should not provide a public constructor, although you will probably want to create a private one. Instead, Course should provide method named fromCSV on a companion object which takes a single String argument and returns an array of Course instances.

Each Course maintains a department and a number, both Strings. Here's how they are loaded from a CSV String. Given the following CSV contents:

 CS, 125
IE, 333
  MUS, 230

You should return an array containing three Course instances: the first with department="CS" and number="125", the second with department="IE" and number="333", etc. Your array should contain the courses in the same order in which they appear in the CSV String. You should require that the passed String is not empty.

To complete this problem you'll want to review our previous work with CSV Strings, apply what you know about companion objects, and determine how to create a private constructor. Have fun!