Problem overview
Now three employees compete. Each has a name, a productivity score and a salary, and the program prints the name and 15% bonus (rounded down) of the one with the highest productivity. If several employees share the highest productivity, the one listed first wins.
With John Doe at 83 (salary 135000), Mary Jane at 133 (salary 335000) and Bob Smith at 97 (salary 210000), Mary Jane has the highest productivity and the program prints:
Mary Jane
Bonus: $50250
The previous version chose between two employees with a single if/else. This one needs a chain.
Approach
Think of the choice as a series of questions. Is employee 1 the winner? If so, we're done. If not, employee 1 has lost to someone, so only employees 2 and 3 are left. Is employee 2 at least as productive as employee 3? If so, employee 2 wins. Otherwise employee 3 is the only one left.
That maps straight onto if / else if / else. Kotlin tests the conditions from top to bottom and runs only the first branch whose condition is true. A later condition is evaluated only when everything above it was false, which is why the second condition needs to compare just employees 2 and 3.
Step by step
- Declare
winnerNameandwinnerSalarybefore the chain, with their types and no values, exactly as in the previous version. - Test employee 1. The condition is
productivity1 >= productivity2 && productivity1 >= productivity3: employee 1 must be at least as productive as both others.&&means "and", so both comparisons must hold. - In that branch, store employee 1's name and salary.
- Add an
else iffor employee 2. It only runs when employee 1 didn't win, so one comparison is enough:productivity2 >= productivity3. - Store employee 2's name and salary in that branch.
- Finish with a plain
else. If employees 1 and 2 both lost, employee 3 is the winner, so this branch needs no condition. - Store employee 3's name and salary.
- After the chain, calculate the bonus from
winnerSalaryand print the name and the bonus line.
The solution in Kotlin
val name1 = "John Doe"
val productivity1 = 83
val salary1 = 135000
val name2 = "Mary Jane"
val productivity2 = 133
val salary2 = 335000
val name3 = "Bob Smith"
val productivity3 = 97
val salary3 = 210000
var winnerName: String
var winnerSalary: Int
if (productivity1 >= productivity2 && productivity1 >= productivity3) {
winnerName = name1
winnerSalary = salary1
} else if (productivity2 >= productivity3) {
winnerName = name2
winnerSalary = salary2
} else {
winnerName = name3
winnerSalary = salary3
}
val bonus = winnerSalary * 15 / 100
println(winnerName)
println("Bonus: $" + bonus)
Kotlin details worth knowing:
- Exactly one branch runs. Because the chain ends with
else, every path assigns both variables, so Kotlin lets you read them afterwards. Without the finalelsethe compiler would report that the variables might not be initialized. &&short-circuits. If the left comparison is false, the right one isn't evaluated. It makes no difference here, but it matters when the right side could fail.- Order is meaningful. Each later condition relies on the earlier ones being false. Reordering the branches changes the meaning.
- The bonus line. As before,
winnerSalary * 15 / 100multiplies first and drops the fraction when dividing.
Following the chain on the lesson's data
With productivities 83, 133 and 97, the program takes these steps:
- The first condition asks whether 83 is at least 133 and at least 97. It is not, so the first branch is skipped.
- The
else ifasks whether 133 is at least 97. It is, so Mary Jane's name and salary are stored, and the finalelsenever runs. - The bonus is 335000 * 15 / 100, which is 50250.
The same chain written as an expression is also valid Kotlin: val w = if (...) name1 else if (...) name2 else name3 picks the name in one statement. The lesson's version keeps the name and the salary together in each branch, which is why it uses two variables.
If you drop the last else and only keep the first two branches, the compiler refuses to read the variables afterwards: println(winnerName) fails with error: variable 'winnerName' must be initialized., because nothing assigns them when both conditions are false.
Edge cases and common mistakes
- Ties with the first employee. With productivities 90, 80 and 90, employee 1 ties with employee 3 and wins, because the comparisons use
>=. The output is John Doe withBonus: $20250. - Ties between the others. With 70, 90 and 90, employee 1 loses, and
productivity2 >= productivity3is true, so Mary Jane wins withBonus: $50250. Writing>there would hand the win to Bob Smith, whose bonus is$31500. - Three-way ties. With all three at 90, the first condition holds and John Doe wins.
- Comparing only one rival. Checking
productivity1 >= productivity2alone lets employee 1 win even when employee 3 is better. - Using separate
ifstatements. Three independentifs can run more than one branch, and the last one that runs overwrites the earlier winner. Theelse ifchain prevents that. - Mixing fields. Each branch must set the name and salary of the same employee.
Testing
Try these productivity values in the editor above, keeping the other data:
| productivity1, 2, 3 | output |
|---|---|
| 83, 133, 97 | Mary Jane, Bonus: $50250 |
| 90, 80, 90 | John Doe, Bonus: $20250 |
| 70, 90, 90 | Mary Jane, Bonus: $50250 |
| 70, 80, 90 | Bob Smith, Bonus: $31500 |
| 90, 90, 90 | John Doe, Bonus: $20250 |
