private void orderByDistance(string direction) { try { IVehicleLogic vehicleOperations = Provider.GetInstance.GetVehicleOperations(); var vehicles = this.listBoxVehiclesOrderedByEfficiency.Items.Cast <Tuple <Vehicle, List <Student> > >().ToList(); var orderedVehicles = new List <Tuple <Vehicle, List <Student> > >(); if (direction.Equals("ASC")) { orderedVehicles = vehicles.OrderBy(v => vehicleOperations.CalculateDistanceToCoverByVehicle(v)).ToList(); } else { orderedVehicles = vehicles.OrderByDescending(v => vehicleOperations.CalculateDistanceToCoverByVehicle(v)).ToList(); } this.listBoxVehiclesOrderedByEfficiency.Items.Clear(); for (int index = 0; index < orderedVehicles.Count; index++) { this.listBoxVehiclesOrderedByEfficiency.Items.Add(orderedVehicles[index]); } } catch (CoreException ex) { this.labelError.Text = ex.Message; this.labelError.Visible = true; } catch (Exception ex) { this.labelError.Text = ex.Message; this.labelError.Visible = true; } }
public void CalculateDistanceToCoverByVehicle() { IVehicleLogic vehicleOperations = DummyProvider.GetInstance.GetVehicleOperations(); IStudentLogic studentOperations = DummyProvider.GetInstance.GetStudentOperations(); Vehicle vehicle = new Vehicle("SBA0001", 10, 10); vehicleOperations.AddVehicle(vehicle); Student studentOne = new Student(); studentOne.Document = "1234567-1"; studentOne.Name = "John"; studentOne.Location = new Location(2.00000, 2.000000); studentOne.HavePickUpService = true; studentOne.StudentNumber = 1; Student studentTwo = new Student(); studentTwo.Document = "1234567-2"; studentTwo.Name = "George"; studentTwo.Location = new Location(1.00000, 1.000000); studentTwo.HavePickUpService = true; studentTwo.StudentNumber = 2; Student studentThree = new Student(); studentThree.Document = "1234567-3"; studentThree.Name = "Paul"; studentThree.Location = new Location(3.00000, 3.000000); studentThree.HavePickUpService = true; studentThree.StudentNumber = 3; Student studentFour = new Student(); studentFour.Document = "1234567-4"; studentFour.Name = "Ringo"; studentFour.Location = new Location(20.00000, 20.000000); studentFour.HavePickUpService = true; studentFour.StudentNumber = 4; studentOperations.AddStudent(studentOne); studentOperations.AddStudent(studentTwo); studentOperations.AddStudent(studentThree); studentOperations.AddStudent(studentFour); List <Tuple <Vehicle, List <Student> > > vehiclesWithStudents = vehicleOperations.GetVehiclesOrderedByEfficiencyConsideringStudentsNumber(); var expectedDistance = 0.0; expectedDistance += Utils.Distance(new Location(), studentTwo.Location); expectedDistance += Utils.Distance(studentTwo.Location, studentOne.Location); expectedDistance += Utils.Distance(studentOne.Location, studentThree.Location); expectedDistance += Utils.Distance(studentThree.Location, studentFour.Location); expectedDistance += Utils.Distance(studentFour.Location, new Location()); var distance = vehicleOperations.CalculateDistanceToCoverByVehicle(vehiclesWithStudents[0]); Assert.IsTrue(distance == expectedDistance); }