Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System;
- using System.Linq;
- public class CarProblem
- {
- public static void Main()
- {
- int[] carInfo = Console.ReadLine().Split().Select(int.Parse).ToArray();
- int speed = carInfo[0];
- int fuel = carInfo[1];
- int fuelEconomy = carInfo[2];
- var car = new Car(speed, fuel, fuelEconomy);
- string input = Console.ReadLine();
- while (input != "END")
- {
- string[] command = input.Split();
- if (command[0] == "Travel")
- {
- car.Travel(int.Parse(command[1]));
- }
- else if (command[0] == "Refuel")
- {
- car.Refuel(int.Parse(command[1]));
- }
- else if (command[0] == "Distance")
- {
- Console.WriteLine($"Total distance: {car.TotalDistance():f1} kilometers");
- }
- else if (command[0] == "Time")
- {
- Console.WriteLine($"Total time: {car.Hours()} hours and {car.Minutes()} minutes");
- }
- else if (command[0] == "Fuel")
- {
- Console.WriteLine($"Fuel left: {car.RemFuel():f1} liters");
- }
- input = Console.ReadLine();
- }
- }
- public class Car
- {
- public double speed;
- public double fuel;
- public double fuelEconomy;
- public double traveledDistance = 0;
- public int hours = 0;
- public int minutes = 0;
- public Car(double speed, double fuel, double fuelEconomy)
- {
- this.speed = speed;
- this.fuel = fuel;
- this.fuelEconomy = fuelEconomy;
- }
- public void Travel(double distanceToTravel)
- {
- double realDistance = Math.Min((fuel / fuelEconomy) * speed, distanceToTravel);
- traveledDistance += realDistance;
- }
- public void Refuel(double refuelAmount)
- {
- this.fuel += refuelAmount;
- }
- public double TotalDistance()
- {
- return traveledDistance;
- }
- public int Hours()
- {
- hours = (int)(traveledDistance / speed);
- return hours;
- }
- public int Minutes()
- {
- minutes = (int)(this.traveledDistance % speed);
- return minutes;
- }
- public double RemFuel()
- {
- return fuel - fuelEconomy * (traveledDistance / speed);
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement