Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

OOP child's play: Day 2 #27

Open
ratulMahjabin opened this issue Apr 16, 2020 · 0 comments
Open

OOP child's play: Day 2 #27

ratulMahjabin opened this issue Apr 16, 2020 · 0 comments
Labels
Easy Difficulty Level - Easy SOLID Topic - SOLID Principles

Comments

@ratulMahjabin
Copy link
Contributor

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.
@ratulMahjabin ratulMahjabin added Easy Difficulty Level - Easy SOLID Topic - SOLID Principles labels Apr 16, 2020
@ratulMahjabin ratulMahjabin changed the title OOP Child's Play: Day 2 OOP child's play: Day 2 Apr 16, 2020
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Easy Difficulty Level - Easy SOLID Topic - SOLID Principles
Projects
None yet
Development

No branches or pull requests

1 participant