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 cars) { //Could use a map here? List petrolCars = new List(), dieselCars = new List(); 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 GetValidCars(List cars) { foreach (var car in cars) { if (car.HasValidRegistration()) yield return car; } } private static IEnumerable GetCarsFromCSV(string filename) { foreach(var carString in CSVUtils.ReadCSV(filename)) { var c = new Car(); yield return c.FromCSV(carString); } } } }