Trying to translate java keyword super to c# keyword base

36 views Asked by At

I am translating code from Design Pattern series made by Derek Banas, the design pattern in question is Decorator. But I got stuck on translating super to base.

The code is supposed to make pizza and dynamically add ingredients with their price included. For example pizza costs 4 dollars, and when we add Mozzarella to it which is 50 cents, it should be 4,50. If we were to add more ingredients to pizza say Tomato sauce which is 35 cents, the price would become 4,85.

However when he tests his code he gets the expected results, and I get only base price of pizza.

So I suspect the problem is that keyword super from java and keyword base from c# don't work the same way?

This is his code:

public class Mozzarella extends ToppingDecorator {

    public Mozzarella(Pizza newPizza) {
        
        super(newPizza);
        
        System.out.println("Adding Dough");
        
        System.out.println("Adding Moz");
    }
    
    // Returns the result of calling getDescription() for
    // PlainPizza and adds " mozzarella" to it
    
    public String getDescription(){
        
        return tempPizza.getDescription() + ", mozzarella";
        
    }
    
    public double getCost(){
        
        System.out.println("Cost of Moz: " + .50);
        
        return tempPizza.getCost() + .50;
        
    }
    
}

And this is mine

public class Mozzarella : ToppingDecorator
    {
        public Mozzarella(Pizza newPizza) : base(newPizza)
        {
            
            Console.WriteLine("Adding Mozzarella to Pizza");
        }

        new public double getCost()
        {
            return tempPizza.getCost() + 0.50;
        }

        new public string getDescription()
        {
            return tempPizza.getDescription() + ", Mozzarella";
        }
    }
}

The problem is when I try to test the code in main class, I get different result and we both run the same code in main:

public static void Main(string[] args)
        {

            Pizza pizza = new TomatoSauce(new Mozzarella(new PlainPizza()));

            Console.WriteLine(pizza.getDescription());
            Console.WriteLine(pizza.getCost());
            
        }

His results are the expected ones; cost is 4,85 and description is thin dough, mozzarella and tomato sauce. But my results are; cost is 4,00 (cost of plain pizza) and description is just thin dough.

0

There are 0 answers