Skip to content

Instantly share code, notes, and snippets.

@Tangrila-BG
Created February 2, 2017 13:54
Show Gist options
  • Save Tangrila-BG/e7da1f8da8fa8d69ff2ee7e12a3fe7c5 to your computer and use it in GitHub Desktop.
Save Tangrila-BG/e7da1f8da8fa8d69ff2ee7e12a3fe7c5 to your computer and use it in GitHub Desktop.
Problem 5. *Speed Racing
using System;
using System.Collections.Generic;
using System.Linq;
namespace Exercises
{
class Program
{
static void Main(string[] args)
{
int n = int.Parse(Console.ReadLine());
for (int i = 0; i < n; i++)
{
string[] input = Console.ReadLine().Split();
string model = input[0];
double fuel = double.Parse(input[1]);
double fuelCost = double.Parse(input[2]);
new Car(model, fuel, fuelCost);
}
while (true)
{
Queue<string> input = new Queue<string>(Console.ReadLine().Split());
string activity = input.Dequeue();
if (activity.ToLower() == "end")
break;
Car.Activity(activity, input);
}
Car.PrintCars();
}
class Car
{
private static readonly List<Car> Cars = new List<Car>();
private string _model;
private double _fuel;
private double _fuelCostPerKm;
private double _distanceTraveled = 0;
public Car(string model, double fuel, double fuelCostPerKm)
{
this._model = model;
this._fuel = fuel;
this._fuelCostPerKm = fuelCostPerKm;
Cars.Add(new Car(fuel, fuelCostPerKm, model));
}
private Car(double fuel, double fuelCostPerKm, string model)
{
this._model = model;
this._fuel = fuel;
this._fuelCostPerKm = fuelCostPerKm;
}
private static void Drive(Car car, int distance)
{
var fuelForDrive = distance * car._fuelCostPerKm;
if (fuelForDrive > car._fuel)
Console.WriteLine("Insufficient fuel for the drive");
else
{
car._fuel -= fuelForDrive;
car._distanceTraveled += distance;
}
}
private static Car FindCar(string model)
{
return Cars.First(c => c._model == model);
}
public static void Activity(string activity, Queue<string> data)
{
activity = activity.ToLower();
switch (activity)
{
case "drive":
string model = data.Dequeue();
int distance = Convert.ToInt32(data.Dequeue());
Car car = FindCar(model);
Drive(car, distance);
break;
}
}
public static void PrintCars()
{
Cars.ForEach(c =>
Console.WriteLine( string.Join(" ", c.ToString()) )
);
}
public override string ToString()
{
return $"{_model} {_fuel:F2} {_distanceTraveled}";
}
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment