Update #2 – Milestone reached: OOP done

Another update. After a week with little time, I finally had the time to finish the final Object Oriented Programming chapter. Including the challenges and the exercises. Feels like a real milestone, as mastering OOP concepts is in my opinion really one of the basics of designing good programs. Now terms like Inheritance, Polymorphism, Encapsulation and Composition have little secrets for me.

Let’s go back to the course itself: the final challenge (explained in a video) was exactly the same as the final exercise. Makes the exercise a little less valuable unfortunately. Anyway, I have been creating Basic Hamburgers, Healthy burgers and Deluxe burgers. That was fun to do, but I really think that the trainer, even with the limited knowledge we students have at this time, did not get everything out of his solution. I liked mine better (maybe think all programmers think that about their code?). Let me explain.

Firstly, a hamburger can have one or more additions, maximum 4. An addition has a name and a price. To me it was obvious that this should be an additional class, and in the hamburger class you would have 4 fields for the additions. Like this:

public class Hamburger {

    private String name;
    private String RollType;
    private String meat;
    private double price;
    private Addition addition1;
    private Addition addition2;
    private Addition addition3;
    private Addition addition4;

And the separate class for Addition:

public class Addition {

    private String name;
    private double price;

I just learning this is called Composition, so let’s use it. But not in the exercise. Here we had to create separate fields for Addition Name and Price like this:

    private String addition1Name;
    private double addition1Price;
    private String addition2Name;
    private double addition2Price;
    private String addition3Name;
    private double addition3Price;
    private String addition4Name;
    private double addition4Price;

Not optimal to say the least. Note: when I learn how to create arrays or lists (the next chapter), I would probably use these in my code for a more generic solution.

Another part of the solution I did not like, was the creation of 4 separate methods for adding an addition, so AddAddition1, AddAddition2 etc…. In stead of one method that figures out which addition to add.

In my code there were 2 parts related to this I really liked (if I say myself). Before I explain this, I have to explain there was another requirement: the HealthyBurger allows for 2 more Additions that had to be fields in the healthyBurger class. In my solution I created a getAddition() method that figures out which Addition object has to be added. And in the AddAddition() method, the Addition is added. I added some extra fields to keep track of number of Additions added and totalAdditions allowed. It looked like this:

   public Addition getAddition(int number) {
        if ((number > 0) && (number <=maxStandardAdditions)) {
            switch(number) {
                case 1:
                    return addition1;
                case 2:
                    return addition2;
                case 3:
                    return addition3;
                case 4:
                    return addition4;
                default:
                    break;
            }
        }
        return null;
    }

    public int addAddition(String name, double price) {
        if (additionsCount < maxAdditions) {
            additionsCount++;
            Addition currentAddition = getAddition(additionsCount);
            currentAddition.setName(name);
            currentAddition.setPrice(price);
            return additionsCount;
        } else {
            return -1;
        }
    }

Not sure this is optimal, but I liked it.

Finally, in the HealthyBurger class (extending Hamburger), I override the addAddition method, so I can also take the 2 extra Additions of the HealtyBurger into account:

   @Override
    public Addition getAddition(int number) {
        if ((number > 0) && (number <= super.getMaxStandardAdditions())) {
            return super.getAddition(number);
        } else if ((number > super.getMaxStandardAdditions()) && (number <= super.getMaxAdditions())) {
            switch (number) {
                case 5:
                    return addition5;
                case 6:
                    return addition6;
                default:
                    break;
            }
        }
        return null;
    }

OK, done with OOP theory now, onto the next chapter (#8) with Arrays, Java Inbuilt Lists, Autoboxing and Unboxing. Another 5 hours and 49 minutes to go for that chapter.