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
- Create a
data class Contact(val name: String, val phone: String, var favorite: Boolean = false). - Create a
ContactBookclass holding aMutableList<Contact>, with methodsaddContact(c: Contact),findContact(name: String): Contact?(returnsnullif not found, don't throw),removeContact(name: String), andlistFavorites(): List<Contact>. - In
main, add at least 3 contacts, mark one as a favorite, then callfindContactwith a name that doesn't exist and handle thenullresult gracefully (using?.and/or?:) instead of crashing. - Print all favorite contacts.
- Write at least one extension function, e.g.
fun Contact.displayName() = "$name ($phone)", and use it when printing contacts.
Stretch goals
- Use a
whenexpression to group contacts by the first letter of their name. - Make lookups in
findContactcase-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!