PYTHON . John Zelle – 3rd Edition
Chapter 12
Object-Oriented Design
(Programming Exercise on page 458)
Q3
Write a program to keep track of conference attendees. For each attendee, your program should keep track name, company, state, and email address. Your program should allow users to do things such as add a new attendee, display information on an attendee, delete an attendee, list the names and email addresss of all attendees, and list the names and email addresses of all attendees from a given state. The attendee list should be stored in a file and loaded when the program starts.
'''
Comment if you have any doubts and Please Like The Answer
'''
'''
File Name: Attendee.py
'''
class Attendee:
def __init__(self,name,company,state,email):
self.name = name.title()
self.company =
company.title()
self.state = state.upper()
self.email = email.lower()
def __str__(self):
return "\n{0:^15} {1:^15} {2:^15}
{3:^15}".format(self.name,self.company,self.state,self.email)
'''
File Name: Attendees.py
'''
from Attendee import Attendee
class Attendees:
def __init__(self):
self.attendees = []
self.count = 0
def addAttendee(self,attendee):
if(type (attendee) ==
Attendee):
self.attendees.append(attendee)
self.count+=
1
else:
print("\nNeed
Attendee object.")
def deleteAttendee(self,email):
email = email.lower()
for i,attendee in
enumerate(self.attendees):
if(attendee.email.lower() == email):
att = self.attendees.pop(i)
self.count-=1
print("\nSuccessfully Deleted attendee with:
",att)
return None
break
print("\nEmail:",email,"Not
Found")
def displayAttendeeByName(self,name):
print("\nList of attendees having
name:",name)
name = name.lower()
found = False
for i,attendee in
enumerate(self.attendees):
if(name in
attendee.name.lower()):
if(not found):
print("--------------------------------------------------------------------\n")
print("\n{0:^15} {1:^15}
{2:^15} {3:^15}".format("Name","Company","State","Email"))
print("\n--------------------------------------------------------------------\n")
print("\n{0:^15} {1:^15} {2:^15}
{3:^15}".format(attendee.name,attendee.company,attendee.state,attendee.email))
found = True
if(not found):
print("\nList is
Empty")
return
None
print("\n--------------------------------------------------------------------\n")
def displayAttendeeByNamesAndEmails(self):
print("\nTotal Attendees
:",self.count)
if(self.count == 0):
print("\nList is
Empty")
return
None
print("--------------------------------------------------------------------\n")
print("\n{0:^15}
{1:^15}".format("Name","Email"))
print("\n--------------------------------------------------------------------\n")
for i,attendee in
enumerate(self.attendees):
print("\n{0:^15}
{1:^15}".format(attendee.name,attendee.email))
print("\n--------------------------------------------------------------------\n")
def displayAttendeeByState(self,state):
print("\nList of attendees from
State :",state)
found = False
state = state.lower()
for i,attendee in
enumerate(self.attendees):
if(state in
attendee.state.lower()):
if(not found):
print("--------------------------------------------------------------------\n")
print("\n{0:^15} {1:^15}
{2:^15} {3:^15}".format("Name","Company","State","Email"))
print("\n--------------------------------------------------------------------\n")
print("\n{0:^15} {1:^15} {2:^15}
{3:^15}".format(attendee.name,attendee.company,attendee.state,attendee.email))
found = True
if(not found):
print("\nList is
Empty")
return
None
print("\n--------------------------------------------------------------------\n")
def storeList(self,fileName):
print()
try:
f =
open(fileName,"w")
for attendee in
self.attendees:
print("Writing to file :
{},{},{},{}\n".format(attendee.name,attendee.company,attendee.state,attendee.email))
f.write("{},{},{},{}\n".format(attendee.name,attendee.company,attendee.state,attendee.email))
f.close()
except Exception as e:
print("\nError
occured while writing data to file:",fileName)
def readList(self,fileName):
try:
loaded = 0
f =
open(fileName,"r")
line =
f.readline()
while(line):
line = line.strip()
data = line.split(",")
if(len(data) != 4):
print("\nline :",line,"has
invalid number of fields")
else:
if(not
self.is_array_has_empty_string(data)):
att
=Attendee(data[0],data[1],data[2],data[3])
print("Adding: ",att)
self.addAttendee(att)
self.count+=1
loaded+=
1
line = f.readline()
except Exception as e:
print("\nError
occured while writing data to file:",fileName)
print("\nLoaded:",loaded,"Attendees
from file :",fileName)
def is_array_has_empty_string(self,arr):
has = False
for i in arr:
if(i.strip() ==
""):
has = True
break
return has
'''
File Name: conference.py
'''
from Attendees import Attendees
from Attendee import Attendee
def display_menu():
print("\nConference Management.\n")
print("\n1. Add Attendee")
print("2. Delete Attendee by Email")
print("3. Display Attendee by Name")
print("4. Display All Attendees Names and
Emails")
print("5. Display Attendees by State")
print("0. Quit")
print("Enter your choice: ",end="" )
def get_choice():
is_valid = False
choice = -1
while(not is_valid):
display_menu()
try:
choice =
int(input("").strip())
is_valid =
True
except Exception as e:
print("\nInvalid
choice. try again")
return choice
def is_array_has_empty_string(arr):
has = False
for i in arr:
if(i.strip() == ""):
has = True
break
return has
def get_attendee():
print()
name = input("Enter Attendee Name : ").strip()
company = input("Enter Attendee Company :
").strip()
state = input("Enter Attendee State : ").strip()
email = input("Enter Attendee Email: ").strip()
if(is_array_has_empty_string([name,company,state,email])):
print("\nEach Information of
Attendee must not empty and must not contain only spaces. Try
again")
return None
else:
return
Attendee(name,company,state,email)
def manage_confernce():
conference = Attendees()
conference.readList("conference.txt")
choice = -1
while(choice != 0):
choice = get_choice()
if(choice == 1):
att =
get_attendee()
if(att !=
None):
conference.addAttendee(att)
elif(choice == 2):
email =
input("\nEnter email of the Attendee to remove: ").strip()
conference.deleteAttendee(email)
elif(choice == 3):
name =
input("\nEnter Name of the Attendee to Display: ").strip()
conference.displayAttendeeByName(name)
elif(choice == 4):
conference.displayAttendeeByNamesAndEmails()
elif(choice == 5):
state =
input("\nEnter State of the Attendees to Display: ").strip()
conference.displayAttendeeByState(state)
conference.storeList("conference.txt")
print("\nThank You.")
def main():
manage_confernce()
if __name__ == "__main__":
main()
















---------------- after output conference.txt --------
John Wick,Smiths,UK,john@wick.com
Tony Stark,Stark Industries,USA,tony@avengers.com
Ant Man,Pim Tech,USA,ant@man.com
PYTHON . John Zelle – 3rd Edition Chapter 12 Object-Oriented Design (Programming Exercise on page 458)...