Advertisement
Guest User

Exam Retake

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