iut-cse/OOKata

OOP Child's Play: Day 2

Closed this issue · 0 comments

Overview

Boltu recently started learning advance object oriented concepts. So he asked his brother who is a CSE graduate to give him a OOP exercise. His brother gave him a code(given below)

class Employee
{
    private String position;
    private double salary;

    public Employee(String position)
    {
        this.salary = 0;
        this.position = position;
    }

    public String getPosition()
    {
        return position;
    }
    public double getSalary()
    {
        return salary;
    }


    public double calculateSalary() {
        switch (getPosition()) {
            case "Manager":
                salary = 100000;
                break;
            case "Senior_officer":
                salary = 50000;
                break;
            case "Junior_officer":
                salary = 30000;
                break;

            default:
                System.out.println("position undefined");
                salary = -1;
                break;
        }

        return salary;
    }
}

and asked him to find out the problem and rewrite it. Boltu spent many hours to find out problem but he noticed that the code works fine. His brother then explained him a concept mentioning "Though the code works fine it is not open for extension and not closed for modification". Then Boltu understood and was able to rewrite the given code.

Can you rewrite the code?

Assumptions

More employee position may be included in calculateSalary() method in the future.

Task

Find out the problem and rewrite code accordingly


Reminders

  • React to the problem if you find it interesting and helpful. This will help others to easily identify good problems to solve.
  • Feel free to comment about the problem. Is the description unclear? Do you think it is too easy or too difficult than what is mentioned? Comment about it.
  • Discussion about the solution is OK. But do not paste a solution here. Give a link to the solution instead.
  • Do you have an interesting problem? Post it.