Kotlinlearncs.online LogoJava
Return to List

Homework: Course CSV to Courses

Created By: Geoffrey Challen
/ Version: 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 public final class named Course. Course should not provide a public constructor, although you will probably want to create a private one. Instead, Course should provide a class method named fromCSV 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. (Your fromCSV method should assert that the passed String is not null or empty.)

Finally, Course should provide getters for the department and number named getDepartment and getNumber, respectively.

To complete this problem you'll want to review our previous work with CSV Strings, and apply what you now know about constructing objects. Have fun!

Related Lessons

Stuck? You may find these lessons helpful: