Advertisement
dragonbs

Need for Speed III

Mar 27th, 2023
158
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 2.99 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4.  
  5. class Car
  6. {
  7.     public string Model { get; set; }
  8.     public int Mileage { get; set; }
  9.     public int Fuel { get; set; }
  10.  
  11.     public Car(string model, int mileage, int fuel)
  12.     {
  13.         this.Model = model;
  14.         this.Mileage = mileage;
  15.         this.Fuel = fuel;
  16.     }
  17.  
  18.     public bool Drive(int distance, int fuelSpent)
  19.     {
  20.         if (this.Fuel < fuelSpent)
  21.         {
  22.             Console.WriteLine("Not enough fuel to make that ride");
  23.         }
  24.         else
  25.         {
  26.             this.Mileage += distance;
  27.             this.Fuel -= fuelSpent;
  28.             Console.WriteLine($"{this.Model} driven for {distance} kilometers. {fuelSpent} liters of fuel consumed.");
  29.         }
  30.         if (this.Mileage >= 100000)
  31.         {
  32.             Console.WriteLine($"Time to sell the {this.Model}!");
  33.             return true;
  34.         }
  35.         return false;
  36.     }
  37.  
  38.     public void Refuel(int fuelToFill)
  39.     {
  40.         int actualFuel = Math.Min(fuelToFill, 75 - this.Fuel);
  41.         Console.WriteLine($"{this.Model} refueled with {actualFuel} liters");
  42.         this.Fuel = this.Fuel + actualFuel;
  43.     }
  44.  
  45.     public void Revert(int revertKm)
  46.     {
  47.         if (this.Mileage - revertKm >= 10000)
  48.         {
  49.             Console.WriteLine($"{this.Model} mileage decreased by {revertKm} kilometers");
  50.         }
  51.         this.Mileage = Math.Max(this.Mileage - revertKm, 10000);
  52.     }
  53.  
  54.     public override string ToString() =>
  55.         $"{this.Model} -> Mileage: {this.Mileage} kms, Fuel in the tank: {this.Fuel} lt.";
  56. }
  57.  
  58.  
  59. internal class Program
  60. {
  61.     static void Main()
  62.     {
  63.         List<Car> cars = GetAllCarsInfo();
  64.         string input;
  65.         while ((input = Console.ReadLine()) != "Stop")
  66.         {
  67.             string[] tokens = input.Split(" : ");
  68.             string command = tokens[0];
  69.             Car thisCar = cars.Find(x => x.Model == tokens[1]);
  70.             if (command == "Drive")
  71.             {
  72.                 bool needToSellCar = thisCar.Drive(int.Parse(tokens[2]), int.Parse(tokens[3]));
  73.                 if (needToSellCar)
  74.                     cars.Remove(thisCar);
  75.             }
  76.             else if (command == "Refuel")
  77.             {
  78.                 thisCar.Refuel(int.Parse(tokens[2]));
  79.             }
  80.             else if (command == "Revert")
  81.             {
  82.                 thisCar.Revert(int.Parse(tokens[2]));
  83.             }
  84.         }
  85.         Console.WriteLine(String.Join("\n", cars));
  86.     }
  87.  
  88.     private static List<Car> GetAllCarsInfo()
  89.     {
  90.         int numberOfCars = int.Parse(Console.ReadLine());
  91.         List<Car> cars = new List<Car>();
  92.         for (int i = 0; i < numberOfCars; i++)
  93.         {
  94.             string[] carInfo = Console.ReadLine().Split('|');
  95.             string model = carInfo[0];
  96.             int mileage = int.Parse(carInfo[1]);
  97.             int fuel = int.Parse(carInfo[2]);
  98.             cars.Add(new Car(model, mileage, fuel));
  99.         }
  100.         return cars;
  101.     }
  102. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement