simeon3000

class Vehicle

Mar 5th, 2018
225
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 1.35 KB | None | 0 0
  1. using System;
  2.  
  3. public abstract class Vehicle
  4. {
  5.     public Vehicle(double quantity, double consumption, double capacity)
  6.     {
  7.         this.FuelQuantity = quantity;
  8.         this.FuelConsumption = consumption;
  9.         this.TankCapacity = capacity;
  10.     }
  11.  
  12.     public double FuelQuantity { get; protected set; }
  13.  
  14.     protected double FuelConsumption { get; set; }
  15.  
  16.     protected double TankCapacity { get; set; }
  17.  
  18.     public double AircondMultiplier { get; protected set; }
  19.  
  20.     public double FuelMultiplier { get; protected set; }
  21.  
  22.  
  23.     public string Drive(double distance, double aircondMultiplier)
  24.     {
  25.         double fuelNeeded = distance * (this.FuelConsumption + aircondMultiplier);
  26.         if (fuelNeeded < this.FuelQuantity)
  27.         {
  28.             this.FuelQuantity -= fuelNeeded;
  29.             return $"{this.GetType().Name} travelled {distance} km";
  30.         }
  31.  
  32.         return $"{this.GetType().Name} needs refueling";
  33.     }
  34.  
  35.     public void Refuel(double liters, double fuelMultiplier)
  36.     {
  37.         if (this.FuelQuantity + liters > this.TankCapacity)
  38.         {
  39.             throw new ArgumentException($"Cannot fit {liters} fuel in the tank");
  40.         }
  41.  
  42.         this.FuelQuantity += liters * fuelMultiplier;
  43.     }
  44.  
  45.     public override string ToString()
  46.     {
  47.         return $"{this.GetType().Name}: {this.FuelQuantity:f2}";
  48.     }
  49. }
Advertisement
Add Comment
Please, Sign In to add comment