/// <summary> /// this is a click event for add journey button /// and it has validation for the dates /// </summary> /// <param name="sender"></param> /// <param name="e"></param> private void Button_Click(object sender, RoutedEventArgs e) { // check if the start and end date valid if (isJourneyStartDateValid && isJourneyEndDateValid) { journeys.journeysListChanged = true; // if its a new journey then it will add new journey to a list if (newJourney) { Journey journey = new Journey(); journey.id = Guid.NewGuid(); journey.BookingID = BookingID; journey.vehicleID = vehicleId; journey.JourneyStartAt = DateTime.Parse(JourneyStartAtDate.Text); journey.JourneyEndedAt = DateTime.Parse(JourneyEndedAtDate.Text); journey.StartOdometer = int.Parse(StartOdometerJourneyTextBox.Text); journey.EndOdometer = int.Parse(EndedOdometerJourneyTextBox.Text); journey.JourneyFrom = JourneyFromTextBox.Text; journey.JourneyTo = JourneyToTextBox.Text; CarList.journeys.Add(journey); // add new journey into journey's list on carList window Booking associatedBooking = CarList.bookings.Find(b => b.id == journey.BookingID); // finding associatedBooking that has the same booking Id Vehicle associatedVehicle = CarList.vehicles.Find(v => v.Id == journey.vehicleID); // finding associatedVehicle that has the same vehicleID List <Journey> associatedJourneys = CarList.journeys.FindAll(j => j.BookingID == journey.BookingID); // finding list of associatedJourneys by using journey guid List <Booking> bookings = CarList.bookings.FindAll(b => b.Vehicleid == associatedVehicle.Id); // finding list of bookings via associated vehicle associatedBooking.updateRentPrice(associatedJourneys); // updates rent price whenever it adds journey associatedVehicle.updateTotalRentCost(bookings); ((CarList)this.Owner).updateOdometer(); } else { journeys.JourneyStartAt = DateTime.Parse(JourneyStartAtDate.Text); journeys.JourneyEndedAt = DateTime.Parse(JourneyEndedAtDate.Text); journeys.StartOdometer = int.Parse(StartOdometerJourneyTextBox.Text); journeys.EndOdometer = int.Parse(EndedOdometerJourneyTextBox.Text); journeys.JourneyFrom = JourneyFromTextBox.Text; journeys.JourneyTo = JourneyToTextBox.Text; } // save the new journey into journey list on carList window journeys.SaveJourney(CarList.journeys); // close the addJourney Window Close(); } else // if the start and end date is null so it will show error message { LabelStartAtDate.Content = "You must put proper start booking date!"; LabelEndedAtDate.Content = "You must put proper end booking date!"; } }
/// <summary> /// this is a click event to delete journey /// </summary> /// <param name="sender"></param> /// <param name="e"></param> private void ButtonDeleteJourney_Clicked(object sender, RoutedEventArgs e) { Button deleteJourneyButton = sender as Button; Journey j = deleteJourneyButton.CommandParameter as Journey; deleteJourney(j); Vehicle vehicle = CarList.vehicles.Find(v => v.Id == j.vehicleID); Booking booking = CarList.bookings.Find(b => b.id == BookingID); List <Journey> journeys = CarList.journeys.FindAll(journey => journey.BookingID == BookingID); JourneysListView.ItemsSource = journeys; journeysListChanged = true; booking.updateRentPrice(journeys); vehicle.updateTotalRentCost(CarList.bookings.FindAll(b => b.Vehicleid == vehicle.Id)); j.SaveJourney(CarList.journeys); vehicle.SaveVehicles(CarList.vehicles); vehicleListView.ItemsSource = CarList.vehicles; vehicleListView.Items.Refresh(); }
/// <summary> /// this is a click event to delete selected row of booking /// </summary> /// <param name="sender"></param> /// <param name="e"></param> private void ButtonDeleteBooking_Clicked(object sender, RoutedEventArgs e) { Button deleteBookingButton = sender as Button; Booking detailsForBooking = deleteBookingButton.DataContext as Booking; deleteBooking(detailsForBooking); CollectionViewSource.GetDefaultView(BookingsListView.ItemsSource).Refresh(); bookingListChanged = true; detailsForBooking.SaveBookings(bookings); Vehicle relatedVehicle = CarList.vehicles.Find(v => v.Id == detailsForBooking.Vehicleid); List <Booking> allBookingsWithRelatedVehicle = CarList.bookings != null && CarList.bookings.Count > 0 ? CarList.bookings.FindAll(b => b.Vehicleid == relatedVehicle.Id): null; Journey allJourneysRelatedWithBooking = CarList.journeys != null && CarList.journeys.Count > 0 ?CarList.journeys.Find(j => j.BookingID == detailsForBooking.id): null; if (allJourneysRelatedWithBooking != null) { CarList.journeys.Remove(allJourneysRelatedWithBooking); allJourneysRelatedWithBooking.SaveJourney(CarList.journeys); } relatedVehicle.updateTotalRentCost(allBookingsWithRelatedVehicle); relatedVehicle.SaveVehicles(CarList.vehicles); vehicleListView.ItemsSource = CarList.vehicles; vehicleListView.Items.Refresh(); }