Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System;
- using System.Collections.Generic;
- using System.Text;
- namespace Vehicles.Models
- {
- public abstract class Vehicle
- {
- protected double fuelQuantity;
- protected double fuelConsumption;
- protected double tankCapacity;
- public Vehicle(double fuelQuantity, double fuelConsum, double tankCapacity)
- {
- if (fuelQuantity > tankCapacity)
- {
- FuelQuantity = 0;
- }
- else
- {
- FuelQuantity = fuelQuantity;
- }
- FuelConsumption = fuelConsum;
- TankCapacity = tankCapacity;
- }
- public double FuelQuantity
- {
- get { return this.fuelQuantity; }
- protected set
- {
- //if (value < 0)
- //{
- // throw new ArgumentException("Invalid fuel");
- //}
- //else
- // {
- this.fuelQuantity = value;
- // }
- }
- }
- public double FuelConsumption
- {
- get { return this.fuelConsumption; }
- protected set
- {
- //if (value < 0)
- //{
- // throw new ArgumentException("Invalid fuel");
- //}
- this.fuelConsumption = value;
- }
- }
- public double TankCapacity
- {
- get { return this.tankCapacity; }
- protected set
- {
- //if (value < 0)
- //{
- // throw new ArgumentException("Invalid fuel");
- //}
- this.tankCapacity = value;
- }
- }
- public virtual void Drive(double distance)
- {
- double fuelNedded = distance * FuelConsumption;
- if (FuelQuantity< fuelNedded)
- {
- throw new ArgumentException($"{ this.GetType().Name} needs refueling");
- }
- FuelQuantity -= fuelNedded;
- Console.WriteLine($"{ this.GetType().Name} travelled {distance} km");
- }
- public virtual void ReFuel(double fuel)
- {
- double availableSpace = this.TankCapacity - this.FuelQuantity;
- if (fuel <= 0)
- {
- throw new ArgumentException("Fuel must be a positive number");
- }
- if (availableSpace < fuel)
- {
- throw new ArgumentException($"Cannot fit {fuel} fuel in the tank");
- }
- FuelQuantity += fuel;
- }
- public override string ToString()
- {
- return $"{this.GetType().Name}: {this.fuelQuantity:f2}";
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement