Serialization and Airline Add Flight

I have a school project where I have to write the code for adding/removing a flight from a file using serialization and so far I've been able to add the flight to the file, but I've been unable to read it back and then delete it from the serialization file. If anyone could help me or give me some pointers that would be great. Here is my code so far.

GUI

publicclass GUIextends JFrame{

privatestaticfinallong serialVersionUID = 1;

private JPanel addPanel =null;

private JTextField number =null;

private JLabel FlightNumber =null;

private JLabel Destination =null;

private JTextField destination =null;

private JPanel First =null;

private JButton AddFlight =null;

private JButton RemoveFlight =null;

private JLabel departTime =null;

private JLabel arrivalTime =null;

private JTextField dTime =null;

private JTextField aTime =null;

private JPanel RemovePanel =null;

private JLabel FlightNumber1 =null;

private JTextField removeflightnumber =null;

private JButton submitadd =null;

private JButton submitremove =null;

private JLabel RemoveDestination =null;

private JTextField removedestination =null;

private JLabel removedepartTime =null;

private JTextField removedTime =null;

private JLabel removearrivalTime =null;

private JTextField removeATime =null;

/**

* This is the default constructor

*/

public GUI(){

super();

initialize();

}

/**

* This method initializes this

*

* @return void

*/

privatevoid initialize(){

this.setSize(575, 368);

this.setResizable(false);

this.setName("Flight Management");

this.setContentPane(getFirst());

this.setTitle("Flight Management");

}

/**

* This method initializes addPanel

*

* @return javax.swing.JPanel

*/

private JPanel getAddPanel(){

if (addPanel ==null){

arrivalTime =new JLabel();

arrivalTime.setBounds(new Rectangle(0, 150, 87, 16));

arrivalTime.setText("Arrival Time");

departTime =new JLabel();

departTime.setBounds(new Rectangle(0, 105, 94, 16));

departTime.setText("Departing Time");

Destination =new JLabel();

Destination.setBounds(new Rectangle(0, 60, 83, 16));

Destination.setText("Destination");

FlightNumber =new JLabel();

FlightNumber.setBounds(new Rectangle(0, 15, 84, 16));

FlightNumber.setText("Flight Number");

addPanel =new JPanel();

addPanel.setLayout(null);

addPanel.setBounds(new Rectangle(15, 90, 256, 226));

addPanel.setVisible(true);

addPanel.add(getNumber(),null);

addPanel.add(FlightNumber,null);

addPanel.add(Destination,null);

addPanel.add(getDestination(),null);

addPanel.add(departTime,null);

addPanel.add(arrivalTime,null);

addPanel.add(getDTime(),null);

addPanel.add(getATime(),null);

addPanel.add(getSubmitadd(),null);

}

return addPanel;

}

/**

* This method initializes number

*

* @return javax.swing.JTextField

*/

private JTextField getNumber(){

if (number ==null){

number =new JTextField();

number.setBounds(new Rectangle(120, 15, 105, 16));

number.setText("");

}

return number;

}

/**

* This method initializes destination

*

* @return javax.swing.JTextField

*/

private JTextField getDestination(){

if (destination ==null){

destination =new JTextField();

destination.setBounds(new Rectangle(105, 60, 130, 16));

}

return destination;

}

/**

* This method initializes First

*

* @return javax.swing.JPanel

*/

private JPanel getFirst(){

if (First ==null){

First =new JPanel();

First.setLayout(null);

First.add(getAddFlight(),null);

First.add(getRemoveFlight(),null);

First.add(getAddPanel(),null);

First.add(getRemovePanel(),null);

}

return First;

}

/**

* This method initializes AddFlight

*

* @return javax.swing.JButton

*/

private JButton getAddFlight(){

if (AddFlight ==null){

AddFlight =new JButton();

AddFlight.setBounds(new Rectangle(84, 16, 126, 45));

AddFlight.setText("Add Flight");

AddFlight.addActionListener(new java.awt.event.ActionListener(){

publicvoid actionPerformed(java.awt.event.ActionEvent e){

System.out.println("actionPerformed()");// TODO Auto-generated Event stub actionPerformed()

addPanel.setVisible(true);

RemovePanel.setVisible(false);

}

});

}

return AddFlight;

}

/**

* This method initializes RemoveFlight

*

* @return javax.swing.JButton

*/

private JButton getRemoveFlight(){

if (RemoveFlight ==null){

RemoveFlight =new JButton();

RemoveFlight.setBounds(new Rectangle(345, 15, 121, 46));

RemoveFlight.setText("Remove Flight");

RemoveFlight.addActionListener(new java.awt.event.ActionListener(){

publicvoid actionPerformed(java.awt.event.ActionEvent e){

System.out.println("actionPerformed()");// TODO Auto-generated Event stub actionPerformed()

RemovePanel.setVisible(true);

addPanel.setVisible(false);

}

});

}

return RemoveFlight;

}

/**

* This method initializes dTime

*

* @return javax.swing.JTextField

*/

private JTextField getDTime(){

if (dTime ==null){

dTime =new JTextField();

dTime.setBounds(new Rectangle(120, 105, 106, 16));

}

return dTime;

}

/**

* This method initializes aTime

*

* @return javax.swing.JTextField

*/

private JTextField getATime(){

if (aTime ==null){

aTime =new JTextField();

aTime.setBounds(new Rectangle(120, 150, 106, 16));

}

return aTime;

}

/**

* This method initializes RemovePanel

*

* @return javax.swing.JPanel

*/

private JPanel getRemovePanel(){

if (RemovePanel ==null){

removearrivalTime =new JLabel();

removearrivalTime.setBounds(new Rectangle(0, 150, 68, 16));

removearrivalTime.setText("Arrival Time");

removedepartTime =new JLabel();

removedepartTime.setBounds(new Rectangle(0, 105, 86, 16));

removedepartTime.setText("Departing Time");

RemoveDestination =new JLabel();

RemoveDestination.setBounds(new Rectangle(0, 60, 64, 16));

RemoveDestination.setText("Destination");

FlightNumber1 =new JLabel();

FlightNumber1.setBounds(new Rectangle(0, 15, 78, 16));

FlightNumber1.setText("Flight Number");

RemovePanel =new JPanel();

RemovePanel.setLayout(null);

RemovePanel.setBounds(new Rectangle(300, 90, 241, 226));

RemovePanel.setVisible(true);

RemovePanel.add(FlightNumber1,null);

RemovePanel.add(getRemoveflightnumber(),null);

RemovePanel.add(getSubmitremove(),null);

RemovePanel.add(RemoveDestination,null);

RemovePanel.add(getRemovedestination(),null);

RemovePanel.add(removedepartTime,null);

RemovePanel.add(getRemovedTime(),null);

RemovePanel.add(removearrivalTime,null);

RemovePanel.add(getRemoveATime(),null);

}

return RemovePanel;

}

/**

* This method initializes removeflightnumber

*

* @return javax.swing.JTextField

*/

private JTextField getRemoveflightnumber(){

if (removeflightnumber ==null){

removeflightnumber =new JTextField();

removeflightnumber.setBounds(new Rectangle(105, 15, 96, 17));

}

return removeflightnumber;

}

/**

* This method initializes submitadd

*

* @return javax.swing.JButton

*/

private JButton getSubmitadd(){

if (submitadd ==null){

submitadd =new JButton();

submitadd.setBounds(new Rectangle(90, 180, 74, 26));

submitadd.setText("Submit");

//submitadd.addActionListener(new AddListener(number, destination, dTime, aTime));

submitadd.addActionListener(new AddListener(number));

}

return submitadd;

}

/**

* This method initializes submitremove

*

* @return javax.swing.JButton

*/

private JButton getSubmitremove(){

if (submitremove ==null){

submitremove =new JButton();

submitremove.setBounds(new Rectangle(90, 180, 74, 26));

submitremove.setText("Submit");

submitremove.addActionListener(new RemoveListener(removeflightnumber, removedestination, removedTime, removeATime));

}

return submitremove;

}

/**

* This method initializes removedestination

*

* @return javax.swing.JTextField

*/

private JTextField getRemovedestination(){

if (removedestination ==null){

removedestination =new JTextField();

removedestination.setBounds(new Rectangle(90, 60, 119, 16));

}

return removedestination;

}

/**

* This method initializes removedTime

*

* @return javax.swing.JTextField

*/

private JTextField getRemovedTime(){

if (removedTime ==null){

removedTime =new JTextField();

removedTime.setBounds(new Rectangle(105, 105, 106, 16));

}

return removedTime;

}

/**

* This method initializes removeATime

*

* @return javax.swing.JTextField

*/

private JTextField getRemoveATime(){

if (removeATime ==null){

removeATime =new JTextField();

removeATime.setBounds(new Rectangle(105, 150, 102, 16));

}

return removeATime;

}

}

Add Listener Class

publicclass AddListenerextends WindowAdapterimplements ActionListener, Serializable{

protected JTextField number;

protected JTextField city;

protected JTextField departTime;

protected JTextField arriveTime;

protected FlightControl fc;

protected ObjectOutputStream out;

protected ObjectInputStream in;

protected Flight flight;

int[] fnumber;

String[] destination;

int[] DepartTime;

int[] ArrivalTime;

//public AddListener(JTextField n, JTextField c, JTextField dt, JTextField at)

public AddListener(JTextField n){

number = n;

//city = c;

//departTime = dt;

//arriveTime = at;

fc =new FlightControl();

//fnumber = new int[10];

//destination = new String[10];

//DepartTime = new int[10];

//ArrivalTime = new int[10];

}

publicvoid windowClosing (WindowEvent e){

System.exit( 0 );

}

publicvoid actionPerformed (ActionEvent e){

int num = Integer.parseInt(number.getText());

//String dest = city.getText();

//int dTime = Integer.parseInt(departTime.getText());

//int aTime = Integer.parseInt(arriveTime.getText());

//System.out.print(num+" "+dest+" "+dTime+" "+aTime);

//fc.addNewFlight(num, dest, dTime, aTime);

fc.addNewFlight(num);

number.setText("");

city.setText("");

departTime.setText("");

arriveTime.setText("");

for(int i = 0; i < 1; i++)

{

fnumber[i] = num;

destination[i] = dest;

DepartTime[i] = dTime;

ArrivalTime[i] = aTime;

}

}

}

Flight Control Class

publicclass FlightControlimplements Serializable{

protected ObjectOutputStream out;

protected ObjectInputStream in;

protected Flight flight;

public FlightControl(){

try{

out =new ObjectOutputStream(new FileOutputStream("flights.dat"));

in =new ObjectInputStream(new FileInputStream("flights.dat"));

}catch (IOException e){}

}

//public void addNewFlight(int n, String c, int dt, int at)

publicvoid addNewFlight(int n){

flight =new Flight();

try{System.out.print("In try block");

out.writeInt(n);

out.writeChars(c);

out.writeInt(dt);

out.writeInt(at);

out.flush();

//out.close();

}catch (IOException e){}

}

publicvoid removeFlight(int rn, String rc,int rdt,int rat){

//Flight[] flights = (Flight[])in.readObject();

//for(int i = 0; i < 10; i++)

//{

//System.out.print(flights[i]);

//}

/*for (int i = 0; i < 1; i++){

try{

flights[i] = (Flight)in.readObject();

} catch (ClassNotFoundException e){}catch (IOException e){}

}

return flight; */

}

}

[25665 byte] By [hodgenpodga] at [2007-11-27 4:29:25]
# 1
Your Flight class should be written as a proper Serializable.
hiwaa at 2007-7-12 9:38:24 > top of Java-index,Java Essentials,New To Java...