-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathContactManager.py
More file actions
125 lines (110 loc) · 3.15 KB
/
ContactManager.py
File metadata and controls
125 lines (110 loc) · 3.15 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
################################################################################
# File Name: ContactManager.py
# Author: Debbie Heisler
# Date: April 4, 2014
# Description:
# This is the contact manager main driver function/file.
###############################################################################
""" Contact Manager main driver functions/files """
from Person import Person
from List import List
import Menus
quit = False # global variable to make getting out of here easier
contactList = List()
def readInContacts():
""" Read in the default contact list file """
file = open('contacts.txt', 'r')
for line in file:
fields = line.split(',')
peep = Person(fields[0].strip(), fields[1].strip(), fields[2].strip())
contactList.addPerson(peep)
def printContactsToFile():
""" Print the contacts to a file. Will loop until get a good file name """
while True:
print("File Name")
fn = input()
try:
file = open(fn, 'w')
contactList.printToFile(file)
print("Contacts written to " + fn)
break
except IOError:
continue
def addNewContact():
""" Adds a new contact to the contact manager. Does no error checking """
global contactList
print("First Name:",)
fn = input()
print("Last Name:",)
ln = input()
print("Email:",)
email = input()
peep = Person(fn, ln, email)
contactList.addPerson(peep)
def handleFoundPerson(person, option):
""" Error checks person. Prints it or an error """
if person is None:
print("No one matches that criteria")
elif option == "search":
person.printToScreen()
elif option == "delete":
contactList.deletePerson(person)
def checkSearchInput(selected, option):
""" Handles the input for searching. """
if selected == 0:
Menus.printSearchCriteria("First Name")
searchStr = input()
peep = contactList.matchPersonByFirstName(searchStr)
handleFoundPerson(peep, option)
elif selected == 1:
Menus.printSearchCriteria("Last Name")
searchStr = input()
peep = contactList.matchPersonByLastName(searchStr)
handleFoundPerson(peep, option)
elif selected == 2:
Menus.printSearchCriteria("Email")
searchStr = input()
peep = contactList.matchPersonByEmail(searchStr)
handleFoundPerson(peep, option)
else:
print("That is not a valid selection")
def searchForPerson(option):
""" Handles the option to search for a person to print or delete """
Menus.printSearchMenu()
while True:
try:
selection = int(input())
checkSearchInput(selection, option)
break
except ValueError:
print("Numbers only. Try again")
continue
def checkInput(selected):
""" Checks the input to make sure it is a valid main menu option """
global quit
if selected == 1:
contactList.printToScreen()
elif selected == 2:
searchForPerson("search")
elif selected == 3:
addNewContact()
elif selected == 4:
searchForPerson("delete")
elif selected == 5:
printContactsToFile()
elif selected == 6:
quit = True
else:
Menus.printInvalidOption(selected)
#####################
# The Main function
####################
if __name__ == "__main__":
readInContacts()
while (quit == False):
Menus.printMainMenu()
try:
selection = int(input())
checkInput(selection)
except ValueError:
continue