TheBulgarianWolf

Speed Racing

Jan 16th, 2021
1,144
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 2.75 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3.  
  4. namespace SpeedRacing
  5. {
  6.     class Program
  7.     {
  8.         static void Main(string[] args)
  9.         {
  10.             List<Car> carsList = new List<Car>();
  11.             Console.WriteLine("Enter the number of cars you are about to enter: ");
  12.             int cars = int.Parse(Console.ReadLine());
  13.             for(int i = 0; i < cars; i++)
  14.             {
  15.                 string[] input = Console.ReadLine().Split(" ");
  16.                 string model = input[0];
  17.                 double fuelAmount = double.Parse(input[1]);
  18.                 double fuelConsumptionFor1Km = double.Parse(input[2]);
  19.                 Car car = new Car(model, fuelAmount, fuelConsumptionFor1Km);
  20.                 carsList.Add(car);
  21.             }
  22.             string inputTwo;
  23.             while((inputTwo = Console.ReadLine()) != "End")
  24.             {
  25.                 string[] inputArr = inputTwo.Split(" ");
  26.                 string command = inputArr[0];
  27.                 if(command == "Drive")
  28.                 {
  29.                     string mod = inputArr[1];
  30.                     double distance = double.Parse(inputArr[2]);
  31.                     Car curCar = carsList.Find(c => (c.Model == mod));
  32.                     if(distance*curCar.FuelConsumptionPerKm <= curCar.FuelAmount)
  33.                     {
  34.                         curCar.TravelledDistance += distance;
  35.                         curCar.FuelAmount -= distance * curCar.FuelConsumptionPerKm;
  36.                     }
  37.                     else
  38.                     {
  39.                         Console.WriteLine("Insufficient fuel for the drive!");
  40.                     }
  41.                 }
  42.  
  43.             }
  44.  
  45.             foreach(Car car in carsList)
  46.             {
  47.                 Console.WriteLine("{0} {1:F2} {2}",car.Model,car.FuelAmount,car.TravelledDistance);
  48.             }
  49.  
  50.         }
  51.     }
  52.  
  53.     class Car
  54.     {
  55.         public Car(string model,double fuelAmount,double consumption)
  56.         {
  57.             Model = model;
  58.             FuelAmount = fuelAmount;
  59.             FuelConsumptionPerKm = consumption;
  60.             TravelledDistance = 0;
  61.         }
  62.  
  63.         public string Model { get; set; }
  64.         public double FuelAmount { get; set; }
  65.         public double FuelConsumptionPerKm { get; set; }
  66.         public double TravelledDistance { get; set; }
  67.  
  68.         public bool isTheFuelEnough(double kilometers,double FuelConsumptionPerKm)
  69.         {
  70.             double fuelUsed = kilometers * FuelConsumptionPerKm;
  71.             if(fuelUsed <= FuelAmount)
  72.             {
  73.                 TravelledDistance += kilometers;
  74.                 FuelAmount -= fuelUsed;
  75.                 return true;
  76.             }
  77.             else
  78.             {
  79.                 return false;
  80.             }
  81.         }
  82.  
  83.     }
  84. }
  85.  
Add Comment
Please, Sign In to add comment