# Kyle S. Dencker
# 7/17/2013
# Solution for Telephone from the SI@UCF 2013 assignments

# Two lists that make up the directory.   
dir_names = []
dir_phone = []

# Inserts a contact (name/phone) into the directory.
# It first looks to see if the name is in the list.
def insertContact(name, phone):
    for i in range(len(dir_names)):
        if (dir_names[i] == name):
            dir_phone[i] = phone
            return
    dir_names.insert(len(dir_names),name)
    dir_phone.insert(len(dir_phone),phone)

# Looks up the name within the list.
def lookup(name):
    for i in range(len(dir_names)):
        if (dir_names[i] == name):
            print(dir_names[i], "'s phone number is ", dir_phone[i], sep="")
            return
    print(name, "was not found")

# Looks up the phone within the list
def lookup_phone(phone):
    for i in range(len(dir_names)):
        if (dir_phone[i] == phone):
            print(dir_names[i], "'s phone number is ", dir_phone[i], sep="")
            return
    print(phone, "was not found")

# Reads the file file, trims the line (gets rid of new line characters)
# then it inserts the contact in the list.
def readFile(fileName):
    inputFile = open(fileName, "r")
    numOfLines = int(inputFile.readline())
    for i in range(numOfLines):
        insertContact(inputFile.readline().strip(), inputFile.readline().strip())
                   
    inputFile.close()

# Deletes both the name and number out of the list.
def delete(name):
    for i in range(len(dir_names)):
        if (dir_names[i] == name):
            dir_names.pop(i)
            dir_phone.pop(i)
            return

# Leftover debug code.   When debugging I want to see what is in the list.
def print_directory():
    for i in range(len(dir_names)):
        print(dir_names[i], "-", dir_phone[i])

# Prints out the menu, and returns the selection.
def mainMenu():
    print("Please make a selection from the following choices:\n")
    print("1) Add entries to the telephone directory from a file.")
    print("2) Add an individual entry/Change an existing entry.")
    print("3) Look up a phone number by name.")
    print("4) Look up a person with their phone number.")
    print("5) Delete an entry, by name only.")
    selection = int(input("6) Quit\n"))
    print()
    return selection

def main():
    option = mainMenu()
    while (option != 6):
        if (option == 1):
            filename = input("What is the name of the file with the entries to add?\n")
            readFile(filename)
        elif (option == 2):
            name = input("What is the name of the person to add/edit to the directory?\n")
            phone = input("What is his/her phone number?\n")
            insertContact(name, phone)
        elif (option == 3):
            name = input("Whose phone number do you want to look up?\n")
            lookup(name)
        elif (option == 4):
            phone = input("Enter the phone number you want to look up.\n")
            lookup_phone(phone)
        elif (option == 5):
            name = input("Whose entry would you like to delete?\n")
            delete(name)
        #elif (option == 7):
        #    print("DEBUG Print Directory")
        #    print_directory()
        else:
            print("Invaild option.")
            
        print()

        option = mainMenu()



main()
