Advertisement
svephoto

Need for Speed III [C#](2)

Aug 3rd, 2020
260
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 3.68 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4.  
  5. namespace NeedForSpeed
  6. {
  7.     class Program
  8.     {
  9.         static void Main(string[] args)
  10.         {
  11.             int countOfCars = int.Parse(Console.ReadLine());
  12.  
  13.             var carMileage = new Dictionary<string, int>();
  14.             var carFuel = new Dictionary<string, int>();
  15.  
  16.             for (int i = 0; i < countOfCars; i++)
  17.             {
  18.                 string line = Console.ReadLine();
  19.                 string[] lineSplit = line.Split('|').ToArray();
  20.  
  21.                 string car = lineSplit[0];
  22.                 int mileage = int.Parse(lineSplit[1]);
  23.                 int fuel = int.Parse(lineSplit[2]);
  24.  
  25.                 carMileage[car] = mileage;
  26.                 carFuel[car] = fuel;
  27.             }
  28.  
  29.             while (true)
  30.             {
  31.                 string command = Console.ReadLine();
  32.  
  33.                 if (command == "Stop")
  34.                 {
  35.                     foreach (var car in carMileage.OrderByDescending(x=>x.Value).ThenBy(y=>y.Key))
  36.                     {
  37.                         Console.WriteLine($"{car.Key} -> Mileage: {car.Value} kms, Fuel in the tank: {carFuel[car.Key]} lt.");
  38.                     }
  39.                     return;
  40.                 }
  41.  
  42.                 string[] commandSplit = command.Split(" : ").ToArray();
  43.                 string currentCar = commandSplit[1];
  44.            
  45.                 if (commandSplit[0] == "Drive")
  46.                 {
  47.                     int distance = int.Parse(commandSplit[2]);
  48.                     int needFuel = int.Parse(commandSplit[3]);
  49.  
  50.                     if (needFuel > carFuel[currentCar])
  51.                     {
  52.                         Console.WriteLine($"Not enough fuel to make that ride");
  53.                     }
  54.  
  55.                     else
  56.                     {
  57.                         carMileage[currentCar] += distance;
  58.                         carFuel[currentCar] -= needFuel;
  59.  
  60.                         Console.WriteLine($"{currentCar} driven for {distance} kilometers. {needFuel} liters of fuel consumed.");
  61.  
  62.                         if (carMileage[currentCar] >= 100000)
  63.                         {
  64.                             Console.WriteLine($"Time to sell the {currentCar}!");
  65.                             carMileage.Remove(currentCar);
  66.                             carFuel.Remove(currentCar);
  67.                         }
  68.                     }
  69.                 }
  70.  
  71.                 if (commandSplit[0] == "Refuel")
  72.                 {
  73.                     int fuel = int.Parse(commandSplit[2]);
  74.                     int differentFuel = 75 - carFuel[currentCar];
  75.  
  76.                     carFuel[currentCar] += fuel;
  77.                  
  78.                     if (carFuel[currentCar] > 75)
  79.                     {
  80.                         Console.WriteLine($"{currentCar} refueled with {differentFuel} liters");
  81.                         carFuel[currentCar] = 75;
  82.                     }
  83.                     else
  84.                     {
  85.                         Console.WriteLine($"{currentCar} refueled with {fuel} liters");
  86.                     }
  87.                 }
  88.  
  89.                 if (commandSplit[0] == "Revert")
  90.                 {
  91.                     int kilometers = int.Parse(commandSplit[2]);
  92.                     carMileage[currentCar] -= kilometers;
  93.  
  94.                     if (carMileage[currentCar] < 10000)
  95.                     {
  96.                         carMileage[currentCar] = 10000;
  97.                     }
  98.                     else
  99.                     {
  100.                         Console.WriteLine($"{currentCar} mileage decreased by {kilometers} kilometers");
  101.                     }
  102.                 }                
  103.             }
  104.         }
  105.     }
  106. }
  107.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement