Skip to content
Snippets Groups Projects
Commit bec33738 authored by AB8057's avatar AB8057
Browse files

Upload New File

parent 290aa767
No related branches found
No related tags found
No related merge requests found
#Define a module and struct Employee, and add struct fields:
#first name
#last name
#id number
#salary
#job, which is one of the {:none, :coder, :designer, :manager, :ceo }
#id number has a default value that is previous id + 1
#salary has a default value 0
#job has a default value of :none
#Implement functions Employee.promote and Employee.demote which updates the job accordingly.
#job :none salary is set to 0
#each job above :none gets 2000 more salary than previous
#Test your Employee module and its promote and demote functions.
defmodule Employee do
defstruct [
:first_name,
:last_name,
id_number: 0,
salary: 0,
job: :none
]
def new_employee(firstname, lastname) do
%Employee{first_name: firstname, last_name: lastname}
end
#promoting
def promote(%Employee{} = employee) do
new_job = promotion(employee.job)
new_salary = update_salary(new_job)
%Employee{employee | job: new_job, salary: new_salary}
end
#demoting
def demote(%Employee{} = employee) do
new_job = demotion(employee.job)
new_salary = update_salary(new_job)
%Employee{employee | job: new_job, salary: new_salary}
end
defp update_salary(:none), do: 0
defp update_salary(job), do: 2000 + update_salary(demotion(job))
#promotion order
defp promotion(:none), do: :coder
defp promotion(:coder), do: :designer
defp promotion(:designer), do: :manager
defp promotion(:manager), do: :ceo
#demotion order
defp demotion(:ceo), do: :manager
defp demotion(:manager), do: :designer
defp demotion(:designer), do: :coder
defp demotion(:coder), do: :none
end
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment