Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System;
- using System.Runtime.CompilerServices;
- public class Program
- {
- static void Main()
- {
- string[] carParams = Console.ReadLine().Split(new[] {' '}, StringSplitOptions.RemoveEmptyEntries);
- decimal speed = decimal.Parse(carParams[0]);
- decimal fuel = decimal.Parse(carParams[1]);
- decimal fuelEconomy = decimal.Parse(carParams[2]);
- Car car = new Car(speed, fuel, fuelEconomy);
- while (true)
- {
- string input = Console.ReadLine();
- if (input == "END")
- {
- break;
- }
- string[] inputParams = input.Split(new[] {' '}, StringSplitOptions.RemoveEmptyEntries);
- string command = string.Empty;
- decimal distance = 0;
- if (inputParams.Length == 1)
- {
- command = inputParams[0];
- }
- else
- {
- command = inputParams[0];
- distance = decimal.Parse(inputParams[1]);
- }
- switch (command)
- {
- case "Travel":
- car.Travel(distance);
- break;
- case "Refuel":
- car.Refuel();
- break;
- case "Distance":
- car.Distance();
- break;
- case "Time":
- car.Time();
- break;
- case "Fuel":
- car.Fuel();
- break;
- }
- }
- }
- }
- public class Car
- {
- public decimal speed;
- public decimal fuel;
- public decimal fuelEconomy;
- public decimal distancePassed;
- private readonly decimal tankCapacity;
- public Car(decimal speed, decimal fuel, decimal fuelEconomy)
- {
- this.speed = speed;
- this.fuel = fuel;
- this.fuelEconomy = fuelEconomy;
- this.distancePassed = 0;
- this.tankCapacity = fuel;
- }
- public void Travel(decimal distance)
- {
- decimal fuelPerKM = this.fuelEconomy / 100;
- decimal fuelNeeded = distance * fuelPerKM;
- if (fuelNeeded > this.fuel)
- {
- decimal kilometersPerLiter = 100 / this.fuelEconomy;
- this.distancePassed += this.fuel * kilometersPerLiter;
- this.fuel = 0;
- }
- else
- {
- this.fuel -= fuelNeeded;
- this.distancePassed += distance;
- }
- }
- public void Refuel()
- {
- this.fuel = this.tankCapacity;
- }
- public void Distance()
- {
- Console.WriteLine("Total distance: {0:F1} kilometers", this.distancePassed);
- }
- public void Time()
- {
- decimal time = this.distancePassed / this.speed;
- decimal hours = Math.Floor(time);
- decimal minutes = Math.Floor((time - hours) * 60);
- Console.WriteLine("Total time: {0} hours and {1} minutes", hours, minutes);
- }
- public void Fuel()
- {
- Console.WriteLine("Fuel left: {0:F1} liters", this.fuel);
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement