228k views
0 votes
Write a class named Employee that has private data members for an employee's name, ID_number, salary, and email_address. It should have an init method that takes four values and uses them to initialize the data members. It should have get methods named get_name, get_ID_number, get_salary, and get_email_address. Write a separate function (not part of the Employee class) named make_employee_dict that takes as parameters a list of names, a list of ID numbers, a list of salaries and a list of email addresses (which are all the same length). The function should take the first value of each list and use them to create an Employee object. It should do the same with the second value of each list, etc. to the end of the lists. As it creates these objects, it should add them to a dictionary, where the key is the ID number and the value for that key is the whole Employee object. The function should return the resulting dictionary. For example, it could be used l

1 Answer

1 vote

Solution :

class Employee:

#Define the

#constructor.

def __
$\text{init}$__(
$\text{self, nam}e$, ID_number,
$\text{salary}$, email):

#Set the values of

#the data members of the class.


$\text{self.nam}e$ = name


$\text{self.ID}$_number = ID_number


$\text{self.salary}$ = salary

self.email_address = email

#Define the function

#make_employee_dict().

def make_employee_dict(list_names, list_ID, list_salary, list_email):

#Define the dictionary

#to store the results.

employee_dict = {}

#Store the length

#of the list.

list_len = len(list_ID)

#Run the loop to

#traverse the list.

for i in range(list_len):

#Access the lists to

#get the required details.

name = list_names[i]

id_num = list_ID[i]

salary = list_salary[i]

email = list_email[i]

#Define the employee

#object and store

#it in the dictionary.

employee_dict[id_num] = Employee(name, id_num, salary, email)

#Return the

#resultant dictionary.

return employee_dict

User Giacomo
by
3.7k points