/// <summary> /// method that reads the data from the file /// </summary> /// <param name="fileName"> file which contains needed data </param> /// <returns></returns> public static VehiclesRegister ReadVehicles(string fileName) { VehiclesRegister vehicles = new VehiclesRegister(); using (StreamReader reader = new StreamReader(fileName)) { string line = null; string city = reader.ReadLine(); string address = reader.ReadLine(); string phoneNum = reader.ReadLine(); while (null != (line = reader.ReadLine())) { string[] values = line.Split(';'); string licensePlate = values[0]; string producer = values[1]; string model = values[2]; int yearOfProduction = int.Parse(values[3]); int monthOfProduction = int.Parse(values[4]); DateTime technicalInspection = DateTime.Parse(values[5]); string fuel = values[6]; double averageFuelConsumption = double.Parse(values[7]); Vehicle vehicle = new Vehicle(licensePlate, producer, model, yearOfProduction, monthOfProduction, technicalInspection, fuel, averageFuelConsumption, city, address, phoneNum); if (!vehicles.Equals(vehicle)) { vehicles.Add(vehicle); } } } return(vehicles); }
/// <summary> /// Method prints the most common producer(s) /// </summary> /// <param name="allVehicles"></param> public static void PrintingVehiclesByProducers(VehiclesRegister register) { List <string> producers = register.FindProducers(); List <Producer> filteredProducersWithNumberOfCars = register.ListOfStringsToProducerObjects(producers); List <Producer> filteredProducers = register.CountVehiclesByProducers(filteredProducersWithNumberOfCars); int HighestNumber = register.HighestNumber(filteredProducers); Console.WriteLine("Daugiausiai automobilių turi: "); Console.WriteLine("| {0} | {1}| ", "Gamintojas(-ai)", "Automobilių kiekis"); Console.WriteLine(new string('-', 39)); foreach (Producer producer in filteredProducers) //searching producer with the most vehicles { if (producer.NumberOfVehicles == HighestNumber) { Console.WriteLine("| {0,-15} | {1,17} |", producer.ProducerName, producer.NumberOfVehicles); } } }
/// <summary> /// Main method /// </summary> /// <param name="args"> program's arguments </param> static void Main(string[] args) { Console.OutputEncoding = Encoding.UTF8; if (new FileInfo(@"Duomenys5.txt").Length <= 3 && new FileInfo(@"Duomenys6.txt").Length <= 3) { Console.WriteLine("Duomenų failai tušti"); } else { if (new FileInfo(@"Duomenys5.txt").Length <= 3) { Console.WriteLine("Pirmasis failas tuščias"); Console.WriteLine("Antrasis registras"); VehiclesRegister register2 = InOutUtils.ReadVehicles(@"Duomenys6.txt"); register2.PrintToTxt("DuomenysPradiniai2.txt"); register2.PrintVehicles(); Console.WriteLine(); Console.WriteLine("Daugiausiai vieno gamintojo (gamintojų) automobilių:"); InOutUtils.PrintingVehiclesByProducers(register2); Console.WriteLine(); List <Vehicle> NewestVehicles = register2.FindNewestVehicles(); Console.WriteLine("Naujausias(-i) automobilis(-iai):"); InOutUtils.PrintNewestVehicles(NewestVehicles); Console.WriteLine(); List <Vehicle> VehiclesWithExpiredTI = register2.FindVehiclesWithExpiredTI(); if (VehiclesWithExpiredTI.Count == 0) { Console.WriteLine("Automobilių su pasibaigusiu T.A. nėra"); Console.WriteLine(); } else { InOutUtils.PrintVehiclesWithExpiredTIToCSV(VehiclesWithExpiredTI, "Apžiūra2.csv"); } Console.ReadKey(); } if (new FileInfo(@"Duomenys6.txt").Length <= 3) { VehiclesRegister register1 = InOutUtils.ReadVehicles(@"Duomenys5.txt"); register1.PrintToTxt("DuomenysPradiniai1.txt"); register1.PrintVehicles(); Console.WriteLine(); InOutUtils.PrintingVehiclesByProducers(register1); Console.WriteLine(); List <Vehicle> NewestVehicles = register1.FindNewestVehicles(); Console.WriteLine("Naujausias(-i) automobilis(-iai):"); InOutUtils.PrintNewestVehicles(NewestVehicles); Console.WriteLine(); List <Vehicle> VehiclesWithExpiredTI = register1.FindVehiclesWithExpiredTI(); if (VehiclesWithExpiredTI.Count == 0) { Console.WriteLine("Automobilių su pasibaigusiu T.A. nėra"); Console.WriteLine(); } else { InOutUtils.PrintVehiclesWithExpiredTIToCSV(VehiclesWithExpiredTI, "Apžiūra1.csv"); } Console.WriteLine("Antrasis failas tuščias"); } else { VehiclesRegister register1 = InOutUtils.ReadVehicles(@"Duomenys5.txt"); register1.PrintToTxt("DuomenysPradiniai1.txt"); register1.PrintVehicles(); Console.WriteLine(); InOutUtils.PrintingVehiclesByProducers(register1); Console.WriteLine(); List <Vehicle> NewestVehicles = register1.FindNewestVehicles(); Console.WriteLine("Naujausias(-i) automobilis(-iai):"); InOutUtils.PrintNewestVehicles(NewestVehicles); Console.WriteLine(); List <Vehicle> VehiclesWithExpiredTI = register1.FindVehiclesWithExpiredTI(); if (VehiclesWithExpiredTI.Count == 0) { Console.WriteLine("Automobilių su pasibaigusiu T.A. nėra"); Console.WriteLine(); } else { InOutUtils.PrintVehiclesWithExpiredTIToCSV(VehiclesWithExpiredTI, "Apžiūra1.csv"); } Console.WriteLine("Antrasis registras"); VehiclesRegister register2 = InOutUtils.ReadVehicles(@"Duomenys6.txt"); register2.PrintToTxt("DuomenysPradiniai2.txt"); register2.PrintVehicles(); Console.WriteLine(); Console.WriteLine("Daugiausiai vieno gamintojo (gamintojų) automobilių:"); InOutUtils.PrintingVehiclesByProducers(register2); Console.WriteLine(); NewestVehicles = register2.FindNewestVehicles(); Console.WriteLine("Naujausias(-i) automobilis(-iai):"); InOutUtils.PrintNewestVehicles(NewestVehicles); Console.WriteLine(); VehiclesWithExpiredTI = register2.FindVehiclesWithExpiredTI(); if (VehiclesWithExpiredTI.Count == 0) { Console.WriteLine("Automobilių su pasibaigusiu T.A. nėra"); Console.WriteLine(); } else { InOutUtils.PrintVehiclesWithExpiredTIToCSV(VehiclesWithExpiredTI, "Apžiūra2.csv"); } Console.ReadKey(); } } }