/// <summary> /// Method to find a list of the newest vehicles /// </summary> /// <returns></returns> public VehicleContainer FindNewestVehicles() { VehicleContainer NewestVehicles = new VehicleContainer(); for (int i = 0; i < this.AllVehicles.Count; i++) { if (NewestVehicles.Count == 0) { NewestVehicles.Add(this.AllVehicles[i]); continue; } Vehicle vehicleToCompare = NewestVehicles[0]; if (this.AllVehicles[i] < vehicleToCompare) { NewestVehicles.Clear(); NewestVehicles.Add(this.AllVehicles[i]); } else if (this.AllVehicles[i].Age == vehicleToCompare.Age) { NewestVehicles.Add(this.AllVehicles[i]); } } return(NewestVehicles); }
/// <summary> /// Method to find vehicles with an expired technical inspection and add them to a list /// </summary> /// <returns>a VehicleContainer that contains all vehicles with their expired TI</returns> public VehicleContainer FindVehiclesWithExpiredTI() { VehicleContainer VehiclesWithExpiredTI = new VehicleContainer(); DateTime Today = DateTime.Today; for (int i = 0; i < this.AllVehicles.Count; i++) { Vehicle vehicle = this.AllVehicles[i]; if (Today.Year > vehicle.TechnicalInspection.Year) { vehicle.TechnicalInspection = Convert.ToDateTime("1111/1/1"); VehiclesWithExpiredTI.Add(vehicle); } else if (vehicle.TechnicalInspection.Year == Today.Year && vehicle.TechnicalInspection.Month - vehicle.TechnicalInspection.Month <= 1) { VehiclesWithExpiredTI.Add(vehicle); } } VehiclesWithExpiredTI.SortWithDelegate((left, right) => { if (left.Producer.CompareTo(right.Producer) > 0) { return(1); } else if (left.Producer.CompareTo(right.Producer) < 0) { return(-1); } // producer names are equal, sort by model next if (left.Model.CompareTo(right.Model) > 0) { return(1); } else if (left.Model.CompareTo(right.Model) < 0) { return(-1); } // model names are equal, sort by ID if (left.LicensePlate.CompareTo(right.LicensePlate) > 0) { return(1); } else if (left.LicensePlate.CompareTo(right.LicensePlate) < 0) { return(-1); } return(0); // vehicles are identical }); //VehiclesWithExpiredTI.Sort(); return(VehiclesWithExpiredTI); }
public VehiclesRegister(VehicleContainer Vehicles) { AllVehicles = new VehicleContainer(); for (int i = 0; i < Vehicles.Count; i++) { Vehicle vehicle = this.AllVehicles[i]; this.AllVehicles.Add(vehicle); } }
/// <summary> /// creates a string-based table of vehicles, /// used in both console output and text output /// </summary> /// <returns>list of strings</returns> private static List <String> CreateVehicleTable(VehicleContainer vehicles) { List <String> output = new List <String>(); // the amount of empty characters given for every value in the table List <int> tableSpacing = new List <int> { 7, 12, 8, 5, 4, 15, 10, 12 }; string topstr = CreateIndexedTableLine(tableSpacing, 8, '┌', '┬', '┐', '─'); string midstr = CreateIndexedTableLine(tableSpacing, 8, '├', '┼', '┤', '─'); string botstr = CreateIndexedTableLine(tableSpacing, 8, '└', '┴', '┘', '─'); output.Add(topstr); output.Add(String.Format( "│{0,-7}│{1,-12}│{2,-8}│{3,-5}│{4,-4}│{5,-15}│{6,-10}│{7,-12}│", "Val. ID", "Gamintojas", "Modelis", "Metai", "Mėn.", "T.A. gal. data", "Kuras", "Vid. sąnaud." )); output.Add(midstr); for (int i = 0; i < vehicles.Count; i++) { Vehicle vehicle = vehicles[i]; output.Add(String.Format( "│{0,-7}│{1,-12}│{2,-8}│{3,-5}│{4,-4}│{5,-15}│{6,-10}│{7,-12}│", vehicle.LicensePlate, vehicle.Producer, vehicle.Model, vehicle.YearOfProduction, vehicle.MonthOfProduction, vehicle.TechnicalInspection.ToShortDateString(), vehicle.Fuel, vehicle.AverageFuelConsumption )); } output.Add(botstr); return(output); }
/// <summary> /// Method to print vehicles to a CSV file /// </summary> /// <param name="Vehicles"></param> /// <param name="fileName"></param> public static void PrintVehiclesToCSV(VehicleContainer Vehicles, string fileName) { string[] lines = new string[Vehicles.Count + 1]; lines[0] = String.Format("{0};{1};{2};{3};{4};{5};{6};{7}", "Valstybinis numeris", "Gamintojas", "Modelis", "Pagaminimo metai", "Pagaminimo mėnuo", "T.A. galiojimo data", "Kuras", "Vid. sąnaudos" ); for (int i = 1; i < Vehicles.Count; i++) { if (Vehicles[i].TechnicalInspection == Convert.ToDateTime("1111/1/1")) { lines[i] = String.Format( "{0};{1};{2};{3};{4};{5};{6};{7:f}", Vehicles[i].LicensePlate, Vehicles[i].Producer, Vehicles[i].Model, Vehicles[i].YearOfProduction, Vehicles[i].MonthOfProduction, "SKUBIAI", Vehicles[i].Fuel, Vehicles[i].AverageFuelConsumption ); } else { lines[i] = String.Format( "{0};{1};{2};{3};{4};{5:yyyy-MM-dd};{6};{7:f}", Vehicles[i].LicensePlate, Vehicles[i].Producer, Vehicles[i].Model, Vehicles[i].YearOfProduction, Vehicles[i].MonthOfProduction, Vehicles[i].TechnicalInspection, Vehicles[i].Fuel, Vehicles[i].AverageFuelConsumption ); } File.WriteAllLines(fileName, lines, Encoding.UTF8); } }
/// <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); }
/// <summary> /// Outputs a list of vehicles to a text file /// </summary> /// <param name="fileName">the name of the text file to which to output</param> /// <param name="vehicles">a list of vehicles</param> public static void PrintVehiclesToText(string fileName, VehicleContainer vehicles) { List <String> output = CreateVehicleTable(vehicles); File.WriteAllLines(fileName, output.ToArray(), Encoding.UTF8); }
/// <summary> /// Outputs a list of vehicles to the console /// </summary> /// <param name="Vehicles">a list of vehicles</param> public static void PrintVehicles(VehicleContainer vehicles) { List <String> output = CreateVehicleTable(vehicles); Console.WriteLine(String.Join("\n", output)); }
public VehiclesRegister() { AllVehicles = new VehicleContainer(); }
/// <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"); } }