Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System;
- using System.Collections.Generic;
- using System.Linq;
- namespace _02._Need_for_Speed_III
- {
- public class StartUp
- {
- static void Main(string[] args)
- {
- int n = int.Parse(Console.ReadLine());
- List<Car> cars = new List<Car>();
- for (int i = 0; i < n; i++)
- {
- string[] input = Console.ReadLine()
- .Split('|', StringSplitOptions.RemoveEmptyEntries);
- Car car = new Car(input[0], int.Parse(input[1]), int.Parse(input[2]));
- cars.Add(car);
- }
- string[] command = Console.ReadLine()
- .Split(" : ", StringSplitOptions.RemoveEmptyEntries);
- while (command[0] != "Stop")
- {
- string carName = command[1];
- Car currentCar = cars.FirstOrDefault(c => c.Name == carName);
- switch (command[0])
- {
- case "Drive":
- int distance = int.Parse(command[2]);
- int fuelNeeded = int.Parse(command[3]);
- if (currentCar.Fuel < fuelNeeded)
- {
- Console.WriteLine("Not enough fuel to make that ride");
- }
- else
- {
- currentCar.Fuel -= fuelNeeded;
- currentCar.Mileage += distance;
- Console.WriteLine($"{currentCar.Name} driven for {distance} kilometers {fuelNeeded} liters of fuel consumed.");
- }
- if (currentCar.Mileage >= 100000)
- {
- Console.WriteLine($"Time to sell the {currentCar.Name}!");
- int index = cars.IndexOf(currentCar);
- cars.RemoveAt(index);
- }
- break;
- case "Refuel":
- int fuel = int.Parse(command[2]);
- currentCar.Fuel += fuel;
- if (currentCar.Fuel > 75)
- {
- currentCar.Fuel = 75;
- }
- Console.WriteLine($"{currentCar.Name} refueled with {fuel} liters");
- break;
- case "Revert":
- int kilometers = int.Parse(command[2]);
- if (currentCar.Mileage - kilometers < 10000)
- {
- currentCar.Mileage = 10000;
- }
- else
- {
- currentCar.Mileage -= kilometers;
- Console.WriteLine($"{currentCar.Name} mileage decreased by {kilometers} kilometers");
- }
- break;
- }
- command = Console.ReadLine()
- .Split(" : ", StringSplitOptions.RemoveEmptyEntries);
- }
- cars = cars.OrderByDescending(c => c.Mileage).ToList();
- foreach (var car in cars)
- {
- Console.WriteLine(car);
- }
- }
- }
- public class Car
- {
- private string name;
- private int mileage;
- private int fuel;
- public int Fuel
- {
- get { return fuel; }
- set { fuel = value; }
- }
- public int Mileage
- {
- get { return mileage; }
- set { mileage = value; }
- }
- public string Name
- {
- get { return name; }
- set { name = value; }
- }
- public Car(string name, int mileage, int fuel)
- {
- this.Name = name;
- this.Mileage = mileage;
- this.Fuel = fuel;
- }
- public override string ToString()
- {
- string result = $"{Name} -> Mileage: {Mileage} kms, Fuel in the tank: {Fuel} lt.";
- return result;
- }
- }
- }
Add Comment
Please, Sign In to add comment