Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System;
- using System.Collections.Generic;
- using System.Linq;
- class Car
- {
- public string Model { get; set; }
- public int Mileage { get; set; }
- public int Fuel { get; set; }
- public Car(string model, int mileage, int fuel)
- {
- this.Model = model;
- this.Mileage = mileage;
- this.Fuel = fuel;
- }
- public bool Drive(int distance, int fuelSpent)
- {
- if (this.Fuel < fuelSpent)
- {
- Console.WriteLine("Not enough fuel to make that ride");
- }
- else
- {
- this.Mileage += distance;
- this.Fuel -= fuelSpent;
- Console.WriteLine($"{this.Model} driven for {distance} kilometers. {fuelSpent} liters of fuel consumed.");
- }
- if (this.Mileage >= 100000)
- {
- Console.WriteLine($"Time to sell the {this.Model}!");
- return true;
- }
- return false;
- }
- public void Refuel(int fuelToFill)
- {
- int actualFuel = Math.Min(fuelToFill, 75 - this.Fuel);
- Console.WriteLine($"{this.Model} refueled with {actualFuel} liters");
- this.Fuel = this.Fuel + actualFuel;
- }
- public void Revert(int revertKm)
- {
- if (this.Mileage - revertKm >= 10000)
- {
- Console.WriteLine($"{this.Model} mileage decreased by {revertKm} kilometers");
- }
- this.Mileage = Math.Max(this.Mileage - revertKm, 10000);
- }
- public override string ToString() =>
- $"{this.Model} -> Mileage: {this.Mileage} kms, Fuel in the tank: {this.Fuel} lt.";
- }
- internal class Program
- {
- static void Main()
- {
- List<Car> cars = GetAllCarsInfo();
- string input;
- while ((input = Console.ReadLine()) != "Stop")
- {
- string[] tokens = input.Split(" : ");
- string command = tokens[0];
- Car thisCar = cars.Find(x => x.Model == tokens[1]);
- if (command == "Drive")
- {
- bool needToSellCar = thisCar.Drive(int.Parse(tokens[2]), int.Parse(tokens[3]));
- if (needToSellCar)
- cars.Remove(thisCar);
- }
- else if (command == "Refuel")
- {
- thisCar.Refuel(int.Parse(tokens[2]));
- }
- else if (command == "Revert")
- {
- thisCar.Revert(int.Parse(tokens[2]));
- }
- }
- Console.WriteLine(String.Join("\n", cars));
- }
- private static List<Car> GetAllCarsInfo()
- {
- int numberOfCars = int.Parse(Console.ReadLine());
- List<Car> cars = new List<Car>();
- for (int i = 0; i < numberOfCars; i++)
- {
- string[] carInfo = Console.ReadLine().Split('|');
- string model = carInfo[0];
- int mileage = int.Parse(carInfo[1]);
- int fuel = int.Parse(carInfo[2]);
- cars.Add(new Car(model, mileage, fuel));
- }
- return cars;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement