Simple People Finder
Background
Every company has a database (or multiple databases) of its employees. In which, every employee’s information is stored in a well-designed structure. Managers can use this database to search employee information for different purposes (promotion, assignment, relocation, etc.).
In this assignment, you will implement a system that can
- Load a data file that contains 1000 data entries.
- Provide users a feature to search people by their first name.
- Output the search result to the user.
In real-life, employee information should be stored in a database system that is very complex and secure. In this assignment, we are not going to implement that database system, but try to utilize the data structure we learned in the past 2 weeks, which is List.
For example, if the user wants to search records with the first name “Terry”, your program should provide the user with the following 2 records:
The user does not have to enter the data file name. Let’s say it is always by default a file named data.txt and it will always be put in the folder together with your program. Your program should continue asking for user input until the user types in “EXIT” or “exit”
Sample Output:
========================
| Simple People Finder |
========================
Loading data file... Done!
Enter the first name of the record: Terry [enter]
Searching...
Done!
Here are the results:
Record #1
User ID: 12
First Name: Terry
Last Name: Rennolds
Email: trennoldsb@yellowpages.com
Gender: Male
Login IP: 236.60.140.5
SIN: 397-03-9254
Record #2
User ID: 273
First Name: Terry
Last Name: Wallsam
Email: twallsam7k@wired.com
Gender: Male
Login IP: 96.99.102.176
SIN: 610-81-9486
Enter the first name of the record: Emily [enter]
Searching...
Done!
Here are the results:
There is no record found with the first name Emily.
Enter the first name of the record: exit [enter]
Thank you for using Simple People Finder. See you next time! =)
End of Processing...
Solution:
class Employee:
def __init__(self, id, fName, lName, email, gender, ip, sin):
self.id = id
self.fName = fName
self.lName = lName
self.email = email
self.gender = gender
self.ip = ip
self.sin = sin
def get_id(self):
return self.id
def get_fName(self):
return self.fName
def get_lName(self):
return self.lName
def get_email(self):
return self.email
def get_gender(self):
return self.gender
def get_ip(self):
return self.ip
def get_sin(self):
return self.sin
def process_file(filename):
f = open(filename, 'r')
result = []
for line in f.readlines():
parts = line.split("\t")
result.append(Employee(int(parts[0]), parts[1], parts[2], parts[3], parts[4], parts[5], parts[6]))
f.close()
return result
def search(employees, name):
result = []
for e in employees:
if e.get_fName() == name:
result.append(e)
return result
if __name__ == '__main__':
employees = process_file('data.txt')
while True:
name = input("Please, enter first name to search: ")
if name.lower() == 'exit':
break
result = search(employees, name)
if len(result) == 0:
print('No data found')
print()
else:
for i in range(len(result)):
print('Record #' + str(i+1))
print('User ID: ' + str(result[i].get_id()))
print('First Name: ' + result[i].get_fName())
print('Last Name: ' + result[i].get_lName())
print('Email: ' + result[i].get_email())
print('Gender: ' + result[i].get_gender())
print('Login IP: ' + result[i].get_ip())
print('SIN: ' + result[i].get_sin())