Final Project: Build a Contact Book with Validated Errors
Combine closures, OOP, patterns, and error handling into one small but real program: a contact book.
Requirements
- Model a
ContactBook"class" (metatable +__index, like the Object-Oriented lesson) with methods to add, remove, and list contacts. - Each contact needs a name and an email; validate the email with a string pattern (at minimum, requires an
@and at least one.after it) anderror()/assert()if it's invalid. - Wrap every call that adds a contact in
pcall, so a bad email prints a friendly error message instead of crashing the whole program. - Add a
search(term)method that usesstring.findorgmatchto return every contact whose name containsterm(case-insensitive). - Use a closure to track how many contacts have ever been added in total (including ones later removed), exposed through a
ContactBook:totalEverAdded()method, without storing it as a plain field on the instance.
Stretch goals
- Support multiple emails per contact (a table of emails instead of one string).
- Add an
export()method that returns a single formatted string listing every contact, built withtable.concat. - Persist contacts to a file with Lua's
iolibrary and reload them on startup (only relevant if you're running outside this course's browser sandbox).
Submit a link to your finished script (a repo or gist) below, an instructor will review it before you can mark this lesson complete.