Problem overview
Now three employees compete: name1, name2, name3, each with a productivity and a salary. The program finds the one with the highest productivity and prints their name and their bonus, 15% of the salary rounded down. If two or more employees share the highest productivity, the one listed first wins.
With productivities 83, 133 and 97, Mary Jane (133, salary 335000) wins, and 335000 * 15 / 100 is 50250:
Mary Jane
Bonus: $50250
The last version chose between two employees with if/else. This one needs a chain, because there are three possible winners.
Approach: knock out candidates in order
With three employees, ask the questions in order:
- Is employee 1 at least as productive as both of the others? Then employee 1 wins.
- If not, someone else is strictly better than employee 1, so employee 1 is out. Only employees 2 and 3 are left: is employee 2 at least as productive as employee 3? Then employee 2 wins.
- Otherwise employee 3 wins, and no further test is needed.
Every comparison uses >=. That makes the first-listed employee win each tie, whichever employees are tied. The chain only reaches its second test after the first one failed, which is why the second test doesn't need to look at employee 1 again.
As before, the branches only record the winner's name and salary. The bonus is computed once, after the chain.
Writing the first test as productivity1 >= productivity2 && productivity1 >= productivity3 is the part that needs care. It is not enough to beat just one rival: the winner has to be at least as good as everyone else. Notice also that the chain is hand-written for exactly three people. A fourth employee would need a longer first test and one more branch, so the code grows with the number of employees.
Step by step
- Declare the blanks.
var winnerName: String = ""andvar winnerSalary: Int = 0, before the chain so that the lines after it can read them. - First condition.
if (productivity1 >= productivity2 && productivity1 >= productivity3) {. The&&operator means "and": both comparisons must hold. Write the closing brace of the whole chain right away. - Employee 1's branch. Assign
name1towinnerNameandsalary1towinnerSalary. - The
else if. Add} else if (productivity2 >= productivity3) {. Employee 1 already lost, so employees 2 and 3 are the only ones left to compare. - Employee 2's branch. Assign
name2andsalary2. - The final
else. Add} else {. It runs when employee 1 lost and employee 2 lost to employee 3. - Employee 3's branch. Assign
name3andsalary3. - Bonus and output. After the chain,
val bonus = winnerSalary * 15 / 100, then print the name and the"Bonus: $"line.
The solution in Scala
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 = 0
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)
Scala details to keep in mind:
- Only one branch runs. Scala checks the conditions from top to bottom and runs the first branch whose condition is true. The rest are skipped, even if they would be true too.
- Short-circuit
&&. If the left side of&&is false, the right side isn't evaluated. Here that is only a saving, since both sides are simple comparisons. - The chain is one expression.
else ifis just anelsewhose body is anotherif. That is why the finalelsebelongs to the whole chain and covers every case not handled above.
Following the program
With the starter data the first test asks whether 83 >= 133 and 83 >= 97. Both are false, so the branch is skipped. The second test asks whether 133 >= 97, which is true, so employee 2 is the winner and the final else never runs.
Testing
Changing only the productivities (the names and salaries stay as in the starter data) gives these outputs:
| Productivities (1, 2, 3) | Output |
|---|---|
| 83, 133, 97 | Mary Jane, Bonus: $50250 |
| 150, 133, 97 | John Doe, Bonus: $20250 |
| 83, 90, 97 | Bob Smith, Bonus: $31500 |
| 100, 100, 100 | John Doe, Bonus: $20250 |
| 80, 100, 100 | Mary Jane, Bonus: $50250 |
Edge cases and common mistakes
- Using
>in some tests. The tie rule only holds if every comparison is>=. With a strict>in the first test, a three-way tie would skip employee 1 and pick someone else. - Forgetting one comparison. If the first test only compares employees 1 and 2, employee 1 can win with 100 even though employee 3 has 120.
- A tie between employees 2 and 3 only. In the last row of the table, employee 1 has 80, so the first test fails. The second test is 100 >= 100, which is true, so the earlier of the two, employee 2, wins.
- Updating only one variable. Every branch must set both the name and the salary, or the output mixes two people.
Key takeaways
- A chain of
if,else ifandelsepicks exactly one branch. - Combine conditions with
&&when a candidate must beat several others. - Use
>=everywhere so that the first-listed employee wins ties. - The next version builds on this one.
