Search code examples
c#oopinheritancepolymorphismabstract-class

How to increase a derived class value


I am modelling a program that simulates driving and refueling cars and trucks. So this is what I have done so far:

public abstract class Vehicle
{
    protected Vehicle(double fuelQuantity, double fuelConsumption)
    {
        this.FuelQuantity = fuelQuantity;
        this.FuelConsumption = fuelConsumption;
    }

    public double FuelQuantity { get; protected set; } 

    public double FuelConsumption { get; protected set; } 

    public abstract string Drive(double distance);

    public abstract void Refuel(double liters);
}

public class Car : Vehicle
{
    public Car(double fuelQuantity, double fuelConsumption) : base      (fuelQuantity, fuelConsumption)
    {       
    }

    public override string Drive(double distance)
    {
    }

    public override void Refuel(double liters)
    {
    }
}

So, I want to increase the value of the fuel consumption of the cars with 0.9 liters (it's summer, so cars use air conditioners). Where this can be done? I don't want to do it in the constructor because I don't think it's okay.


Solution

  • I think the problem you have is that you're passing fuelConsumption as a single variable in to the constructor, thereby stating

    This is the fuel consumption of the car, full stop.

    As you've found out, working through the problem - fuel consumption isn't a static thing, it's dependant on other variables, such as whether the AC is on. Doug was getting close with his mention of the decorator, but I think it can be a little simpler, but more flexible.

    I think you should still pass a fuel consumption figure in, and for simplicitys sake, we'll call it baseFuelConsumption. Remember, vehicles are usually graded on urban, and highway fuel consumptions as they are generally different, but for the purposes of this, we'll ignore it.

    Leaving out distance travelled etc, we have:

    public abstract class Vehicle
    {
        private readonly double _baseFuelConsumption;
    
        protected double BaseFuelConsumption => _baseFuelConsumption;
    
        protected Vehicle(double baseFuelConsumption) => _baseFuelConsumption = baseFuelConsumption;
    
        public virtual double ActualFuelConsumption => BaseFuelConsumption;
    }
    

    So, how much extra fuel consumption does an AC use? Let's take Doug's answer as a base-point, and give that to our car....

    public class Car : Vehicle
    {
        private const double _ACModifier = 0.9;
    
        public Car()
        :base(1)
        {       
        }
    
        public bool IsACOn { get; set; }
    
        public override double ActualFuelConsumption
        {
            get
            {
                double consumption = base.ActualFuelConsumption;
                consumption += IsACOn ? _ACModifier : 0;
                return consumption;
            }
        }
    }
    

    Now, for the purposes of your simulation you can switch the AC on and off, over time, and measure the ActualFuelConsumption property over time.