Guest User

Vehicles-2

a guest
Mar 2nd, 2018
240
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. public abstract class Vehicle
  2. {
  3. private double fuelQuantity;
  4. private double fuelConsumptionInLitersPerKm;
  5.  
  6.  
  7. public Vehicle(double fuelQuantity, double fuelConsumption)
  8. {
  9. this.FuelQuantity = fuelQuantity;
  10. this.FuelConsumption = fuelConsumption;
  11. }
  12.  
  13. public double FuelQuantity
  14. {
  15. get => this.fuelQuantity;
  16. set => this.fuelQuantity = value;
  17. }
  18. public double FuelConsumption
  19. {
  20. get => this.fuelConsumptionInLitersPerKm;
  21. set => this.fuelConsumptionInLitersPerKm = value;
  22. }
  23.  
  24. public string Drive(double distance)
  25. {
  26. if (distance * FuelConsumption > FuelQuantity)
  27. {
  28. return $"{this.GetType().Name} needs refueling";
  29. }
  30. this.FuelQuantity -= distance * this.FuelConsumption;
  31. return $"{this.GetType().Name} travelled {distance} km";
  32. }
  33.  
  34. public virtual void Refuel(double quantity)
  35. {
  36. this.FuelQuantity += quantity;
  37. }
  38.  
  39. public override string ToString()
  40. {
  41. return $"{this.GetType().Name}: {this.FuelQuantity:f2}";
  42. }
  43. }
RAW Paste Data