Valeri
Valeri

Reputation: 97

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.

Upvotes: 0

Views: 81

Answers (3)

Doug Moses
Doug Moses

Reputation: 176

This would be a good place to add a decorator. Some pseudo code (not complete!) but hopefully you get the idea.

public class VehicleDecorator : Vehicle

  public VehicleDecorator(Vehicle vehicle)
  {
      this.vehicle = vehicle;
  } 

public class VehicleWithAc : VehicleDecorator

  public VehicleWithAc(Vehicle vehicle) : base(vehicle){}
  public override double FuelConsumption { 
               get{
                     return base.FuelConsumption+0.9 } }

Then in your program, create your car and decorate it with a VehicleWithAc decorator

Program
   var baseCar = new Car();
   var summerDriver = new VehicleWithAc(baseCar)

Upvotes: 1

Moo-Juice
Moo-Juice

Reputation: 38825

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.

Upvotes: 0

Shervin Ivari
Shervin Ivari

Reputation: 2501

If you want to use this stracture you have to set custom rules for properties.

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

    public double FuelQuantity { get; protected set; }
    private double _fuelConsumption { get;  set; }

    public double FuelConsumption {
        get { return _fuelConsumption; }
        protected set {
            _fuelConsumption = (_fuelConsumption + 0.9);
        } }

    public abstract string Drive(double distance);

    public abstract void Refuel(double liters);
}

Because your class is abstract and you dont have no overridable in properties in your derived class you cant have access to base properties.You can use condition in set for example

public double FuelConsumption {
    get { return _fuelConsumption; }
    protected set {
      if(Issummer)
      {
      _fuelConsumption = (_fuelConsumption + 0.9);
      }else{ _fuelConsumption =_fuelConsumption;}
    } }

Upvotes: 0

Related Questions