Practice - code this logic
In this exercise, we are going to:
- understand the requirements provided by a product manager
- write the pseudocode for it
- sketch the flow on paper
- code the logic
The aim is to use communication (words) and visual aids to help you and others understand the code you plan to write. And then execute that plan!
The task: calculate an employee’s bonus
The logic is to determine whether an employee should earn a bonus on top of their salary. Our team product owner has given us this description:
Calculate the bonus for an employee based on their pay grade and performance rating.
- If the employee's pay grade is less than 3, the bonus is 20% of their salary.
- If the employee's pay grade is 3, 4, 5, or 6, and their performance rating is 1, the bonus is 10% of their salary in addition to any existing bonus.
- If the employee's pay grade is 3, 4, 5, or 6, and their performance rating is 2, the bonus is 90% of their salary in addition to any existing bonus.
- If the employee's pay grade is less than 5, the bonus is 50% of their salary in addition to any existing bonus.
- If the employee's pay grade is between 5 and 7 (inclusive), the bonus is 40% of their salary in addition to any existing bonus.
- If the employee's pay grade is 7 or higher, the bonus is 10% of their salary.
Calculate the total bonus based on the above rules.
Try write the pseudocode and sketch it before reading further...
Here is the completed pseudocode
STEP 2: Now, code it
There are a few ways of writing these if statements, and it is intentionally a bit confusing. It’s the kind of thing you would receive from a manager or product owner. The trick is to make it as readable and as easy to follow as possible.
The logic will be the body of a function, which receives salary, pay grade and a perfomance rating as arguments. You can copy and paste this as a starting point: