Advertisement
another90sm

SpeedRacing

Jun 21st, 2016
90
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 1.80 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4.  
  5. class SpeedRacing
  6. {
  7.     static void Main()
  8.     {
  9.         var listOfCars = new List<Car>();
  10.  
  11.         int numberOfCars = int.Parse(Console.ReadLine());
  12.  
  13.         for (int i = 0; i < numberOfCars; i++)
  14.         {
  15.             string[] car = Console.ReadLine().Split();
  16.             Car newCar = new Car(car[0], double.Parse(car[1]), double.Parse(car[2]));
  17.             listOfCars.Add(newCar);
  18.         }
  19.  
  20.         string command;
  21.  
  22.         while ((command = Console.ReadLine()) != "End")
  23.         {
  24.             string[] driveCommandArg = command.Split();
  25.             string carModel = driveCommandArg[1];
  26.             int amountOfKm = int.Parse(driveCommandArg[2]);
  27.             var carToDrive = listOfCars.First(c => c.model == carModel);
  28.             carToDrive.Drive(amountOfKm);
  29.         }
  30.  
  31.         foreach (var car in listOfCars)
  32.         {
  33.             Console.WriteLine("{0} {1:F2} {2}", car.model, car.fuelAmount, car.distanceTraveled);
  34.         }
  35.     }
  36. }
  37.  
  38. public class Car
  39. {
  40.     public string model;
  41.     public double fuelAmount;
  42.     public double fuelConsumptionPerKm;
  43.     public double distanceTraveled;
  44.  
  45.     public Car(string model, double fuelAmount, double fuelConsumptionPerKm)
  46.     {
  47.         this.model = model;
  48.         this.fuelAmount = fuelAmount;
  49.         this.fuelConsumptionPerKm = fuelConsumptionPerKm;
  50.         this.distanceTraveled = 0;
  51.     }
  52.  
  53.     public void Drive(int amountOfKm)
  54.     {
  55.         if (amountOfKm <= this.fuelAmount / this.fuelConsumptionPerKm)
  56.         {
  57.             this.distanceTraveled += amountOfKm;
  58.             this.fuelAmount -= this.fuelConsumptionPerKm * amountOfKm;
  59.         }
  60.         else
  61.         {
  62.             Console.WriteLine("Insufficient fuel for the drive");
  63.         }
  64.     }
  65. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement