Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

PYTHON PROGRAM (NOT JAVA) Do not use import please. i didn\'t learn import yet .

ID: 3794500 • Letter: P

Question

PYTHON PROGRAM (NOT JAVA)

Do not use import please. i didn't learn import yet . Thank you

Problem 1: Rolodex (Note: A Rolodex is a Contact List) Write a program that manages a rolodex. This program prompts the user for the name of a rol file. This program should be able to load any rol file and require that the file extension -i.e the last 4 letters of the file be rol. The file rol is consider a valid file. If the file exists the program loads in the contacts stored in the file. A Rolodex contains unique individuals, no duplicate names are allowed. However, name lookups are case sensitive, so "Apple" and "apple" are considered different names allowed However, name

Explanation / Answer

#!/usr/bin/python

import os.path

class Contact:

    def __init__(self, name, phone, email, note=""):
        self.name = name
        self.phone = phone
        self.email = email
        self.note = note

    def displayContact(self) :
        print "Name : ", self.name
        print "Phone Number : ", self.phone
        print "Email : ", self.email
        print "Note : ", self.note, " "

def roledoxMenu() :
    print " Insert(I), Remove(R), Lookup By Name(F), Lookup By Phone Number (N), "
    print "Lookup by Email(E), Save(S), Quit and Save(Q), Print All(P), Quit and Don't Save(X) "

# Maintains the list of contact objects
contactList = []

# Looking up by Name
def lookUpByName(name) :
    for contact in contactList :
        if contact.name == name :
            return
    return False

def lookUpByName2() :
    name = raw_input(" Enter user's name to be searched : ")
    for contact in contactList :
        if contact.name == name :
            contact.displayContact()
            return True;
    print "Contact Not Found!"

# Looking up by Phone number
def lookUpByPhone(phone) :
    for contact in contactList :
        if contact.phone == phone :
            return True
    return False

def lookUpByPhone2() :
    phone = raw_input(" Enter phone number of the contact to be searched : ")
    for contact in contactList :
        if contact.phone == phone :
            contact.displayContact()
            return True;
    print "Contact Not Found!"

# Looking up by Email
def lookUpByEmail(email) :
    for contact in contactList :
        if contact.email == email :
            return True
    return False

def lookUpByEmail2() :
    email = raw_input(" Enter email of the contact to be searched : ")
    for contact in contactList :
        if contact.email == email :
            contact.displayContact()
            return True;
    print "Contact Not Found!"


# Inserts the user data into the contact List
def insert() :
    print " Enter the user details "
    name = raw_input("Name : ").strip()
    phone = raw_input("Phone : ").strip()
    email = raw_input("Email : ").strip()
    note = raw_input("Note : ").strip()

    name.replace(" ", " ")
    phone.replace(" ", " ")
    email.replace(" ", " ")

    # Create an object and store it
    if lookUpByName(name) == False:
        contact = Contact(name, phone, email, note)
        contactList.append(contact)
    else :
        print "Contact Already Exists"


def remove() :
    userName = raw_input(" Enter the name of the contact to be removed : ")
    for contact in contactList :
        if contact.name == userName :
            contactList.remove(contact)
            break;

def save(fileName) :
    fileInstance = open(fileName,'w')
    for contact in contactList:
        fileInstance.write(contact.name), fileInstance.write(" ")
        fileInstance.write(contact.phone), fileInstance.write(" ")
        fileInstance.write(contact.email),fileInstance.write(" ")
        fileInstance.write(contact.note), fileInstance.write(" ")

def printAll() :
    for contact in contactList :
        contact.displayContact()

print "Roledox program can load an old Roledox or create a new one"
print "If you specity a file that doesn't exists then the program will create it when saving "

fileName = ""
while (True) :
    fileName = raw_input("Please Enter the name of the Roledox File(.rol) to load and save to : ")
    if (fileName.count(".rol") == 1) :
        break;
    else :
        print " Invalid File name entered (Must end with .rol extension)"

doesFileExists = False;
if os.path.isfile(fileName) :
        doesFileExists = True

# If the file exists then Load the existing data into the contactList
if doesFileExists :
    fileInstance = open(fileName,'r')
    fileText = fileInstance.read()
    lineSplits = fileText.split(" ")
    for line in lineSplits :
        if line == "" :
            continue
        wordSplits = line.split(" ")
        name = wordSplits[0]
        phone = wordSplits[1]
        email = wordSplits[2]
        note = ""
        for i in range(3, len(wordSplits)) :
                note += wordSplits[i]
                if (i != len(wordSplits)) :
                    note += " "
        contact = Contact(name, phone, email, note)
        contactList.append(contact)


while (True) :
    roledoxMenu()
    userChoice = raw_input("Enter your option : ")

    if userChoice == "I" or userChoice == "i":
        insert()
    elif userChoice == "R" or userChoice == "r":
        remove()
    elif userChoice == "F" or userChoice == "f":
        lookUpByName2()
    elif userChoice == "N" or userChoice == "n":
        lookUpByPhone2()
    elif userChoice == "E" or userChoice == "e":
        lookUpByEmail2()
    elif userChoice == "S" or userChoice == "s":
        save(fileName)
    elif userChoice == "P" or userChoice == "p":
        printAll()
    elif userChoice == "Q" or userChoice == "q":
        save(fileName)
        exit()
    elif userChoice == "X" or userChoice == "x":
        exit()
    else :
        print "Invalid Option Enter !!"