Teo_Yanchev

C# Fundamentals - 03. SpeedRacing - MoreExercise - ClassesAndObjects

Dec 1st, 2020
134
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 2.61 KB | None | 0 0
  1. using System;
  2. using System.Linq;
  3. using System.Collections.Generic;
  4.  
  5. namespace _03._SpeedRacing
  6. {
  7.     class Program
  8.     {
  9.         static void Main(string[] args)
  10.         {
  11.             List<Car> cars = new List<Car>();
  12.  
  13.             int carsAmount = int.Parse(Console.ReadLine());
  14.             for (int i = 0; i < carsAmount; i++)
  15.             {
  16.                 string[] car = Console.ReadLine().Split(' ',
  17.                     StringSplitOptions.RemoveEmptyEntries);
  18.                 string model = car[0];
  19.                 int fuel = int.Parse(car[1]);
  20.                 double consumption = double.Parse(car[2]);
  21.  
  22.                 Car currentCar = new Car(model, fuel, consumption);
  23.                 cars.Add(currentCar);
  24.             }
  25.  
  26.             string command;
  27.             while ((command = Console.ReadLine()) != "End")
  28.             {
  29.                 string[] currentCommand = command.Split(' ',
  30.                     StringSplitOptions.RemoveEmptyEntries);
  31.                 string drive = currentCommand[0];
  32.                 string model = currentCommand[1];
  33.                 int driveKM = int.Parse(currentCommand[2]);
  34.  
  35.                 if (cars.Any(m => m.Model == model))
  36.                 {
  37.                     var car = cars.FindIndex(m=>m.Model == model);
  38.                     var curCar = cars[car];
  39.  
  40.                     curCar.Distance(curCar, driveKM);
  41.                 }
  42.             }
  43.  
  44.             Console.WriteLine(string.Join(Environment.NewLine, cars.ToString()));
  45.         }
  46.  
  47.         public class Car
  48.         {
  49.             public string Model { get; set; }
  50.             public double Fuel { get; set; }
  51.             public double ConsumptionPerKM { get; set; }
  52.             public int TraveledDistance { get; set; }
  53.  
  54.             public Car(string model, int fuel, double consumption)
  55.             {
  56.                 this.Model = model;
  57.                 this.Fuel = fuel;
  58.                 this.ConsumptionPerKM = consumption;
  59.                 this.TraveledDistance = 0;
  60.             }
  61.  
  62.             public void Distance(Car car, int driveKM)
  63.             {
  64.                 double differenceL = driveKM * car.ConsumptionPerKM;
  65.                 if (car.Fuel > differenceL)
  66.                 {
  67.                     car.Fuel -= differenceL;
  68.                     car.TraveledDistance += driveKM;
  69.                 }
  70.  
  71.                 else
  72.                 {
  73.                     Console.WriteLine($"Insufficient fuel for the drive");
  74.                 }
  75.             }
  76.  
  77.             public override string ToString()
  78.             {
  79.                 return $"{Model} {Fuel} {TraveledDistance}";
  80.             }
  81.         }
  82.     }
  83. }
  84.  
Advertisement
Add Comment
Please, Sign In to add comment