simeon3000

07. Speed Racing

Feb 15th, 2018
228
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 2.39 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4.  
  5. public class StartUp
  6. {
  7.     private static List<Car> cars;
  8.  
  9.     public static void Main()
  10.     {
  11.         cars = new List<Car>();
  12.  
  13.         int n = int.Parse(Console.ReadLine());
  14.  
  15.         AddCars(n);
  16.  
  17.         DriveCars();
  18.  
  19.         PrintCars();
  20.     }
  21.  
  22.     private static void PrintCars()
  23.     {
  24.         foreach (var car in cars)
  25.         {
  26.             Console.WriteLine($"{car.Model} {car.FuelAmount:f2} {car.DistanceTraveled:f0}");
  27.         }
  28.     }
  29.  
  30.     private static void DriveCars()
  31.     {
  32.         string output;
  33.         while ((output = Console.ReadLine()) != "End")
  34.         {
  35.             string[] args = output.Split();
  36.             string model = args[1];
  37.             double distance = double.Parse(args[2]);
  38.  
  39.             Car car = cars.FirstOrDefault(c => c.Model == model);
  40.             if (!car.CanDrive(distance))
  41.             {
  42.                 Console.WriteLine("Insufficient fuel for the drive");                
  43.             }            
  44.         }
  45.     }
  46.  
  47.     private static void AddCars(int n)
  48.     {
  49.         for (int i = 0; i < n; i++)
  50.         {
  51.             string[] args = Console.ReadLine().Split();
  52.             string model = args[0];
  53.             double fuelAmount = double.Parse(args[1]);
  54.             double fuelConsumption = double.Parse(args[2]);
  55.  
  56.             Car car = new Car(model, fuelAmount, fuelConsumption);
  57.             cars.Add(car);            
  58.         }
  59.     }
  60. }
  61.  
  62. ////////////////////////////
  63.  
  64. public class Car
  65. {    
  66.     public Car(string model, double fuelAmount, double fuelConsumption)
  67.     {
  68.         this.Model = model;
  69.         this.FuelAmount = fuelAmount;
  70.         this.FuelConsumption = fuelConsumption;
  71.         this.DistanceTraveled = 0;
  72.     }    
  73.  
  74.     public string Model { get; set; }
  75.     public double FuelAmount { get; set; }
  76.     public double FuelConsumption { get; set; }
  77.     public double DistanceTraveled { get; private set; }
  78.  
  79.     public bool CanDrive(double distance)
  80.     {
  81.         double fuelNeeded = distance * this.FuelConsumption;
  82.         if (fuelNeeded <= this.FuelAmount)
  83.         {
  84.             DriveCar(distance, fuelNeeded);
  85.             return true;
  86.         }
  87.         return false;
  88.     }
  89.  
  90.     private void DriveCar(double distance, double fuelNeeded)
  91.     {
  92.         this.DistanceTraveled += distance;
  93.         this.FuelAmount -= fuelNeeded;
  94.     }
  95. }
Advertisement
Add Comment
Please, Sign In to add comment