/// <summary> /// Writes program's execution results to a file /// </summary> /// <param name="fileName">file to write to</param> /// <param name="list">list containing sorted car list that are aged between minAge and maxAge</param> /// <param name="minAge">minimum age of the car</param> /// <param name="maxAge">maximum age of the car</param> /// <param name="mostDrivenModel">car model(-s) that are driven the most per year</param> public static void WriteResultsToFile(string fileName, CarList list, int minAge, int maxAge, string mostDrivenModel) { using (StreamWriter writer = new StreamWriter(fileName)) { if (mostDrivenModel == null) { writer.WriteLine("Duomenų failas tuščias, todėl rezultatų nėra!"); return; } if (list.Count > 0) { writer.WriteLine(string.Format("Mašinos, kurių amžius yra tarp {0} ir {1} metų:", minAge, maxAge)); writer.WriteLine(string.Format("{0, -20} {1, -10} {2, -10} {3, -10} {4, -30}", "Vairuotojas", "Markė", "Numeris", "Metai", "Rida (tūkst. km per metus)")); while (list.MoveToNextElement()) { writer.WriteLine(list.GetCurrentElement().ToString()); } } else { writer.WriteLine(string.Format("Nėra mašinų, kurių amžius yra tarp {0} ir {1} metų:", minAge, maxAge)); } writer.Write("Labiausiai eksploatuojamo(-ų) automobilio(-ių) markė(-ės): " + mostDrivenModel); } }
/// <summary> /// Writes initial car data (before any operations are performed on it) to a file /// </summary> /// <param name="fileName">file to write to</param> /// <param name="list">list containing car data to write</param> public static void WriteInitialCarDataToFile(string fileName, CarList list) { using (StreamWriter writer = new StreamWriter(fileName)) { writer.WriteLine(string.Format("{0, -20} {1, -10} {2, -10} {3, -10} {4, -30}", "Vairuotojas", "Markė", "Numeris", "Metai", "Rida (tūkst. km per metus)")); while (list.MoveToNextElement()) { writer.WriteLine(list.GetCurrentElement().ToString()); } } }
/// <summary> /// Inserts a new row to a given table which contains car information /// </summary> /// <param name="table">table to insert into</param> /// <param name="cars">list of cars that will be inserted to the table</param> private void AddCarsToTable(Table table, CarList cars) { while (cars.MoveToNextElement()) { CarData data = cars.GetCurrentElement(); TableRow row = new TableRow(); row.Cells.Add(CreateTableCell(data.DriverName)); row.Cells.Add(CreateTableCell(data.Model)); row.Cells.Add(CreateTableCell(data.Number)); row.Cells.Add(CreateTableCell(data.ProductionYear.ToString())); row.Cells.Add(CreateTableCell(data.KmPerYear.ToString())); table.Rows.Add(row); } }
/// <summary> /// Loops through a list and stores all cars that are aged between /// minAge and maxAge in a new list /// </summary> /// <param name="cars">list of cars to pick from</param> /// <param name="minAge">minimum age of the car</param> /// <param name="maxAge">maximum age of the car</param> /// <returns>new list of cars aged between minAge and maxAge</returns> private CarList GetCarsAgedBetween(CarList cars, int minAge, int maxAge) { CarList newCars = new CarList(); while (cars.MoveToNextElement()) { int carAge = cars.GetCurrentElement().GetAge(); if (carAge >= minAge && carAge <= maxAge) { newCars.AddCar(cars.GetCurrentElement()); } } newCars.SortList(); return(newCars); }
/// <summary> /// Finds the most driven car model (all models if there are more which are equaly driven) /// </summary> /// <param name="cars">list containing cars which will be searched</param> /// <returns>string containing the most driven model (or models)</returns> private string GetMostDrivenCarModel(CarList cars) { string model = null; int mostKmDriven = 0; while (cars.MoveToNextElement()) { CarData currentCar = cars.GetCurrentElement(); if (currentCar.KmPerYear > mostKmDriven) { mostKmDriven = currentCar.KmPerYear; model = currentCar.Model; } else if (currentCar.KmPerYear == mostKmDriven) { mostKmDriven = currentCar.KmPerYear; model += ", " + currentCar.Model; } } return(model); }