Advertisement
svephoto

Need for Speed III [C#]

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