Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System;
- using System.Collections.Generic;
- using System.Linq;
- class SpeedRacing
- {
- static void Main()
- {
- var listOfCars = new List<Car>();
- int numberOfCars = int.Parse(Console.ReadLine());
- for (int i = 0; i < numberOfCars; i++)
- {
- string[] car = Console.ReadLine().Split();
- Car newCar = new Car(car[0], double.Parse(car[1]), double.Parse(car[2]));
- listOfCars.Add(newCar);
- }
- string command;
- while ((command = Console.ReadLine()) != "End")
- {
- string[] driveCommandArg = command.Split();
- string carModel = driveCommandArg[1];
- int amountOfKm = int.Parse(driveCommandArg[2]);
- var carToDrive = listOfCars.First(c => c.model == carModel);
- carToDrive.Drive(amountOfKm);
- }
- foreach (var car in listOfCars)
- {
- Console.WriteLine("{0} {1:F2} {2}", car.model, car.fuelAmount, car.distanceTraveled);
- }
- }
- }
- public class Car
- {
- public string model;
- public double fuelAmount;
- public double fuelConsumptionPerKm;
- public double distanceTraveled;
- public Car(string model, double fuelAmount, double fuelConsumptionPerKm)
- {
- this.model = model;
- this.fuelAmount = fuelAmount;
- this.fuelConsumptionPerKm = fuelConsumptionPerKm;
- this.distanceTraveled = 0;
- }
- public void Drive(int amountOfKm)
- {
- if (amountOfKm <= this.fuelAmount / this.fuelConsumptionPerKm)
- {
- this.distanceTraveled += amountOfKm;
- this.fuelAmount -= this.fuelConsumptionPerKm * amountOfKm;
- }
- else
- {
- Console.WriteLine("Insufficient fuel for the drive");
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement