Question

In: Computer Science

                      Contact Manager COMMAND MENU ---------------------------------------------------------- list - Display all contacts view - View

                      Contact Manager

COMMAND MENU

----------------------------------------------------------

list - Display all contacts

view - View a contact

add - Add a contact

del - Delete a contact

exit - Exit program

Command: list

1. Tom van Rossum

2. Edward Idle

Command: view

Number: 2

Name: Edward Idle

Email: [email protected]

Phone: +44 20 7946 0958

Command: add

Name: John Smith

Email: [email protected]

Phone: 559-123-4567

John Smith was added.

Command: list

1. Tom van Rossum

2. Edward Idle

3. John Smith

Command: exit

Bye!

Specifications

 Use the attached CSV file named contacts.csv.

 When the program starts, it should read the contacts from the CSV file.

 For the view and del commands, display an error message if the user enters an invalid

contact number.

 When you add or delete a contact, the change should be saved to the CSV file

immediately. That way, no changes are lost, even if the program crashes later.

Solutions

Expert Solution

import csv,os;

## add new entry to the contact file
def add(name, email, phone):
  with open('contacts.csv','a', newline='') as  csv_file:
     writter = csv.writer(csv_file, delimiter=',');
     writter.writerow([name,email,phone]);
     csv_file.close();
     print(name,"was added");

##list all  contacts present in the csv file
def list():
     contacts = [];
     with open('contacts.csv','r') as  csv_file:
         reader = csv.reader(csv_file);
         for line in reader:
             contacts.append(line[0]);
         csv_file.close();
     count = 1;
     for cont in contacts:
         print(count,".",cont);
         count +=1;


## delete contact from the list based on the phone number
def delete(phone):
      with open('contacts.csv','r') as input, open('contacts1.csv','a', newline='') as output:
          writer = csv.writer(output);
          contacts = [];
          for line in csv.reader(input):
              contacts.append(line);
          for cont in contacts:
              if cont[2] != phone:
               writer.writerow(cont);
      os.remove('contacts.csv');
      os.rename('contacts1.csv','contacts.csv');
      print("contact is deleted", phone);

## view contact details of the user.
def view(phone):
    data =[];
    count =0;
    with open('contacts.csv','r') as csv_file:
         reader = csv.reader(csv_file);
         for line in reader:
            count+=1;
            if line[2] == phone:
                data = line;
         csv_file.close();
    print("Number:", count);
    print("Name:", data[0]);
    print("Email:", data[1]);
    print("Phone:", data[2]);


##Contact Manager simulator. 
print("Contact Manager");
flag = 0;
while flag != 1:
    print("COMMAND MENU");
    print("list - Display all contacts");
    print("view - View a contact");
    print("add - Add a contact");
    print("del - Delete a contact");
    print("exit - Exit program");

    choice = input("Enter your choice:");
    if choice == 'list':
        list();
    else:
        if choice == 'add':
            name = input("Enter name:");
            email = input("Enter email: ");
            phone = input("Enter phone number: ");
            add(name,email,phone);
        else:
            if choice == 'view':
                phone = input("Enter phone number:");
                view(phone);
            else:
                if choice == 'del':
                    phone = input("Enter phone number:");
                    delete(phone);
                else:
                    if choice == 'exit':
                        flag = 1;

I tested and it run according to the requirements

Please please please upvote it will help me alot

Please dont downvote


Related Solutions

Console ================================================================                        Baseball Team Manager MENU OPTIONS 1 – Display
Console ================================================================                        Baseball Team Manager MENU OPTIONS 1 – Display lineup 2 – Add player 3 – Remove player 4 – Move player 5 – Edit player position 6 – Edit player stats 7 - Exit program POSITIONS C, 1B, 2B, 3B, SS, LF, CF, RF, P ================================================================ Menu option: 1 Player POS AB H AVG ---------------------------------------------------------------- 1 Denard OF 545 174 0.319 2 Joe 2B 475 138 0.291 3 Buster C 535 176 0.329 4 Hunter OF 485...
Windows XP Command Line questions. a) What is the command to display all of the files...
Windows XP Command Line questions. a) What is the command to display all of the files on the disk in drive A:--do not use the DIR command or the CHKDSK command C:\> b) Display the contents of the PERSONAL.FIL file located in the root directory of drive A: one screenful at a time C) Display the contents of the PERSONAL.FIL file located in the root directory of drive A:, one screenful at a time, beginning with the 25th record, on...
1. Write a bash command that will display the value of all environment variables defined in...
1. Write a bash command that will display the value of all environment variables defined in your shell process, sorted in alphabetical order, and numbered. Here is a sample of the type of output your command should produce: 1 } 2 ALSA_CONFIG_PATH=/etc/alsa-pulse.conf 3 AUDIODRIVER=pulseaudio 4 BASH_FUNC_mc%%=() { . /usr/share/mc/mc-wrapper.sh 5 COLORTERM=1 2. Write a bash command that will display the value (and only the value) of the JAVA_ROOT environment variable. Here is a sample of the type of output your...
Java code for a binary tree that does the following:  Display a menu to the...
Java code for a binary tree that does the following:  Display a menu to the user and request for the desired option.  Based on the user’s input, request for additional information as follows: o If the user wants to add a node, request for the name (or ID) of the new node to be added as well as the name of the desired parent of that node.  If the parent node already has two children, the new...
Java code for a binary tree that does the following:  Display a menu to the...
Java code for a binary tree that does the following:  Display a menu to the user and request for the desired option.  Based on the user’s input, request for additional information as follows: o If the user wants to add a node, request for the name (or ID) of the new node to be added as well as the name of the desired parent of that node.  If the parent node already has two children, the new...
The program is to be called CarClub The application will display menu as below 1. load...
The program is to be called CarClub The application will display menu as below 1. load cars 2. display all cars 3. search car 4 count cars older the 30 years 5 exit Please enter your option: Load car menu should read input data from text file name (car.txt) Car class have 4 data members which are. Car make, car model, car year and car price In addition If both car year and price were not provided, year has a...
Use the find command to find all directories under /pub/cs/ whose name is cs160a. Be sure to only display the results that match, do not display any error output.
Unix/Linux1)Use the find command to find all directories under /pub/cs/ whose name is cs160a. Be sure to only display the results that match, do not display any error output.Hint: use: 2>/dev/null.2)Create a one-line command, using he famous.dat file, to add the word " STREET" to the address, for all who live on 2nd or 3rd.For example: '2nd' becomes "2nd STREET" and '3rd' becomes "3rd STREET".Display only the first 9 lines.Hint: use 2 sed statements with pipes.3)Display all lines in famous.dat...
What is the command to view the process? What is a zombie process & What is...
What is the command to view the process? What is a zombie process & What is the command to take care of zombie process?
Write an interactive Java class Project8Q3, that will display a menu with the available commands 'G',...
Write an interactive Java class Project8Q3, that will display a menu with the available commands 'G', 'D', and 'X'. If 'G' is selected, prompt the user for the ID of a president to display to the screen and then display the president's information. If 'D' is selected, display all the values in the LinkedHashMap to the user with their associated LinkedHashMap key. If 'X' is selected, exit the program. The class should have the method addPresident(id, lastName, firstName, middleInitial) which...
Write a program to prompt the user to display the following menu: Sort             Matrix                   Q
Write a program to prompt the user to display the following menu: Sort             Matrix                   Quit If the user selects ‘S’ or ‘s’, then prompt the user to ask how many numbers you wish to read. Then based on that fill out the elements of one dimensional array with integer numbers. Then sort the numbers and print the original and sorted numbers in ascending order side by side. How many numbers: 6 Original numbers:                     Sorted numbers 34                                                                         2          55                                                      ...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT