Nikolcho

Need for speed |||

Apr 10th, 2020
101
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 4.17 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4.  
  5. namespace _02._Need_for_Speed_III
  6. {
  7.     public class StartUp
  8.     {
  9.         static void Main(string[] args)
  10.         {
  11.             int n = int.Parse(Console.ReadLine());
  12.             List<Car> cars = new List<Car>();
  13.  
  14.             for (int i = 0; i < n; i++)
  15.             {
  16.                 string[] input = Console.ReadLine()
  17.                     .Split('|', StringSplitOptions.RemoveEmptyEntries);
  18.  
  19.  
  20.                 Car car = new Car(input[0], int.Parse(input[1]), int.Parse(input[2]));
  21.  
  22.                 cars.Add(car);
  23.             }
  24.  
  25.             string[] command = Console.ReadLine()
  26.                 .Split(" : ", StringSplitOptions.RemoveEmptyEntries);
  27.  
  28.             while (command[0] != "Stop")
  29.             {
  30.                 string carName = command[1];
  31.                 Car currentCar = cars.FirstOrDefault(c => c.Name == carName);
  32.                 switch (command[0])
  33.                 {
  34.                     case "Drive":
  35.                         int distance = int.Parse(command[2]);
  36.                         int fuelNeeded = int.Parse(command[3]);
  37.                         if (currentCar.Fuel < fuelNeeded)
  38.                         {
  39.                             Console.WriteLine("Not enough fuel to make that ride");
  40.                         }
  41.                         else
  42.                         {
  43.                             currentCar.Fuel -= fuelNeeded;
  44.                             currentCar.Mileage += distance;
  45.                             Console.WriteLine($"{currentCar.Name} driven for {distance} kilometers {fuelNeeded} liters of fuel consumed.");
  46.                         }
  47.  
  48.                         if (currentCar.Mileage >= 100000)
  49.                         {
  50.                             Console.WriteLine($"Time to sell the {currentCar.Name}!");
  51.                             int index = cars.IndexOf(currentCar);
  52.                             cars.RemoveAt(index);
  53.                         }
  54.                         break;
  55.                     case "Refuel":
  56.                         int fuel = int.Parse(command[2]);
  57.                         currentCar.Fuel += fuel;
  58.                         if (currentCar.Fuel > 75)
  59.                         {
  60.                             currentCar.Fuel = 75;
  61.                         }
  62.                         Console.WriteLine($"{currentCar.Name} refueled with {fuel} liters");
  63.                         break;
  64.                     case "Revert":
  65.                         int kilometers = int.Parse(command[2]);
  66.                         if (currentCar.Mileage - kilometers < 10000)
  67.                         {
  68.                             currentCar.Mileage = 10000;
  69.                         }
  70.                         else
  71.                         {
  72.                             currentCar.Mileage -= kilometers;
  73.                             Console.WriteLine($"{currentCar.Name} mileage decreased by {kilometers} kilometers");
  74.                         }
  75.                         break;
  76.                 }
  77.  
  78.                 command = Console.ReadLine()
  79.                 .Split(" : ", StringSplitOptions.RemoveEmptyEntries);
  80.             }
  81.  
  82.             cars = cars.OrderByDescending(c => c.Mileage).ToList();
  83.  
  84.             foreach (var car in cars)
  85.             {
  86.                 Console.WriteLine(car);
  87.             }
  88.  
  89.         }
  90.     }
  91.  
  92.     public class Car
  93.     {
  94.         private string name;
  95.         private int mileage;
  96.         private int fuel;
  97.  
  98.         public int Fuel
  99.         {
  100.             get { return fuel; }
  101.             set { fuel = value; }
  102.         }
  103.  
  104.  
  105.         public int Mileage
  106.         {
  107.             get { return mileage; }
  108.             set { mileage = value; }
  109.         }
  110.  
  111.  
  112.         public string Name
  113.         {
  114.             get { return name; }
  115.             set { name = value; }
  116.         }
  117.  
  118.         public Car(string name, int mileage, int fuel)
  119.         {
  120.             this.Name = name;
  121.             this.Mileage = mileage;
  122.             this.Fuel = fuel;
  123.         }
  124.  
  125.         public override string ToString()
  126.         {
  127.             string result = $"{Name} -> Mileage: {Mileage} kms, Fuel in the tank: {Fuel} lt.";
  128.             return result;
  129.         }
  130.     }
  131. }
Add Comment
Please, Sign In to add comment