Skip to content

Instantly share code, notes, and snippets.

@Tangrila-BG
Created February 2, 2017 16:54
Show Gist options
  • Save Tangrila-BG/c061f13246d3aa37bd2aa40ab355fe3a to your computer and use it in GitHub Desktop.
Save Tangrila-BG/c061f13246d3aa37bd2aa40ab355fe3a to your computer and use it in GitHub Desktop.
Problem 6. *Raw Data
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading;
namespace Exercises
{
class Program
{
static void Main(string[] args)
{
int n = Convert.ToInt32(Console.ReadLine());
for (int i = 0; i < n; i++)
{
Queue<string> input = new Queue<string>(Console.ReadLine().Split());
string model = input.Dequeue();
int engineSpeed = Convert.ToInt32(input.Dequeue());
int enginePower = Convert.ToInt32(input.Dequeue());
int cargoWeight = Convert.ToInt32(input.Dequeue());
string cargoType = input.Dequeue();
Cargo cargo = new Cargo(cargoType, cargoWeight);
Car car = new Car(model, enginePower, engineSpeed, cargo);
int tireAge = -1;
double tirePressure = -1;
/* 0 - store tire pressure
* 1 - store tire age
* 2 - add tire to car
*/
short counter = 0;
while (true)
{
switch (counter)
{
case 0:
tirePressure = Double.Parse(input.Dequeue());
break;
case 1:
tireAge = Convert.ToInt32(input.Dequeue());
break;
case 2:
car.AddTire(new Tire(tireAge, tirePressure));
break;
}
counter++;
counter %= 3;
if (input.Count == 0)
{
car.AddTire(new Tire(tireAge, tirePressure));
break;
}
}
}
string command = Console.ReadLine().ToLower();
Car.Print(Car.Calc(command));
}
class Car
{
private string _model;
private int _engineSpeed;
private int _enginePower;
private Cargo _cargo;
private List<Tire> _tires = new List<Tire>();
private static readonly List<Car> Cars = new List<Car>();
public Car(string model, int enginePower, int engineSpeed, Cargo cargo)
{
this._model = model;
this._enginePower = enginePower;
this._engineSpeed = engineSpeed;
this._cargo = cargo;
}
private Car(int enginePower, int engineSpeed, Cargo cargo, string model, List<Tire> tires)
{
this._model = model;
this._enginePower = enginePower;
this._engineSpeed = engineSpeed;
this._cargo = cargo;
this._tires = tires;
}
public void AddTire(Tire tire)
{
if (_tires.Count < 4)
_tires.Add(tire);
if (_tires.Count == 4)
Cars.Add(new Car(_enginePower, _engineSpeed, _cargo, _model, _tires));
}
public static void Print(IEnumerable<Car> cars)
{
foreach (var car in cars)
{
Console.WriteLine(car._model);
}
}
public static IEnumerable<Car> Calc(string command)
{
IEnumerable<Car> filteredCars = new Car[4];
switch (command)
{
case "fragile":
filteredCars = Cars
.Where(c => c._cargo.CargoType == command)
.Where(c => c._tires.Count(t => t.Pressure < 1) > 0);
break;
case "flamable":
filteredCars = Cars
.Where(c => c._enginePower > 250)
.Where(c => c._cargo.CargoType == command);
break;
}
return filteredCars;
}
}
class Tire
{
private int _age;
private double _pressure;
public Tire(int age, double pressure)
{
_age = age;
_pressure = pressure;
}
public double Pressure
{
get { return _pressure; }
set { _pressure = value; }
}
public int Age
{
get { return _age; }
set { _age = value; }
}
}
class Cargo
{
private string _cargoType;
private int _cargoWeight;
public Cargo(string cargoType, int cargoWeight)
{
_cargoType = cargoType;
_cargoWeight = cargoWeight;
}
public int CargoWeight
{
get { return _cargoWeight; }
set { _cargoWeight = value; }
}
public string CargoType
{
get { return _cargoType; }
set { _cargoType = value; }
}
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment