iut-cse/OOKata

Arthur Weasley and the Magic of the Muggles

Opened this issue · 4 comments

Overview

After all this time, the wizards have realized that muggles have some magical abilities in them. It is quite different than the wizards' magic, but still, it is magic. Arthur Weasley, a high official at the Ministry, has convinced the ministry of magic that integrating muggle magic in day to day wizarding activities will improve their efficiency.
A muggle magician was hired to implement muggle magic at Hogwarts, however, he is no longer feeling like working with wizards. Now they have hired you to complete the unfinished work. The wizards have a hard time understanding how writing the spells in a small box works, but you are master of it. As you may have guessed, muggle magic is known as software in the muggle world.
You found that the code is somewhat well-written, but there is one thing that is bothering you. There are a lot of places with code similar to the following

// Language: C#
class PetTime
{
    void TakeCareOfPet(Student student)
    {
        if (student.pet == null)
            Relax();
        else
            student.pet.Feed();
    }

    void Relax()
    {
        // relax!
    }
}

Tracking down farther, you find that the student class is something like this:

// Language: C#
class Student
{
    public readonly IPet pet;
    public Student(IPet pet)
    {
        this.pet = pet;
    }
}

That means the pet object cannot reset after an object of Student is initialized.

Tracking down even farther, you are happy to find that there is only one place from where a student object is created.

// Language: C#
class StudentRegistry
{
    public Student RegisterStudent(string petType)
    {
        var pet = GetPet(petType);
        var student = new Student(pet);
        return student;
    }

    private IPet GetPet(string petType)
    {
        if (petType == "owl")
            return new Owl();
        else if (petType == "rat")
            return new Rat();
        else
            return null;
    }
}

Assumptions

  • Owl and Rat are subtypes of IPet
  • PetTime.TakeCareOfPet is only an example of a conditional statement checking if pet is null. There are more places with a similar structure.

Task

  • Refactor the code so that the duplicate conditional statements like in PetTime.TakeCareOfPet are removed.
  • You can introduce additional classes/methods if required

Notes

There are other things in the code, but those are ignored because those are irrelevant to this problem.


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.

There could be a concrete class NoPet like owl,rat.
Remove Realx() method from PetTime class and add it to the Nopet class

https://gist.github.com/abidKiller/8a59c301eb7cdbc598b0c1f5f131b654

@abidKiller : That exactly is the solution.

Note: we prefer not posting solution in the comment thread to avoid spoilers. Please read the README

Okay sir .. should i delete it?

Okay sir .. should i delete it?

You can remove the code segment. But the text is fine. Better if you have the code in a gist or somewhere and give the link here.