63 lines
1.8 KiB
C#
63 lines
1.8 KiB
C#
using System;
|
|
using System.Collections.Generic;
|
|
using System.Linq;
|
|
|
|
namespace Data_Import
|
|
{
|
|
class Program
|
|
{
|
|
static void Main(string[] args)
|
|
{
|
|
var carContext = GetCarsFromCSV("data.csv").ToList();
|
|
|
|
ExportCSVsByFuelType(carContext);
|
|
|
|
var validCars = GetValidCars(carContext);
|
|
Console.WriteLine("Valid Cars:\n");
|
|
foreach(var c in validCars)
|
|
{
|
|
Console.WriteLine(c.AsCSV());
|
|
}
|
|
Console.WriteLine();
|
|
|
|
var invalidCarCount = carContext.Count - validCars.Count();
|
|
Console.WriteLine(String.Format("Invalid Car Count:\n{0}", invalidCarCount));
|
|
}
|
|
|
|
private static void ExportCSVsByFuelType(List<Car> cars)
|
|
{
|
|
//Could use a map here?
|
|
List<string> petrolCars = new List<string>(), dieselCars = new List<string>();
|
|
|
|
foreach (var c in cars)
|
|
{
|
|
if (c.FuelType == "Petrol")
|
|
petrolCars.Add(c.AsCSV());
|
|
else if (c.FuelType == "Diesel")
|
|
dieselCars.Add(c.AsCSV());
|
|
}
|
|
|
|
CSVUtils.WriteCSV(petrolCars.ToArray(), "petrol.csv");
|
|
CSVUtils.WriteCSV(dieselCars.ToArray(), "diesel.csv");
|
|
}
|
|
|
|
private static IEnumerable<Car> GetValidCars(List<Car> cars)
|
|
{
|
|
foreach (var car in cars)
|
|
{
|
|
if (car.HasValidRegistration())
|
|
yield return car;
|
|
}
|
|
}
|
|
|
|
private static IEnumerable<Car> GetCarsFromCSV(string filename)
|
|
{
|
|
foreach(var carString in CSVUtils.ReadCSV(filename))
|
|
{
|
|
var c = new Car();
|
|
yield return c.FromCSV(carString);
|
|
}
|
|
}
|
|
}
|
|
}
|