murkata86

Car

Jun 29th, 2016
249
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. using System;
  2. using System.Runtime.CompilerServices;
  3.  
  4. public class Program
  5. {
  6.     static void Main()
  7.     {
  8.         string[] carParams = Console.ReadLine().Split(new[] {' '}, StringSplitOptions.RemoveEmptyEntries);
  9.  
  10.         decimal speed = decimal.Parse(carParams[0]);
  11.         decimal fuel = decimal.Parse(carParams[1]);
  12.         decimal fuelEconomy = decimal.Parse(carParams[2]);
  13.  
  14.         Car car = new Car(speed, fuel, fuelEconomy);
  15.  
  16.         while (true)
  17.         {
  18.             string input = Console.ReadLine();
  19.  
  20.             if (input == "END")
  21.             {
  22.                 break;
  23.             }
  24.  
  25.             string[] inputParams = input.Split(new[] {' '}, StringSplitOptions.RemoveEmptyEntries);
  26.  
  27.             string command = string.Empty;
  28.             decimal distance = 0;
  29.  
  30.             if (inputParams.Length == 1)
  31.             {
  32.                 command = inputParams[0];
  33.             }
  34.             else
  35.             {
  36.                 command = inputParams[0];
  37.                 distance = decimal.Parse(inputParams[1]);
  38.             }
  39.  
  40.             switch (command)
  41.             {
  42.                 case "Travel":
  43.                     car.Travel(distance);
  44.                     break;
  45.                 case "Refuel":
  46.                     car.Refuel();
  47.                     break;
  48.                 case "Distance":
  49.                     car.Distance();
  50.                     break;
  51.                 case "Time":
  52.                     car.Time();
  53.                     break;
  54.                 case "Fuel":
  55.                     car.Fuel();
  56.                     break;
  57.             }
  58.         }
  59.     }
  60. }
  61.  
  62. public class Car
  63. {
  64.     public decimal speed;
  65.     public decimal fuel;
  66.     public decimal fuelEconomy;
  67.  
  68.     public decimal distancePassed;
  69.  
  70.     private readonly decimal tankCapacity;
  71.  
  72.    
  73.     public Car(decimal speed, decimal fuel, decimal fuelEconomy)
  74.     {
  75.         this.speed = speed;
  76.         this.fuel = fuel;
  77.         this.fuelEconomy = fuelEconomy;
  78.         this.distancePassed = 0;
  79.         this.tankCapacity = fuel;
  80.     }
  81.  
  82.     public void Travel(decimal distance)
  83.     {
  84.         decimal fuelPerKM = this.fuelEconomy / 100;
  85.  
  86.         decimal fuelNeeded = distance * fuelPerKM;
  87.  
  88.         if (fuelNeeded > this.fuel)
  89.         {  
  90.             decimal kilometersPerLiter = 100 / this.fuelEconomy;
  91.                    
  92.             this.distancePassed += this.fuel * kilometersPerLiter;
  93.             this.fuel = 0;
  94.         }
  95.         else
  96.         {
  97.             this.fuel -= fuelNeeded;
  98.             this.distancePassed += distance;
  99.         }
  100.     }
  101.  
  102.     public void Refuel()
  103.     {
  104.         this.fuel = this.tankCapacity;
  105.     }
  106.  
  107.     public void Distance()
  108.     {
  109.         Console.WriteLine("Total distance: {0:F1} kilometers", this.distancePassed);
  110.     }
  111.  
  112.     public void Time()
  113.     {
  114.  
  115.         decimal time = this.distancePassed / this.speed;
  116.  
  117.         decimal hours = Math.Floor(time);
  118.         decimal minutes = Math.Floor((time - hours) * 60);
  119.  
  120.         Console.WriteLine("Total time: {0} hours and {1} minutes", hours, minutes);
  121.     }
  122.  
  123.     public void Fuel()
  124.     {
  125.         Console.WriteLine("Fuel left: {0:F1} liters", this.fuel);
  126.     }
  127. }
RAW Paste Data