CPSC223 Lab 6

$30.00

Category: You will Instantly receive a download link for .zip solution file upon Payment

Description

5/5 - (1 vote)

Program Instructions

1. Write a Python program that performs as a Tuffy Titan Contact List which
contains a dictionary of contacts that is stored on the hard drive and that can
have contacts added, modified, or deleted.

2. Create a contacts module to meet the following requirements:
i. Create a file named contacts.py.
a. Define a class named Contacts.
a. Define a member function named __init__ to meet the
following requirements:

a. Take a self as a positional parameter.
b. Take a filename as a keyword parameter.
c. Set a member variable equal to the filename.
d. Set a member variable equal to an empty data
dictionary.

e. Open the filename and load the JSON decoded
contents to the empty data dictionary.
f. Cleanly manage the FileNotFoundError if the filename
does not exist.

b. Define a member function named add_contact to meet the
following requirements:
a. Take a self as a positional parameter.
b. Take id as a keyword parameter.
c. Take first_name as a keyword parameter.
d. Take last_name as a keyword parameter.
e. If the id exists in the data dictionary, return the
string error.

f. Set the id:[first_name, last_name] key:value pair to the
data dictionary.
g. Sort the data dictionary in ascending order by last
name, and then by first name, ignoring case.

h. Write the contents of the data dictionary to the
filename that was set to the member variable.
i. Return the key:value pair that was added.

c. Define a member function named modify_contact to meet the
following requirements:
a. Take self as a positional parameter.
b. Take id as a keyword parameter.
c. Take first_name as a keyword parameter.
d. Take last_name as a keyword parameter.
e. If the id does not exists in the dictionary, return the
string error.

f. Set the id:[first_name, last_name’] key:value pair to the
contact dictionary.
g. Sort the data dictionary in ascending order by last
name, and then by first name, ignoring case.

h. Write the contents of the data dictionary to the
filename that was set to the member variable.
i. Return the key:value pair that was modified.

d. Define a member function named delete_contact to meet the
following requirements:
a. Take self as a positional parameter.
b. Take id as a keyword parameter.
c. If the id does not exists in the dictionary, return the
string error.

d. Remove the key:value pair with the key equal to id.
e. Write the contents of the data dictionary to the
filename that was set to the member variable.
f. Return the key:value pair with the key equal to id.

3. Create a main driver program to meet the following requirements:
i. Create a file named main.py.
ii. Import the contacts module.
iii. Instantiate a Contacts object with any default filename.

iv. Implement a menu within a loop with following choices:
a. Add contact
b. Modify contact
c. Delete contact
d. Print contact list
e. Set contact filename
f. Exit the program

v. Prompt the user for the menu choice.
vi. Prompt the user for the information needed for the
appropriate Contacts member function and call the function.
vii. Print out appropriate errors with function return values of error.

4. After thoroughly testing your program, upload main.py and contacts.py to
Canvas.
Typical input and output for the program:
*** TUFFY TITAN CONTACT MAIN MENU
1. Add contact
2. Modify contact
3. Delete contact
4. Print contact list
5. Set contact filename
6. Exit the program
Enter menu choice: 1
Enter phone number: 7145551212
Enter first name: Steve
Enter last name: Jobs
Added: Steve Jobs.

*** TUFFY TITAN CONTACT MAIN MENU
1. Add contact
2. Modify contact
3. Delete contact
4. Print contact list
5. Set contact filename
6. Exit the program
Enter menu choice: 1
Enter phone number: 5625553333
Enter first name: Bill
Enter last name: Gates
Added: Bill Gates.

*** TUFFY TITAN CONTACT MAIN MENU
1. Add contact
2. Modify contact
3. Delete contact
4. Print contact list
5. Set contact filename
6. Exit the program
Enter menu choice: 4
==================== CONTACT LIST ====================
Last Name First Name Phone
==================== ==================== ==========
Gates Bill 5625553333
Jobs Steve 7145551212

*** TUFFY TITAN CONTACT MAIN MENU
1. Add contact
2. Modify contact
3. Delete contact
4. Print contact list
5. Set contact filename
6. Exit the program
Enter menu choice: 1
Enter phone number: 7145551111
Enter first name: Alpha
Enter last name: Jobs
Added: Alpha Jobs.

*** TUFFY TITAN CONTACT MAIN MENU
1. Add contact
2. Modify contact
3. Delete contact
4. Print contact list
5. Set contact filename
6. Exit the program
Enter menu choice: 4

==================== CONTACT LIST ====================
Last Name First Name Phone
==================== ==================== ==========
Gates Bill 5625553333
Jobs Alpha 7145551111
Jobs Steve 7145551212

*** TUFFY TITAN CONTACT MAIN MENU
1. Add contact
2. Modify contact
3. Delete contact
4. Print contact list
5. Set contact filename
6. Exit the program
Enter menu choice: 1
Enter phone number: 7145551111
Enter first name: Richard
Enter last name: Stallman
Phone number already exists.

*** TUFFY TITAN CONTACT MAIN MENU
1. Add contact
2. Modify contact
3. Delete contact
4. Print contact list
5. Set contact filename
6. Exit the program
Enter menu choice: 2
Enter phone number: 7145551111
Enter first name: Richard
Enter last name: Stallman
Modified: Richard Stallman.

*** TUFFY TITAN CONTACT MAIN MENU
1. Add contact
2. Modify contact
3. Delete contact
4. Print contact list
5. Set contact filename
6. Exit the program
Enter menu choice: 4
==================== CONTACT LIST ====================
Last Name First Name Phone
==================== ==================== ==========
Gates Bill 5625553333
Jobs Steve 7145551212
Stallman Richard 7145551111

*** TUFFY TITAN CONTACT MAIN MENU
1. Add contact
2. Modify contact
3. Delete contact
4. Print contact list
5. Set contact filename
6. Exit the program
Enter menu choice: 3
Enter phone number: 5625553333
Deleted: Bill Gates.

*** TUFFY TITAN CONTACT MAIN MENU
1. Add contact
2. Modify contact
3. Delete contact
4. Print contact list
5. Set contact filename
6. Exit the program
Enter menu choice: 4
==================== CONTACT LIST ====================
Last Name First Name Phone
==================== ==================== ==========
Jobs Steve 7145551212
Stallman Richard 7145551111

*** TUFFY TITAN CONTACT MAIN MENU
1. Add contact
2. Modify contact
3. Delete contact
4. Print contact list
5. Set contact filename
6. Exit the program
Enter menu choice: 3
Enter phone number: 7145559999
Invalid phone number.

*** TUFFY TITAN CONTACT MAIN MENU
1. Add contact
2. Modify contact
3. Delete contact
4. Print contact list
5. Set contact filename
6. Exit the program
Enter menu choice: 4
==================== CONTACT LIST ====================
Last Name First Name Phone
==================== ==================== ==========
Jobs Steve 7145551212
Stallman Richard 7145551111

*** TUFFY TITAN CONTACT MAIN MENU
1. Add contact
2. Modify contact
3. Delete contact
4. Print contact list
5. Set contact filename
6. Exit the program
Enter menu choice: 6