Question

In this assignment, you will create an application that holds a list of contact information. You ...

In this assignment, you will create an application that holds a list of contact information. You will be able to prompt the user for a new contact, which is added to the list. You can print the contact list at any time. You will also be able to save the contact list.

MUST BE IN PYTHON FORMAT

Create the folllowing objects:

ContactsItem - attributes hold the values for the contact, including

  • FirstName - 20 characters
  • LastName - 20 characters
  • Address - 20 characters
  • State - 2 characters
  • Zip - 5 characters
  • Phone - 15 characters
  • E-Mail - 20 characters

Contacts - an object with the following methods:

constructor (__init__) - parameter is the name of the file containing the contacts (Contacts.txt)

  • Creates an empty list called ContactsList
  • Opens Contexts.txt
  • Read a line and splits it into components (Note that the "|" is the separator)
  • Constructs a ContactsItem and appends it to the ContactList

addcontacts - parameters are: FirstName, LastName, Address, City, State, Zip, Phone, Email

  • Constucts a ContactsItem and appends it to the ContactList

printcontacts - No Parameters

  • Prints a heading
  • Prints all of the contacts in the ContactsList

savecontacts - parameter is the name of the file to save the contacts to

  • Open the file for output
  • Loop through the ContactsList
  • Concatenate the contacts values and write to the file. Be sure to use "|" as a separator and a newline at the end of the line

Main Program

  • Create a Contacts object using the data in Contacts.txt
  • Read a prompt from the keyboard
  • if "Print" then print the contacts
  • if "Add" then prompt for the contact values, and append them to the list
  • if "Save" then save the contacts list to a file
  • if "Exit" then exit the program

Save your solution as Contacts.py and submit it via Blackboard

Grading Rubric

  • 2 points - ContactsItem Object
  • 2 points - Contact Constructor
  • 1 point - add method
  • 1 point - print method
  • 2 points - save method
  • 2 points - main program

Contacts.txt

Contacts.txt contains the following:

John|Smith|101 Main|Little Rock|AR|72211|(501) 555-1212|[email protected]
Jane|Doe|5 Mockingbird Lane|Benton|AR|72215|(501) 666-1234|[email protected]
0 0
Add a comment Improve this question Transcribed image text
Answer #1

Screenshot Of Code :

# this is the contact item class stores the structure of the contact class ContactItem: def İnİt (self, FirstName , LastName37 ew contaC tact methed adds def addContact (self, firstname, lastname, address, state, ziR phene, email): ContactItem (firs66、 if name == main # creating the contact object Contactobject = Contact(Contacts.txt) # promting for user to enter the

Screenshot of Sample Output:

C:lUsers Abhineet\PycharmProjectsluntitled venv Scripts python.exe C:/Users/Abhineet/PycharmP ENTER THE COMMAND >>>COMPLETE LENTER THE NEXT COMMAND Print >>>COMPLETE LIST OF ALL CONTACTS<<< John 1 Smith 1 101 Main 1 Little Rock I AR 1 72211 1 (501) 5

Note : Contacts.txt is present in the location where the python file is present (for this solution)

Screenshot of Contact.txt before running program :

1 John | Smith| 101 MainlLittle Rock ARI722111 (501) 555-12121joe@mail.com 2 Jane | Doel5 Mockingbird Lane | Benton |AR | 722

Screenshot of Contact.txt after running Program :

1 Johnl Smith|101 MainlLittle Rock AR |722111 (501) 555-12121joe@mail.com 2 Jane | Doel 5 Mockingbird Lane | Benton |ARI 7221

Note : Same contacts are appended to the list because it is mentioned in the question for the constructor "Constructs a ContactsItem and appends it to the ContactList".

So ContactList initially contains all the contacts present in the file. And when the new contact added they are added to the ContactList. And when saved the entire ContactList gets appended to the Contacts.txt (this is according to the question)

If you want to to add just new contacts to the Contacts.txt then do not read the Contacts.txt in "__init__ " method, in this way ContactList will be empty and when you add to the ContactList, only new contacts will be present and when saved duplicate contacts will not be written to the file

So, I tried to follow the instructions mentioned in the question.

Textual Source Code :

# this is the Contact item class stores the structure of the contact
class ContactItem:
    # constructor
    def __init__(self, FirstName, LastName,
                 Address, State, Zip, Phone, Email):
        self.FirstName = FirstName
        self.LastName = LastName
        self.Address = Address
        self.State = State
        self.Zip = Zip
        self.Phone = Phone
        self.Email = Email

class Contact:
    # constructor
    def __init__(self, filename):
        # creating a contact list
        self.ContactList = []
        self.filename = filename

        # opening the file in the reading mode
        self.file = open(filename, 'r')

        # reading content of the files
        lines = self.file.readlines()

        # Constructs a ContactsItem and appends it to the ContactList
        for line in lines:
            attributes = line.strip().split("|")
            newcontact = ContactItem(attributes[0], attributes[1], attributes[2], attributes[3],
                                     attributes[4], attributes[5], attributes[6])

            self.ContactList.append(newcontact)

        # closing the file
        self.file.close()


    # this method adds a new contact to the ContactList
    def addContact(self, firstname, lastname, address, state, zip, phone, email):
        newcontact = ContactItem(firstname, lastname, address, state, zip, phone, email)
        self.ContactList.append(newcontact)

    # this method print all the contact in the contactlist
    def printContacts(self):
        # printing heading
        print(">>>COMPLETE LIST OF ALL CONTACTS<<<")

        # iterating over all headings
        for contact in self.ContactList:
            print(contact.FirstName , "|", contact.LastName, "|", contact.Address, "|"
            , contact.State, "|", contact.Zip, "|",  contact.Phone, "|" + contact.Email)


    # saving contacts to the file
    def saveContacts(self):

        # opening will in append mode
        with open(self.filename, 'a', newline='\n') as self.file:
            # iterating over ContactList and appending data to the file
            for contact in self.ContactList:
                self.file.write("\n" + str(contact.FirstName + "|" + contact.LastName + "|" + contact.Address + "|"
                + contact.State + r"|" + contact.Zip + "|" + contact.Phone + "|" + contact.Email))

            self.file.close()


if __name__ == '__main__':
    # creating the Contact Object
    ContactObject = Contact('Contacts.txt')

    # promting for user to enter the command
    prompt = input("ENTER THE COMMAND\n")

    # continue to take command form user until user enters exit
    while prompt != "Exit":
        if prompt == "Print":
            ContactObject.printContacts()

        elif prompt == "Add":
            firstname = input("Enter Firstname\n")
            lastname = input("Enter Lastname\n")
            address = input("Enter Address\n")
            state = input("Enter state\n")
            zip = input("Enter zip\n")
            phone = input("Enter phone\n")
            email = input("Enter Email\n")
            ContactObject.addContact(firstname, lastname, address, state, zip, phone, email)

        elif prompt == "Save":
            ContactObject.saveContacts()

        prompt = input("\nENTER THE NEXT COMMAND\n")
Add a comment
Know the answer?
Add Answer to:
In this assignment, you will create an application that holds a list of contact information. You ...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • In this assignment, you will create an application that holds a list of contact information. You...

    In this assignment, you will create an application that holds a list of contact information. You will be able to prompt the user for a new contact, which is added to the list. You can print the contact list at any time. You will also be able to save the contact list. Must Be In Python Format Create the following objects: ContactsItem - attributes hold the values for the contact, including FirstName - 20 characters LastName - 20 characters Address...

  • DESCRIPTION Create a C++ program to manage phone contacts. The program will allow the user to...

    DESCRIPTION Create a C++ program to manage phone contacts. The program will allow the user to add new phone contacts, display a list of all contacts, search for a specific contact by name, delete a specific contact. The program should provide the user with a console or command line choice menu about possible actions that they can perform. The choices should be the following: 1. Display list of all contacts. 2. Add a new contact. 3. Search for a contact...

  • A cell phone has the ability to save contact information. In this assignment, you will simulate...

    A cell phone has the ability to save contact information. In this assignment, you will simulate the contact list for a very simple cell phone. Download lab8.py below, rename it according to the instructions above, and make sure you understand it. Take the program above and modify it by adding the missing Contact class such that when the program is run, it produces this output. Hint: for help on formatting output, revisit the online textbook's chapter 9.5.1 Grading - 10...

  • Need help working through CLion: Description: For this assignment you will create a program to manage...

    Need help working through CLion: Description: For this assignment you will create a program to manage phone contacts. Your program will allow the user to add new phone contacts, display a list of all contacts, search for a specific contact by name, delete a specific contact. Your program should provide the user with a console or command line choice menu about possible actions that they can perform. The choices should be the following: 1. Display list of all contacts. 2....

  • This program is used to create a phonebook for storing contact information into a text file....

    This program is used to create a phonebook for storing contact information into a text file. The program begins by asking the user to provide a name for the file that will contain their phone book information. It will then ask the user to provide the names and numbers of their contacts. It should keep asking the user for contact information until they type in Done (case-sensitive). After the user types Done, store all contact information into the file name...

  • Please help!! (C++ PROGRAM) You will design an online contact list to keep track of names...

    Please help!! (C++ PROGRAM) You will design an online contact list to keep track of names and phone numbers. ·         a. Define a class contactList that can store a name and up to 3 phone numbers (use an array for the phone numbers). Use constructors to automatically initialize the member variables. b.Add the following operations to your program: i. Add a new contact. Ask the user to enter the name and up to 3 phone numbers. ii. Delete a contact...

  • Create a class named Module2. You should submit your source code file (Module2.java). The Module2 class...

    Create a class named Module2. You should submit your source code file (Module2.java). The Module2 class should contain the following data fields and methods (note that all data and methods are for objects unless specified as being for the entire class) Data fields: A String object named firstName A String object named middleName A String object name lastName Methods: A Module2 constructor method that accepts no parameters and initializes the data fields from 1) to empty Strings (e.g., firstName =...

  • In Java: Develop a simple class for a Student. Include class variables; StudentID (int), FirstName, LastName,...

    In Java: Develop a simple class for a Student. Include class variables; StudentID (int), FirstName, LastName, GPA (double), and Major. Extend your class for a Student to include classes for Graduate and Undergraduate. o Include a default constructor, a constructor that accepts the above information, and a method that prints out the contents FOR EACH LEVEL of the object, and a method that prints out the contents of the object. o Write a program that uses an array of Students...

  • C++ program Create a Student class that contains three private data members of stududentID (int), lastName...

    C++ program Create a Student class that contains three private data members of stududentID (int), lastName (string), firstName(string) and a static data member studentCount(int). Create a constructor that will take three parameters of studentID, lastName and firstName, and assign them to the private data member. Then, increase the studentCount in the constructor. Create a static function getStudentCount() that returns the value of studentCount. studentCount is used to track the number of student object has been instantiated. Initialize it to 0...

  • C++ In this assignment, you will write a class that implements a contact book entry. For...

    C++ In this assignment, you will write a class that implements a contact book entry. For example, my iPhone (and pretty much any smartphone) has a contacts list app that allows you to store information about your friends, colleagues, and businesses. In later assignments, you will have to implement this type of app (as a command line program, of course.) For now, you will just have to implement the building blocks for this app, namely, the Contact class. Your Contact...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT