Kotlin Foundations

Lesson 7 of 7

Final Project: Contact Book Console App

Combine everything from this course, null safety, data classes, when, and extension functions, into a small, real console application: a contact book.

Requirements

  1. Create a data class Contact(val name: String, val phone: String, var favorite: Boolean = false).
  2. Create a ContactBook class holding a MutableList<Contact>, with methods addContact(c: Contact), findContact(name: String): Contact? (returns null if not found, don't throw), removeContact(name: String), and listFavorites(): List<Contact>.
  3. In main, add at least 3 contacts, mark one as a favorite, then call findContact with a name that doesn't exist and handle the null result gracefully (using ?. and/or ?:) instead of crashing.
  4. Print all favorite contacts.
  5. Write at least one extension function, e.g. fun Contact.displayName() = "$name ($phone)", and use it when printing contacts.

Stretch goals

  • Use a when expression to group contacts by the first letter of their name.
  • Make lookups in findContact case-insensitive.
  • Sort the favorites list alphabetically by name before printing it.

Submit a link to your finished project below, an instructor will review it before you can mark this lesson complete. Good luck!

This lesson ends in a project. Build it on your own machine, there's nowhere to submit it here, but the brief above is everything you need.