Codecademy Practice

Contact Book - Array database

Challenge: We are going to build a contact book as a CLI app. The contacts will be stored in an array and will be lost when the application is closed.

Step by step instructions

UI

Create contacts:

Update the UI so that it can ask questions about contacts.

Contact field: name
Prompt: Contact name:

Contact field: address
Prompt: Contact address:

Contact field: phone
Prompt: Contact phone:

Contact field: email
Prompt: Contact email:

Contact field: notes
Prompt: Contact notes:

{
  name: 'Matt Damon',
  address: 'Some address',
  phone: '1234567',
  email: 'matt@damon.com',
  notes: 'I think he has an Oscar'
}
FIELDS_TO_PROMPTS = {
  name: NAME_PROMPT,
  address: ADDRESS_PROMPT,
  # etc.
}
{
  name: 'Matt Damon',
  address: 'Some address',
  phone: '1234567',
  email: 'matt@damon.com',
  notes: 'I think he has an Oscar'
}
Name:    Matt Damon
Address: Some address
Phone:   1234567
Email:   matt@damon.com
Notes:   I think he has an Oscar
Add another contact? (y/n): _

The _ indicates where the user prompt is.

The user can reply y or n.

If the user replies y, the method returns true, if not, it returns false.

Tests:

Suggestion: build the method one contact field at a time.

List contacts:

Extract a validator class out of the user interface, moving all the boolean valid_X? methods to it.

------------------------------
              A
------------------------------

Search contacts:

Type search term: _

(_ is where the user prompt goes). It returns the search term typed by the user.

Search again? (y/n): _

(_ is where the user prompt goes). It returns true if the user wants to search for another contact and false if not.

Update contacts:

Update the User Interface with the following four methods:

1. Choose contact

Create a public method choose_contact(contacts) which takes an array of contacts as an argument and returns a contact index. It uses the following methods:

Example:

[0]
Name:    Matt Damon
Address: Some address
Phone:   12345678901
Email:   matt@damon.com
Notes:   I think he has an Oscar

[1]
Name:    Another Contact
Address: Another address
Phone:   12345678901
Email:   another@contact.com
Notes:   Just another contact

2. Edit field

3. Update another field

4. Update another contact

Delete contacts:

Database

Create a database class that represents your persistence layer, i.e., it encapsulates the manipulation and operations on your application’s data, isolating the application from these type of changes.

It will be in charge of creating, updating, deleting and searching for contacts.

For now, it will just create contacts and save them in an array. So it is going to be called ArrayDatabase.

Create contacts:

{
  name: 'Matt Damon',
  address: 'Some address',
  phone: '1234567',
  email: 'matt@damon.com',
  notes: 'I think he has an Oscar'
}

List contacts:

Add a boolean method to the database class called any? that checks if there are any contacts in the database.

Search contacts:

Add a search method to the database that takes a search term as an argument. It searches for the term in any of the fields of a contact, for every contact in the database. For example:

Suppose we have these contacts in the database:

contacts = [
  {
    name: 'Matt Damon',
    address: 'Some address',
    phone: '1234567',
    email: 'matt@damon.com',
    notes: 'I think he has an Oscar'
  },
  {
    name: 'Oscar Wilde',
    address: 'Paris',
    phone: '00000000000',
    email: 'oscar@wilde.com',
    notes: 'I think he has an Oscar'
  },
  {
  #  another contact
  }
  ...
]

Calling:

database.search('Oscar')

Returns a list of all the findings:

[
  {
    name: 'Matt Damon',
    address: 'Some address',
    phone: '1234567',
    email: 'matt@damon.com',
    notes: 'I think he has an Oscar'
  },
  {
    name: 'Oscar Wilde',
    address: 'Paris',
    phone: '00000000000',
    email: 'oscar@wilde.com',
    notes: 'I think he has an Oscar'
  }
]

Update contacts:

Delete contacts:

Create a method delete(index) that takes an array index and deletes a contact from the contacts array.

Creator action

Add a Creator class that is in charge of orchestrating the logic to create a contact.

It takes a user interface and a database as arguments in the constructor.

The creator class is a Command Object: a class that has just one method, a command method (run) that performs the creation operation. This operation has the following steps:

Pager action

Add a Pager class that is in charge of orchestrating the logic to list all contacts.

It takes a user interface and a database as arguments in the constructor.

The Pager class is a Command Object: a class that has just one method, a command method (run) that performs the listing operation.

It works like this:

Finder action

Add a Finder class that is in charge of orchestrating the logic to search for contacts matching a search term.

It takes a user interface and a database as arguments in the constructor.

The Finder class is a Command Object: a class that has just one method, a command method (run) that performs the listing operation.

It works like this:

Updater action

Add an Updater class that is in charge of orchestrating the logic to update one or several fields in one or several contacts.

It takes a user interface and a database as arguments in the constructor.

The Updater class is a Command Object: a class that has just one method, a command method (run) that performs the updating operation.

It works like this:

Terminator action

Add a Terminator class that is in charge of orchestrating the logic to delete one or several contacts.

It takes a user interface and a database as arguments in the constructor.

The Terminator class is a Command Object: a class that has just one method, a command method (run) that performs the updating operation.

It works like this:

bin/app

Create contacts:

---------------------
    CONTACT BOOK
---------------------

1) Add contact
2) Exit the program

Choose a menu option:

List contacts:

---------------------
    CONTACT BOOK
---------------------

1) List contacts
2) Add contact
3) Exit the program

Choose a menu option:

Search contacts:

---------------------
    CONTACT BOOK
---------------------

1) List contacts
2) Add contact
3) Search contact
4) Exit the program

Choose a menu option: _

Update contacts:

---------------------
    CONTACT BOOK
---------------------

1) List contacts
2) Add contact
3) Search contact
4) Update contact
5) Exit the program

Choose a menu option: _

Delete contacts:

---------------------
    CONTACT BOOK
---------------------

1) List contacts
2) Add contact
3) Search contact
4) Update contact
5) Delete contact
6) Exit the program

Choose a menu option: _