Advertisement
warXx_

not ready

Jun 29th, 2016
98
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 1.86 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5. using System.Threading.Tasks;
  6.  
  7. class Car
  8. {
  9.     public int speed;
  10.     public double fuel;
  11.     public double fuelEconomy;
  12.     public double distanceTraveled;
  13.  
  14.     public Car(int speed, double fuel, double fuelEconomy)
  15.     {
  16.         this.speed = speed;
  17.         this.fuel = fuel;
  18.         this.fuelEconomy = fuelEconomy;
  19.         distanceTraveled = 0;
  20.     }
  21.  
  22.     public void CommandTravel(string command, double kilometers)
  23.     {
  24.        
  25.         double fuelPerKm = fuelEconomy / 100;
  26.         double fuelForDistance = fuelPerKm * distanceTraveled;
  27.         if (fuel >= fuelForDistance)
  28.         {
  29.             fuel -= fuelForDistance;
  30.             distanceTraveled += kilometers;
  31.         }
  32.         else
  33.         {
  34.             distanceTraveled = fuelPerKm * fuel;
  35.             fuel = 0;
  36.         }
  37.     }
  38.     public string CommandDistance(string command)
  39.     {
  40.         return $"Total distance: {distanceTraveled:F1} kilometers";
  41.     }
  42. }
  43.  
  44. class Program
  45. {
  46.     static void Main(string[] args)
  47.     {
  48.  
  49.         string input = Console.ReadLine();
  50.         string[] carInfo = input.Split(new[] { ' ' }, StringSplitOptions.RemoveEmptyEntries);
  51.         var car = new Car(int.Parse(carInfo[0]), int.Parse(carInfo[1]), int.Parse(carInfo[2]));
  52.  
  53.         input = Console.ReadLine();
  54.         while (input != "END")
  55.         {
  56.             string[] command = input.Split(' ');
  57.             switch(command[0])
  58.             {
  59.                 case "Travel":
  60.                     car.CommandTravel(command[0], double.Parse(command[1]));
  61.                     break;
  62.                 case "Distance":
  63.                     Console.WriteLine(car.CommandDistance(command[0]));
  64.                     break;
  65.                    
  66.             }
  67.             input = Console.ReadLine();
  68.         }
  69.     }
  70. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement