Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System;
- using System.Collections.Generic;
- using System.Linq;
- public class StartUp
- {
- private static List<Car> cars;
- public static void Main()
- {
- cars = new List<Car>();
- int n = int.Parse(Console.ReadLine());
- AddCars(n);
- DriveCars();
- PrintCars();
- }
- private static void PrintCars()
- {
- foreach (var car in cars)
- {
- Console.WriteLine($"{car.Model} {car.FuelAmount:f2} {car.DistanceTraveled:f0}");
- }
- }
- private static void DriveCars()
- {
- string output;
- while ((output = Console.ReadLine()) != "End")
- {
- string[] args = output.Split();
- string model = args[1];
- double distance = double.Parse(args[2]);
- Car car = cars.FirstOrDefault(c => c.Model == model);
- if (!car.CanDrive(distance))
- {
- Console.WriteLine("Insufficient fuel for the drive");
- }
- }
- }
- private static void AddCars(int n)
- {
- for (int i = 0; i < n; i++)
- {
- string[] args = Console.ReadLine().Split();
- string model = args[0];
- double fuelAmount = double.Parse(args[1]);
- double fuelConsumption = double.Parse(args[2]);
- Car car = new Car(model, fuelAmount, fuelConsumption);
- cars.Add(car);
- }
- }
- }
- ////////////////////////////
- public class Car
- {
- public Car(string model, double fuelAmount, double fuelConsumption)
- {
- this.Model = model;
- this.FuelAmount = fuelAmount;
- this.FuelConsumption = fuelConsumption;
- this.DistanceTraveled = 0;
- }
- public string Model { get; set; }
- public double FuelAmount { get; set; }
- public double FuelConsumption { get; set; }
- public double DistanceTraveled { get; private set; }
- public bool CanDrive(double distance)
- {
- double fuelNeeded = distance * this.FuelConsumption;
- if (fuelNeeded <= this.FuelAmount)
- {
- DriveCar(distance, fuelNeeded);
- return true;
- }
- return false;
- }
- private void DriveCar(double distance, double fuelNeeded)
- {
- this.DistanceTraveled += distance;
- this.FuelAmount -= fuelNeeded;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment