Advertisement
Guest User

Untitled

a guest
Jun 1st, 2017
136
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 3.09 KB | None | 0 0
  1. using System;
  2. using System.Linq;
  3. using System.Text;
  4.  
  5. public class CarMain
  6. {
  7.     public static void Main()
  8.     {
  9.         var carStats = Console.ReadLine().Split(new char[] { ' ' }, StringSplitOptions.RemoveEmptyEntries)
  10.             .Select(decimal.Parse).ToArray();
  11.  
  12.         var car = new Car(carStats[0], carStats[1], carStats[2]);
  13.         decimal distance = 0M;
  14.         var spendTime = 1.99d;
  15.         var result = new StringBuilder();
  16.  
  17.         var command = Console.ReadLine().Split(new char[] { ' ' }, StringSplitOptions.RemoveEmptyEntries);
  18.  
  19.         while (!command[0].Equals("END"))
  20.         {
  21.             if (command[0].Equals("Travel"))
  22.             {
  23.                 decimal km = decimal.Parse(command[1]);
  24.                 Travel(car, km, ref distance, ref spendTime);
  25.             }
  26.  
  27.             if (command[0].Equals("Distance"))
  28.             {
  29.                 result.AppendLine($"Total distance: {distance:F1} kilometers");
  30.             }
  31.  
  32.             if (command[0].Equals("Time"))
  33.             {
  34.                 TimeSpan timespan = TimeSpan.FromHours(spendTime);
  35.  
  36.                 result.AppendLine($"Total time: {(timespan.Days * 24) + timespan.Hours} hours and {timespan.Minutes} minutes");
  37.             }
  38.  
  39.             if (command[0].Equals("Fuel"))
  40.             {
  41.                 result.AppendLine($"Fuel left: {car.Fuel:F1} liters");
  42.             }
  43.  
  44.             if (command[0].Equals("Refuel"))
  45.             {
  46.                 car.Fuel += decimal.Parse(command[1]);
  47.             }
  48.  
  49.             command = Console.ReadLine().Split(new char[] { ' ' }, StringSplitOptions.RemoveEmptyEntries);
  50.         }
  51.  
  52.         Console.WriteLine(result);
  53.     }
  54.  
  55.     private static void Travel(Car car, decimal currentKm, ref decimal distance, ref double spendTime)
  56.     {
  57.         decimal km = currentKm;
  58.         decimal calculateTravelInfo = (km / car.Speed) * car.FuelEconomy;
  59.         if (calculateTravelInfo <= car.Fuel)
  60.         {
  61.             distance += km;
  62.             car.Fuel -= calculateTravelInfo;
  63.             spendTime += (double)(km / car.Speed);
  64.         }
  65.         else
  66.         {
  67.             while (calculateTravelInfo > car.Fuel && km > 0)
  68.             {
  69.                 km--;
  70.                 calculateTravelInfo = (km / car.Speed) * car.FuelEconomy;
  71.             }
  72.             if (km > 0)
  73.             {
  74.                 distance += km;
  75.                 car.Fuel -= calculateTravelInfo;
  76.                 spendTime += (double)(km / car.Speed);
  77.             }
  78.         }
  79.     }
  80. }
  81.  
  82. public class Car
  83. {
  84.     private decimal speed;
  85.     private decimal fuel;
  86.     private decimal fuelEconomy;
  87.  
  88.     public Car(decimal speed, decimal fuel, decimal fuelEconomy)
  89.     {
  90.         this.speed = speed;
  91.         this.fuel = fuel;
  92.         this.fuelEconomy = fuelEconomy;
  93.     }
  94.  
  95.     public decimal Speed
  96.     {
  97.         get { return speed; }
  98.         set { speed = value; }
  99.     }
  100.  
  101.     public decimal Fuel
  102.     {
  103.         get { return fuel; }
  104.         set { fuel = value; }
  105.     }
  106.  
  107.     public decimal FuelEconomy
  108.     {
  109.         get { return fuelEconomy; }
  110.         set { fuelEconomy = value; }
  111.     }
  112. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement