Start learning free

LESSON 98 OF 291 · CONDITIONAL STATEMENTS - CHALLENGES

Employee of the Year V2 in Kotlin: Choosing Between Two with if/else

Practice Read the full explanation

Two employees are up for the Employee of the Year award. Pick the more productive one and compute their 15% bonus.

You start with:

    val name1 = "John Doe"
    val productivity1 = 83
    val salary1 = 135000
    val name2 = "Mary Jane"
    val productivity2 = 133
    val salary2 = 335000

Build:

Mary Jane
Bonus: $50250

Rules:

  • The winner is whichever employee has the higher productivity.
  • If both have the same productivity, employee 1 (the one listed first) wins.
  • The winner's bonus is 15% of their salary, rounded down to a whole number.
  • Print the winner's name on the first line, then "Bonus: $<amount>" on the second.

Declare var winnerName: String; var winnerSalary: Int before the if, then use if/else to assign them, so both are still in scope for the prints. A tie goes to employee 1, so compare with >=.

Dividing two whole numbers (Int) drops the fraction, so winnerSalary * 15 / 100 is already rounded down: val bonus = winnerSalary * 15 / 100

+ turns the Int into text: println("Bonus: $" + bonus)

    val name1 = "John Doe"
    val productivity1 = 83
    val salary1 = 135000
    val name2 = "Mary Jane"
    val productivity2 = 133
    val salary2 = 335000

    var winnerName: String
    var winnerSalary: Int
    if (productivity1 >= productivity2) {
        winnerName = name1
        winnerSalary = salary1
    } else {
        winnerName = name2
        winnerSalary = salary2
    }
    val bonus = winnerSalary * 15 / 100
    println(winnerName)
    println("Bonus: $" + bonus)

STEP 1 OF 10
    Ctrl + Enter to check

    KOTLIN · CONDITIONAL STATEMENTS - CHALLENGES

    Employee Of The Year V2 in Kotlin, explained

    Problem overview

    Two employees are up for the award, each with a name, a productivity score and a salary. The program picks the one with the higher productivity, then prints that employee's name and bonus. The bonus is 15% of the winner's salary, rounded down. Version 1 did this for a single employee; this version adds the decision.

    In the lesson's data, John Doe has productivity 83 and a salary of 135000, and Mary Jane has productivity 133 and a salary of 335000. Mary Jane is more productive, and 15% of 335000 is 50250:

    Mary Jane
    Bonus: $50250
    

    If both employees have the same productivity, the first one (John Doe) wins.

    Approach

    The bonus formula doesn't change. What changes is whose salary it uses, and whose name is printed. So the program needs to decide once, remember the winner's name and salary, and then run the rest of the code on those two values.

    That calls for two variables, winnerName and winnerSalary, and an if/else that fills them. After the if/else, the code that calculates and prints doesn't need to know which branch ran.

    Step by step

    1. Declare the two winner variables without a value. winnerName is a String and winnerSalary is an Int. Because there is no initial value, Kotlin needs the type written out. Declare them before the if: a variable created inside the braces would disappear at the closing brace, and the lines after the if need it.
    2. Write the condition. productivity1 >= productivity2 is true when employee 1 is at least as productive. Using >= rather than > is what sends a tie to employee 1.
    3. Fill both variables in the if branch with employee 1's name and salary. The two values always go together.
    4. Add the else branch, which runs only when employee 2 is strictly more productive.
    5. Fill both variables in the else branch with employee 2's name and salary.
    6. Calculate the bonus after the if/else, from winnerSalary, with the same integer formula as version 1.
    7. Print the name, then 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
    
        var winnerName: String
        var winnerSalary: Int
        if (productivity1 >= productivity2) {
            winnerName = name1
            winnerSalary = salary1
        } else {
            winnerName = name2
            winnerSalary = salary2
        }
        val bonus = winnerSalary * 15 / 100
        println(winnerName)
        println("Bonus: $" + bonus)
    

    Kotlin details worth knowing:

    • Declaring without a value. var winnerName: String is allowed, but you can't read the variable until every path has assigned it. If the else were missing and the condition were false, println(winnerName) would be rejected with error: variable 'winnerName' must be initialized. Here both branches assign both variables, so the compiler accepts the code.
    • Comparison operators. >= means "greater than or equal", > means "strictly greater". The one-character difference decides who wins a tie.
    • Integer division. winnerSalary * 15 / 100 multiplies first and divides second, and dividing two Int values drops the fraction.
    • The dollar sign. A $ followed directly by a closing quote is printed as an ordinary character, so "Bonus: $" is a valid string.

    Following the code on the lesson's data

    The condition compares 83 with 133. 83 is not at least 133, so the if block is skipped and the else block runs. It stores "Mary Jane" in winnerName and 335000 in winnerSalary. The bonus is 335000 * 15 / 100, which is 50250, and the two println calls print the name and then Bonus: $50250.

    An alternative: if as an expression

    In Kotlin, if/else produces a value, so each variable can be set in one line:

    val winnerName = if (productivity1 >= productivity2) name1 else name2
    val winnerSalary = if (productivity1 >= productivity2) salary1 else salary2
    

    With the lesson's data this gives Mary Jane and 335000. Both variables become val, and Kotlin infers their types. The cost is that the comparison is written twice. The lesson's version, with two var declarations and a block per employee, keeps the name and salary assignments side by side, and it is the one the steps follow. Expression form is the usual choice once you only need one value from the decision.

    Edge cases and common mistakes

    • Ties. With both productivities at 120, the condition 120 >= 120 is true and John Doe wins, printing Bonus: $20250. Writing > instead would give the tie to Mary Jane and print Bonus: $50250, so the lesson's tests would fail.
    • Declaring inside the branches. A val bonus or var winnerName created inside an if block exists only inside it. Declare what you need after the block before the if.
    • Mixing employees. The name and the salary must come from the same employee. Taking the name from employee 1 and the salary from employee 2 prints a believable but wrong answer.
    • Calculating the bonus in both branches. It works, but it repeats the formula. Calculating once after the if/else keeps one copy.
    • Comparing the wrong values. The condition compares productivities, not salaries.

    Testing

    Change the productivity values in the editor above and check the output:

    productivity1productivity2output
    83133Mary Jane, Bonus: $50250
    140133John Doe, Bonus: $20250
    120120John Doe, Bonus: $20250
    Keep going with Kotlin1,600+ step-by-step lessons, from your first line of code to coding interviews. Free to start.
    Start learning free