private void Book_Click(object sender, EventArgs e) { Traveler currentTraveler = null; string fName = firstName.Text; string lName = lastName.Text; /* check whether the name is entered correctly */ if (fName.Length == 0 || lName.Length == 0) { MessageBox.Show("Plase make sure you enter a valid name!"); } else /* if correct, then continue with the booking */ { try { currentTraveler = new Traveler(firstName.Text, lastName.Text); allTravelers.Add(currentTraveler); /* grab the information from the listBoxes */ string depCity = departureCities.SelectedItem.ToString(); string lanCity = landingCities.SelectedItem.ToString(); bool tripFound = false; Flight flightToBeFound = null; // the flight we are looking for foreach (Flight flight in allFlights) { /* check whether the flight exists in the allFlights list*/ // [DEBUG] MessageBox.Show(deptDate.Value.ToShortDateString() + " " + flight.getDepartingTime().ToShortDateString()); if (flight.getDeptartingCity() == depCity && flight.getLandingCity() == lanCity && deptDate.Value.ToShortDateString() == flight.getDepartingTime().ToShortDateString()) { tripFound = true; // flight found flightToBeFound = flight; // assign the flight break; } } if (tripFound == true) { /* file to write */ string textToAppendInFile = ""; textToAppendInFile += flightToBeFound.getFlightID() + " "; textToAppendInFile += currentTraveler.getFullName() + " "; textToAppendInFile += flightToBeFound.getDeptartingCity() + " "; textToAppendInFile += flightToBeFound.getLandingCity(); System.IO.File.WriteAllText(@"bookingInfo.txt", textToAppendInFile); /* success message */ MessageBox.Show("Booking information successfully sent to the file!"); /* add the booking to the allBookings list and Traveler to allPassegners list */ allBookings.Add(new Booking(flightToBeFound, currentTraveler, returnTicket.Checked)); allPassengers.Items.Add(currentTraveler.getFullName()); /* success message */ MessageBox.Show("Successfully booked!"); } else { MessageBox.Show("No flights avaialble for selected cities or dates!"); } } catch (Exception) { MessageBox.Show("Please make sure you select cities!"); } } }
public Booking(Flight f, Traveler t, bool rTicket) { flight = f; traveler = t; returnTicket = rTicket; }