Results 1 to 4 of 4
  1. #1
    adlb1300 is offline Member
    Join Date
    Jul 2007
    Posts
    46
    Rep Power
    0

    Default Help Needed - I'm so lost

    I have a project that is due in a few hours that involves creating a flight class to handle flight data provided by the user. The user is requested to supply airline name, flight number, city of origin and city of destination. I'm compiling the data in ArrayLists. Once the user is done entering data the data is being passed to a new instance of the flight class. I than have to instantiate the variables in the constructor and within the flight class use get and set methods to search either by flight number or city of destination. The program should than use a toString to output a list of all matching flights to the user. I have must of it written but cannot seem to figure out how to pass the data to the flight class, search for each matching item and output the results of the search. Any help would be greatly appreciated.

    Thanks in advance and here is the code that I have so far:

    Java Code:
    import java.util.ArrayList;
    import javax.swing.JOptionPane;
     
    public class FlightGUI {
        
        ArrayList<String> airlineNames = new ArrayList<String>();
        ArrayList<String> flightNums = new ArrayList<String>();
        ArrayList<String> originCities = new ArrayList<String>();
        ArrayList<String> destinationCities = new ArrayList<String>();
        
        
        public static void main(String[] args) {
            
            ArrayList<String> airlineNames = new ArrayList<String>();
            ArrayList<String> flightNums = new ArrayList<String>();
            ArrayList<String> originCities = new ArrayList<String>();
            ArrayList<String> destinationCities = new ArrayList<String>();
            boolean keepGoing = true;
            String inputtedAirlineName = "";
            int flightCount = 0;
            
            while(keepGoing == true){
                inputtedAirlineName = JOptionPane.showInputDialog("Flight Info Input\n\n" +
                        "Please enter the name of the Airline of the new record.\n" +
                        "Please enter the phrase search to begin searching flights\n\n");
                
                String inputtedFlightNum = JOptionPane.showInputDialog("Flight Info Input\n\n" +
                        "Please enter the flight number of the new record.\n\n");
                
                String inputtedOriginCity = JOptionPane.showInputDialog("Flight Info Input\n\n" +
                        "Please enter the city of origin for the new record.\n\n");
                
                String inputtedDestinationCity = JOptionPane.showInputDialog("Flight Info Input\n\n" +
                        "Please enter the destination city for the new record.\n\n");
                
                airlineNames.add(inputtedAirlineName);
                flightNums.add(inputtedFlightNum);
                originCities.add(inputtedOriginCity);
                destinationCities.add(inputtedDestinationCity);
                
                flightCount++;
                
                String askForMore = JOptionPane.showInputDialog("Flight Info Input\n\n" +
                        "Do you wish to enter more flights? (Y/N)\n\n");
                
                if(askForMore.equalsIgnoreCase("n")){
                    keepGoing = false;
                    
                    Flight flight = new Flight(airlineNames, flightNums, originCities, destinationCities, flightCount);
                    
                    String searchType = JOptionPane.showInputDialog("Would you like to complete a search by flight number (enter f)\n" +
                            "or a search by city of Destination (enter d)?\n" +
                            "Enter x to exit program\n\n");
                    
                    if(searchType.equalsIgnoreCase("s")){
                        String flightSearch = JOptionPane.showInputDialog("Please enter the flight number that you wish to search for:\n\n");
                        
                        flight = Flight.searchFlightNums(flightSearch);
                        
                    }
                    
                    if(searchType.equalsIgnoreCase("d")){
                        String destinationSearch = JOptionPane.showInputDialog("Please enter the city of destination that you wish to search for:\n\n");
                    }
                    
                    if(searchType.equalsIgnoreCase("x")){
                        break;
                    }
                    
                    
                }
                
            } // end while
        } // end main
    } // end FlightGUI
    Java Code:
    import java.util.ArrayList;
     
    public class Flight {
        
        ArrayList<String> airlineName = new ArrayList<String>();
        ArrayList<String> flightNum = new ArrayList<String>();
        ArrayList<String> originCity = new ArrayList<String>();
        ArrayList<String> destinationCity = new ArrayList<String>();
        private static int numFlights;
        
        public Flight(ArrayList<String> a, ArrayList<String> fn, ArrayList<String> oc, ArrayList<String> dc, int flightCount){
        
            this.numFlights = flightCount;
            this.airlineName.addAll(a);
            this.flightNum.addAll(fn);
            this.originCity.addAll(oc);
            this.destinationCity.addAll(dc);
            
        } // end constructor
        
        public String searchFlightNums(String flightSearch) {
     
            for(int i = 0; i < flightNum.length; i++){
                
            }
       }
     
    }

  2. #2
    hardwired's Avatar
    hardwired is offline Senior Member
    Join Date
    Jul 2007
    Posts
    1,576
    Rep Power
    8

    Default

    Java Code:
    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 + "]";
        }
    }

  3. #3
    adlb1300 is offline Member
    Join Date
    Jul 2007
    Posts
    46
    Rep Power
    0

    Default

    THanks Hardwired I was nowhere close. I ended up making it harder than it needed to be and than when trying to simplify got lost.I appreciate the help.

  4. #4
    nitroxx is offline Member
    Join Date
    Nov 2007
    Posts
    2
    Rep Power
    0

    Default can someone pls help

    Using methods, re-write the popular song "My Bonnie Lies Over the Ocean". There is more than one efficient way to answer this question. Check the web for the lyrics.

Similar Threads

  1. need help with program im lost
    By lifeturn in forum JCreator
    Replies: 1
    Last Post: 10-28-2008, 07:09 PM
  2. Lost my javadocs
    By orchid in forum Eclipse
    Replies: 3
    Last Post: 04-30-2008, 09:45 PM
  3. A little lost with for loops and making a design
    By LinxuS in forum New To Java
    Replies: 5
    Last Post: 01-22-2008, 09:05 AM
  4. help needed.
    By dirtycash in forum New To Java
    Replies: 3
    Last Post: 12-03-2007, 09:17 PM
  5. Absolutely Lost
    By Lehane_9 in forum New To Java
    Replies: 2
    Last Post: 12-03-2007, 06:25 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •