import java.util.ArrayList;
import java.util.List;
import javax.swing.JOptionPane;
public class FlightTest {
static List<Flight> flights = new ArrayList<Flight>();
public static void main(String[] args) {
boolean keepGoing = true;
while(keepGoing == true) {
String[] options = { "enter flight data", "find a flight" };
String choice = (String)JOptionPane.showInputDialog(null,
"What do you want to do?",
"Input", JOptionPane.QUESTION_MESSAGE,
null, options, options[0]);
System.out.println("choice = " + choice);
if(choice == null) { // null -> user canceled/dismissed dialog
keepGoing = false;
break;
}
if(choice.indexOf("enter") != -1) {
receiveData();
} else if(choice.indexOf("find") != -1) {
search();
}
}
System.exit(0);
}
private static void receiveData() {
String name = JOptionPane.showInputDialog("Flight Info Input\n\n" +
"Please enter the name of the Airline of the new record.\n\n");
String fltNumStr = JOptionPane.showInputDialog("Flight Info Input\n\n" +
"Please enter the flight number of the new record.\n\n");
int flightNum = Integer.parseInt(fltNumStr);
String origin = JOptionPane.showInputDialog("Flight Info Input\n\n" +
"Please enter the city of origin for the new record.\n\n");
String dest = JOptionPane.showInputDialog("Flight Info Input\n\n" +
"Please enter the destination city for the new record.\n\n");
flights.add(new Flight(name, origin, dest, flightNum));
}
private static void search() {
//System.out.println("Number of flights = " + flights.size());
String searchType = JOptionPane.showInputDialog("Would you like to " +
"complete a search by flight number (enter s)\n" +
"or a search by city of Destination (enter d)?\n\n");
boolean found = false;
if(searchType.equalsIgnoreCase("s")){
String fltNumStr = JOptionPane.showInputDialog("Please enter the " +
"flight number that you wish to search for:\n\n");
int numSearch = Integer.parseInt(fltNumStr);
for(int j = 0; j < flights.size(); j++) {
Flight flight = flights.get(j);
if(numSearch == flight.getFlightNumber()) {
System.out.println("Found flight with flightNmber " +
numSearch + ":");
System.out.println(flight);
found = true;
break;
}
}
if(!found)
System.out.println("no joy");
}
if(searchType.equalsIgnoreCase("d")){
String destSearch = JOptionPane.showInputDialog("Please enter the " +
"city of destination that you wish to search for:\n\n");
for(int j = 0; j < flights.size(); j++) {
Flight flight = flights.get(j);
if(destSearch.equals(flight.getDestCity())) {
System.out.println("Found flight with destination " +
destSearch + ":");
System.out.println(flight);
found = true;
break;
}
}
if(!found)
System.out.println("no joy");
}
}
}
class Flight {
String airlineName;
String originCity;
String destinationCity;
int flightNumber;
public Flight(String name, String orig, String dest, int fltNum) {
this.airlineName = name;
this.originCity = orig;
this.destinationCity = dest;
this.flightNumber = fltNum;
}
public String getAirlineName() { return airlineName; }
public String getOriginCity() { return originCity; }
public String getDestCity() { return destinationCity; }
public int getFlightNumber() { return flightNumber; }
public String toString() {
return "Flight[airlineName:" + airlineName +
" originCity:" + originCity +
" destinationCity:" + destinationCity +
" flightNumber:" + flightNumber + "]";
}
}