krasizorbov

CarExtension

Jun 4th, 2019
184
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 1.81 KB | None | 0 0
  1. using System;
  2. using System.Text;
  3.  
  4. namespace CarManufacturer
  5. {
  6.     public class StartUp
  7.     {
  8.         static void Main(string[] args)
  9.         {
  10.             Car car = new Car();
  11.  
  12.             car.Make = "VW";
  13.             car.Model = "MK3";
  14.             car.Year = 1992;
  15.             car.FuelQuantity = 200;
  16.             car.FuelConsumption = 15;
  17.  
  18.             car.Drive(200);
  19.             Console.WriteLine(car.WhoAmI());
  20.         }
  21.     }
  22.  
  23.  
  24.     public class Car
  25.     {
  26.         string make;
  27.         string model;
  28.         int year;
  29.         double fuelQuantity;
  30.         double fuelConsumption;
  31.  
  32.         public string Make { get { return make; } set { make = value; } }
  33.  
  34.         public string Model { get { return model; } set { model = value; } }
  35.  
  36.         public int Year { get { return year; } set { year = value; } }
  37.  
  38.         public double FuelQuantity { get { return fuelQuantity; } set { fuelQuantity = value; } }
  39.  
  40.         public double FuelConsumption { get { return fuelConsumption; } set { fuelConsumption = value; } }
  41.  
  42.         public void Drive(double distance)
  43.         {
  44.             double currenFuelQuantity = distance * FuelConsumption / 100;
  45.  
  46.             if (FuelQuantity - currenFuelQuantity >= 0)
  47.             {
  48.                 FuelQuantity -= currenFuelQuantity;
  49.             }
  50.             else
  51.             {
  52.                 Console.WriteLine("Not enough fuel to perform this trip!");
  53.             }
  54.         }
  55.  
  56.         public string WhoAmI()
  57.         {
  58.             var carInfo = new StringBuilder();
  59.  
  60.             carInfo.AppendLine($"Make: {this.Make}");
  61.  
  62.             carInfo.AppendLine($"Model: {this.Model}");
  63.  
  64.             carInfo.AppendLine($"Year: {this.Year}");
  65.  
  66.             carInfo.Append($"Fuel: {this.FuelQuantity:F2}L");
  67.  
  68.             return carInfo.ToString();
  69.         }
  70.     }
  71. }
Advertisement
Add Comment
Please, Sign In to add comment