Advertisement
1_9tdiMamkaMu

Vehicle

Apr 21st, 2021 (edited)
962
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 2.86 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Text;
  4.  
  5. namespace Vehicles.Models
  6. {
  7.     public abstract class Vehicle
  8.     {
  9.         protected double fuelQuantity;
  10.         protected double fuelConsumption;
  11.         protected double tankCapacity;
  12.  
  13.         public Vehicle(double fuelQuantity, double fuelConsum, double tankCapacity)
  14.         {
  15.             if (fuelQuantity > tankCapacity)
  16.             {
  17.                 FuelQuantity = 0;
  18.             }
  19.             else
  20.             {
  21.                 FuelQuantity = fuelQuantity;
  22.             }
  23.            
  24.             FuelConsumption = fuelConsum;
  25.             TankCapacity = tankCapacity;
  26.         }
  27.         public double FuelQuantity
  28.         {
  29.             get { return this.fuelQuantity; }
  30.             protected set
  31.             {
  32.                 //if (value < 0)
  33.                 //{
  34.                 //    throw new ArgumentException("Invalid fuel");
  35.                 //}
  36.                 //else
  37.                // {
  38.                     this.fuelQuantity = value;
  39.  
  40.                // }
  41.             }
  42.         }
  43.  
  44.         public double FuelConsumption
  45.         {
  46.             get { return this.fuelConsumption; }
  47.             protected set
  48.             {
  49.                 //if (value < 0)
  50.                 //{
  51.                 //    throw new ArgumentException("Invalid fuel");
  52.                 //}
  53.                 this.fuelConsumption = value;
  54.             }
  55.         }
  56.  
  57.         public double TankCapacity
  58.         {
  59.             get { return this.tankCapacity; }
  60.             protected set
  61.             {
  62.                 //if (value < 0)
  63.                 //{
  64.                 //    throw new ArgumentException("Invalid fuel");
  65.                 //}
  66.                 this.tankCapacity = value;
  67.             }
  68.         }
  69.  
  70.         public virtual void Drive(double distance)
  71.         {
  72.             double fuelNedded = distance * FuelConsumption;
  73.  
  74.             if (FuelQuantity< fuelNedded)
  75.             {
  76.                 throw new ArgumentException($"{ this.GetType().Name} needs refueling");
  77.             }
  78.            
  79.                 FuelQuantity -= fuelNedded;
  80.                 Console.WriteLine($"{ this.GetType().Name} travelled {distance} km");
  81.            
  82.         }
  83.         public virtual void ReFuel(double fuel)
  84.         {
  85.             double availableSpace = this.TankCapacity - this.FuelQuantity;
  86.  
  87.             if (fuel <= 0)
  88.             {
  89.                 throw new ArgumentException("Fuel must be a positive number");
  90.             }
  91.             if (availableSpace < fuel)
  92.             {
  93.                 throw new ArgumentException($"Cannot fit {fuel} fuel in the tank");
  94.  
  95.             }        
  96.                 FuelQuantity += fuel;        
  97.         }
  98.  
  99.         public override string ToString()
  100.         {
  101.             return $"{this.GetType().Name}: {this.fuelQuantity:f2}";
  102.         }
  103.     }
  104. }
  105.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement