/// <summary> /// the method that reads the data from a file /// </summary> /// <param name="fileName"> input filename </param> /// <returns>a vehicleRegister</returns> public static VehiclesRegister ReadVehicles(string fileName) { string[] lines = new string[150]; VehiclesRegister output = new VehiclesRegister(); // file error handling if (System.IO.File.Exists(fileName)) { lines = File.ReadAllLines(fileName, Encoding.UTF8); } else { Console.WriteLine("Failas nerastas. Programa negali veikti."); System.Environment.Exit(1); // exit code 1 means that the program did not run successfuly } if (lines.Length <= 0) { Console.WriteLine("Pateiktas tuščias failas. Programa negali veikti."); System.Environment.Exit(1); // exit code 1 means that the program did not run successfuly } string city = lines[0]; string address = lines[1]; string phoneNum = lines[2]; for (int i = 3; i < lines.Length; i++) { string line = lines[i]; // basic support for comments // if a line in the input file starts with // '//', then ignore the line and move on if (line.StartsWith("//")) { continue; } string[] values = line.Split(';'); Vehicle vehicleToAdd = new Vehicle( values[0], values[1], values[2], int.Parse(values[3]), int.Parse(values[4]), DateTime.Parse(values[5]), values[6], double.Parse(values[7]), city, address, phoneNum ); output.Add(vehicleToAdd); } return(output); }
/// <summary> /// Method prints the most common producer(s) /// </summary> /// <param name="allVehicles"></param> public static List <String> CreateVehiclesByProducerTable(VehiclesRegister register) { List <int> tableSpacing = new List <int> { 16, 18 }; string topstr = CreateIndexedTableLine(tableSpacing, 2, '┌', '┬', '┐', '─'); string midstr = CreateIndexedTableLine(tableSpacing, 2, '├', '┼', '┤', '─'); string botstr = CreateIndexedTableLine(tableSpacing, 2, '└', '┴', '┘', '─'); List <String> output = new List <String>(); List <string> producers = register.FindProducers(); List <Producer> filteredProducersWithNumberOfCars = register.ListOfStringsToProducerObjects(producers); List <Producer> filteredProducers = register.CountVehiclesByProducers(filteredProducersWithNumberOfCars); int HighestNumber = register.HighestNumber(filteredProducers); output.Add("Daugiausiai automobilių turi: "); output.Add(topstr); output.Add(String.Format( "│{0,-16}│{1,-18}│", "Gamintojas(-ai)", "Automobilių kiekis" )); output.Add(midstr); foreach (Producer producer in filteredProducers) //searching producer with the most vehicles { if (producer.NumberOfVehicles == HighestNumber) { output.Add(String.Format( "│{0,-16}│{1,-18}│", producer.ProducerName, producer.NumberOfVehicles )); } } output.Add(botstr); return(output); }
/// <summary> /// a method to find all matching vehicles from another VehiclesRegister /// </summary> /// <param name="other">a vehicle register to which to compare against</param> /// <returns>a VehicleContainer that contains all matching vehicles</returns> public VehicleContainer FindMatches(VehiclesRegister other) { VehicleContainer matches = new VehicleContainer(); VehicleContainer selfContainer = this.AllVehicles; VehicleContainer otherContainer = other.AllVehicles; for (int i = 0; i < selfContainer.Count; i++) { for (int j = 0; j < otherContainer.Count; j++) { if (selfContainer[i].Equals(otherContainer[j])) { matches.Add(selfContainer[i]); } } } return(matches); }
public static void PrintMatchedVehiclesToCSV(string fileName, VehiclesRegister left, VehiclesRegister right, VehicleContainer matches) { if (matches.Count <= 0) { return; } string[] lines = new string[matches.Count + 2]; lines[0] = left.City; lines[1] = right.City; for (int i = 0; i < matches.Count; i++) { int lineIndex = i + 2; lines[lineIndex] = String.Format( "{0};{1}", matches[i].LicensePlate, matches[i].Model ); } File.WriteAllLines(fileName, lines, Encoding.UTF8); }
public static void PrintVehiclesByProducer(VehiclesRegister register) { List <String> output = CreateVehiclesByProducerTable(register); Console.WriteLine(String.Join("\n", output)); }
/// <summary> /// Main method /// </summary> /// <param name="args"> program's arguments </param> static void Main(string[] args) { Console.OutputEncoding = Encoding.UTF8; // filenames string fileName1 = "Duomenys1.txt"; string fileName2 = "Duomenys2.txt"; // register initializations VehiclesRegister register2 = InOutUtils.ReadVehicles(fileName2); VehiclesRegister register1 = InOutUtils.ReadVehicles(fileName1); // write out initial data to text register1.PrintToTxt("DuomenysPradiniai1.txt"); register2.PrintToTxt("DuomenysPradiniai2.txt"); double reg1AvgAge = register1.GetAverageVehicleAge(); double reg2AvgAge = register2.GetAverageVehicleAge(); // find and print the regsiter with the oldest cars on average if (reg1AvgAge > reg2AvgAge) { Console.WriteLine("Pirmame Filiale (registre) yra senesni automobiliai."); register1.PrintVehicles(); } else if (reg1AvgAge == reg2AvgAge) { Console.WriteLine("Abu filialai (registrai) turi lygiai tokio pačio senumo automobilius."); } else { Console.WriteLine("Antrame Filiale (registre) yra senesni automobiliai."); register2.PrintVehicles(); } Console.Write("\n"); // add some space to distinguish the tasks better // find and write to file the matching vehicles in both registers VehicleContainer matches = register1.FindMatches(register2); InOutUtils.PrintMatchedVehiclesToCSV("Klaidos.csv", register1, register2, matches); Console.WriteLine("Pirmas registras:"); // print out the first register's initial data register1.PrintVehicles(); // print out the first register's newest vehicle(s) VehicleContainer NewestVehicles = register1.FindNewestVehicles(); Console.WriteLine("Naujausias(-i) automobilis(-iai):"); InOutUtils.PrintVehicles(NewestVehicles); Console.WriteLine(); // print out the first register's vehicles that have an expired technical inspection status VehicleContainer VehiclesWithExpiredTI = register1.FindVehiclesWithExpiredTI(); if (VehiclesWithExpiredTI.Count == 0) { Console.WriteLine("Automobilių su pasibaigusiu T.A. nėra"); Console.WriteLine(); } else { InOutUtils.PrintVehiclesToCSV(VehiclesWithExpiredTI, "Apžiūra1.csv"); } Console.WriteLine("Antras registras:"); // print out the second register's initial data register2.PrintVehicles(); // print out the second register's newest vehicle(s) VehicleContainer NewestVehicles2 = register2.FindNewestVehicles(); Console.WriteLine("Naujausias(-i) automobilis(-iai):"); InOutUtils.PrintVehicles(NewestVehicles2); Console.WriteLine(); // print out the second register's vehicles that have an expired technical inspection status VehicleContainer VehiclesWithExpiredTI2 = register2.FindVehiclesWithExpiredTI(); if (VehiclesWithExpiredTI2.Count == 0) { Console.WriteLine("Automobilių su pasibaigusiu T.A. nėra"); Console.WriteLine(); } else { InOutUtils.PrintVehiclesToCSV(VehiclesWithExpiredTI2, "Apžiūra2.csv"); } }