Guest User

Untitled

a guest
Apr 19th, 2021
227
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.19 KB | None | 0 0
  1. using System;
  2. using Vehicles.Exceptions;
  3. using Vehicles.Interfaces;
  4.  
  5. namespace Vehicles.Models
  6. {
  7. public abstract class Vehicle : IFuel, IDriveable, IRefuelable
  8. {
  9. protected Vehicle(double fuelQuantity, double fuelConsumption)
  10. {
  11. this.FuelQuantity = fuelQuantity;
  12. this.FuelConsumption = fuelConsumption;
  13. }
  14.  
  15. public double FuelQuantity { get; private set; }
  16.  
  17. public virtual double FuelConsumption { get; protected set; }
  18.  
  19. public string Drive(double distance)
  20. {
  21. double needFuel = this.FuelConsumption * distance;
  22. if(this.FuelQuantity >= needFuel)
  23. {
  24. this.FuelQuantity -= needFuel;
  25. return $"{this.GetType().Name} travelled {distance} km";
  26. }
  27.  
  28. else
  29. {
  30. string exceptionMessage = String.Format(ExceptionMessages.NotEnoughFuelMessage, this.GetType().Name);
  31. throw new InvalidOperationException(exceptionMessage);
  32. }
  33. }
  34.  
  35. public virtual void Refuel(double amountOfFuel)
  36. {
  37. this.FuelQuantity += amountOfFuel;
  38. }
  39. }
  40. }
Advertisement
Add Comment
Please, Sign In to add comment