The ternary operator is a lightweight, compact alternative for simple if/else statements.
Usually used in (but not restricted to) return statements, it needs just one single line to make the decision, returning the left value if the expression is true and the right value if false, as follows:
boolean expr = 0 != 200;
// Ternary statement
int value = expr ? 22 : 33;
// => 22
A lot of simple if/else expressions can be simplified using ternary operators.
In this exercise, you'll be implementing rules for calculating the total salary of an employee in a month. The International Siderurgy Company (ISC) requires assistance in calculating employee salaries, considering various factors that can impact the final wage.
You have three tasks, and you should use the ternary operator instead of if/else statements to implement them.
Implement the salaryMultiplier method, which returns the salary multiplier based on the number of days an employee skipped work.
Apply a 15% penalty if the employee skipped at least five days.
int daysSkipped = 3;
salaryMultiplier(daysSkipped);
// => 1.0
daysSkipped = 7;
salaryMultiplier(daysSkipped);
// => 0.85
Implement the bonusMultiplier and bonusForProductsSold methods.
The ISC pays ten monetary units for each product sold, and if an employee sells twenty products or more, the multiplier improves to thirteen.
bonusMultiplier should determine which multiplier to apply, and bonusForProductSold should return the total bonus in monetary units.
int productsSold = 21;
bonusMultiplier(productsSold);
// => 13
bonusForProductsSold(productsSold);
// => 273
productsSold = 5;
bonusMultiplier(productsSold);
// => 10
bonusForProductsSold(productsSold);
// => 50
Implement the finalSalary method.
It should multiply the base salary of 1000.00 by the salary multiplier, add the bonus, and return the result.
However, salaries should be capped at 2000.00.
int daysSkipped = 2;
int productsSold = 3;
finalSalary(daysSkipped, productsSold);
// => 1030.00
productsSold = 90;
finalSalary(daysSkipped, productsSold);
// => 2000.00
Sign up to Exercism to learn and master Java with 25 concepts, 147 exercises, and real human mentoring, all for free.