This discussion is archived
6 Replies Latest reply: Feb 25, 2007 11:11 PM by 807606 RSS

java help with vector list

807606 Newbie
Currently Being Moderated
hey i have an assignment in which i have to make a flight reservation now i have my inputs all setup but im having trouble adding the names the user inputs into the vector list i created below i posted my code this is my reservation class where the main is and where user enters input
 
import java.io.*; 
import java.awt.*;
import javax.swing.*;

public class Reservation {
          
     List airlineList = new List();
     
     private void makeReservation(Passenger p) {
          
     }
     
     private void listAllPassengers(Airline a) {
          
     }
     
     private void checkReservation(long confNo) {
          
     }
     
     private void listAllAirlines() {
          for (int i=0; i<airlineList.getItemCount(); i++) {
               System.out.println(airlineList.getItem(i));
          }
     }
     
     private void addAirline(String aAirline, String aCarrier, String aType, int aCapacity, String d, String a) {
          Airline lAirline = new Airline(aAirline);
          
          lAirline.setCarrier(aCarrier);
          lAirline.setArrivalCity(a);
          lAirline.setDepartureCity(d);
          lAirline.setCapacity(aCapacity);
          
          airlineList.add(lAirline.toString());
     }
     

     public static void main(String[] args) {
          System.out.println("");
          System.out.println("1. Reservation");
          System.out.println("2. Cancellation");
          System.out.println("3. List All Airlines");
          System.out.println("4. List Passengers by Flights");
          System.out.println("5. Check Reservation");
          System.out.println("6. Add an Airline");
          System.out.println("7. Quit");
          
          BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
         Passenger p = new Passenger();
         Airline a = new Airline();
        
         try {
                  String str = br.readLine()  ;  
                  int i = Integer.parseInt(str);

                  
                  switch(i){
   
                          case 1:
                           
                          String name = JOptionPane.showInputDialog("Please enter your name");
                          String sexs  = JOptionPane.showInputDialog("Please enter your sex");
                          
                          if (sexs == "male" || sexs == "m" )
                          {
                          
                          boolean     sexChoice = true;
                          }
                          else if (sexs == "female" )
                          {
                                         
                          boolean sexChoice = false;
                          }
                          
                                    
                          
                               
                          String depart = JOptionPane.showInputDialog("Please enter Departure City");
                          String arrive = JOptionPane.showInputDialog("Please enter Arrival City");
                          
                    
                    p.setName(name);
                    p.setSex(sexChoice);
                    p.setDepartureCity(depart);
                    p.setArrivalCity(arrive);
                     
                          break;
                   }  
                }
                        catch (IOException ioe) 
                  { 
       
                   System.out.println("Error with input"); 
                   System.exit(1); 
                }
                
            

     
}}
This is the airline class which defines the airline an adds an airline
import java.util.*;
public class Airline {
     private String airlineCarrier;
     private String airlineType;
     private String airline;
     private int capacity;
     private String departureCity;
     private String arrivalCity;
     private Vector passengerList = null;
     
     public Airline(String aAirline) {
          airline = aAirline;
     }
     
     ///Getter and Setters     
     public void setCarrier(String aCarrier) {
          airlineCarrier = aCarrier;
     }
     public String getCarrier() {
          return airlineCarrier;
     }
     public void setCapacity(int aCapacity) {
          capacity = aCapacity;
     }     
     public String getAirline() {
          return airline;
     }
     public String getArrivalCity() {
          return arrivalCity;
     }
     public String getDepartureCity() {
          return departureCity;
     }
     public void setArrivalCity(String arrivalCity) {
          this.arrivalCity = arrivalCity;
     }
     public void setDepartureCity(String departureCity) {
          this.departureCity = departureCity;
     }
     
     ///Methods
     private String getAirlineType() {
          return airlineType;
     }
     
     private String getPassengerList() {
          String list = "";
          for (int i=0; i<passengerList.size(); i++) {
               list = list + passengerList.get(i); 
          }
          return list;
     }
     
     private int getCapacity() {
          return capacity;
     }     
     
     private long addPassenger(Passenger p) {
          passengerList.add(p);
          return p.getConfNo();
     }
     
     private boolean removePassenger(Passenger p) {
          boolean isRemoved = false;
          if(passengerList.contains(p)) {
               passengerList.remove(p);
               isRemoved = true;
          }
          return isRemoved;
     }
          
     @Override
     public String toString() {
          String lString =  super.toString();
          
          lString = "<"+ getAirline() + ">, <"+ getCarrier() + ">, " +
                    "<"+ getAirlineType() +">, <"+ getCapacity()+ ">, " +
                    "<"+ getDepartureCity() +">, <"+ getArrivalCity()+ ">";
          
          return lString;
     }     
}
and finally my passenger class which defines the passenger and addes them to a list
public class Passenger {

     String passengerName;
     boolean sex;
     String departureCity;
     String arrivalCity;
     long confNo;
     
     // Constructor
     public Passenger() {
          
     }
     
     //Getter/Setter
     public void setName(String aName) {
          passengerName = aName;
     }
     
     public String getName() {
          return passengerName;
     }
     
     public boolean getSex() {
          return sex;
     }
     
     public void setSex(boolean aSex) {
          sex = aSex;
     }
     
     public void setDepartureCity(String aCity) {
          departureCity = aCity;
     }
     
     public String getDepartureCity() {
          return departureCity;
     }
     
     public void setArrivalCity(String aCity) {
          arrivalCity = aCity;
     }
     
     public String getArrivalCity() {
          return arrivalCity;
     }
     
     public void setConfNo(long aNum) {
          confNo = aNum;
     }
     
     public long getConfNo() {
          return confNo;
     }


     // Instance Method
     @Override
     public String toString() {
          String lString =  super.toString();
          
          lString = "<"+ getName() + ">, <"+ getSex() + ">, " +
                    "<"+ getDepartureCity() +">, <"+ getArrivalCity()+ ">";
          
          return lString;
     }
     
     
     
}
i hope it wasnt confusing the best way i can explain it is i want to added what ever the user inputs into a vector array so i can make multiple airlines and passengers and reprint and cancel if someone can lead me to a site which can help me out im so lost or any suggestions on how i can do this would be great thanks
  • 1. Re: java help with vector list
    807606 Newbie
    Currently Being Moderated
    Can you be more precise about exactly what problem you're having?

    Rather than trying to do everything at once, you might find it helpful to work incrementally. Just do enough to get a single step working (say, loading the airlines and listing them). Then add new functionality one step at a time, making sure that everything works correctly before moving on to the next step. Revise your code as necessary as you go.
  • 2. Re: java help with vector list
    807606 Newbie
    Currently Being Moderated
    ook my problem is getting what i had in reservations the methods setname, setsex, set arrival and setdeparture into an array so i can add multiple passengers
  • 3. Re: java help with vector list
    807606 Newbie
    Currently Being Moderated
    In Airline class you have initialised passengerList variable to null and there is not setter method to initialise this private variable. So obviously you will be getting NullPointerException while calling these method getPassengerList(), removePassenger() and addPassenger().
  • 4. Re: java help with vector list
    807606 Newbie
    Currently Being Moderated
    Your Passenger list is an attribute of Airline. So you're going to have to add your passenger object via your Airline objects. You're going to need something that lets you select an airline before you start adding passengers.

    By the way, when you catch an exception, you're doing this:
                 System.out.println("Error with input"); 
    That's not very useful. The exception itself has lots of useful information inside it. You can print that stuff out like this:
    ioe.printStackTrace();
  • 5. Re: java help with vector list
    807606 Newbie
    Currently Being Moderated
    ok i get what ur saying hmmm look at this
     private String getPassengerList() {
              String list = "";
              for (int i=0; i><passengerList.size(); i++) {
                   list = list + passengerList.get(i); 
              }
              return list;
         
     
         private long addPassenger(Passenger p) {
              passengerList.add(p);
              return p.getConfNo();
         }
    so i cut out a piece of my code so thats part of airline class so i gotta refernce that like make a new airline ... airline a = new airline alrite lets say now i want to add the string thats inside setName from my code in my first post how would i go about doing that thanks
  • 6. Re: java help with vector list
    807606 Newbie
    Currently Being Moderated
    It would be a lot easier to help you if you could communicate. Try using complete, distinct sentences.

    Anyway at this point I think you're stumbling because you're trying to jump right to implementation without really knowing the details of the requirements. Think about what your code has to do, what data is available, and how you're going to represent it.

    What are the relationships between passengers, airlines, and flights? Try drawing that out on a piece of paper before you code anything.

    One thing about your code -- it looks like "passenger" and "flight" are mixed together into one class. This doesn't really make sense. It's probably making your program a lot harder than it needs to be. You might want to create a separate Flight class before you do anything else. Then get your program so that it can create, remove, and list airlines and flights. Once that's working, start adding the passenger stuff.