Java help.

RendRend Registered User regular
edited May 2008 in Help / Advice Forum
Alright so I'm coding the final project for my Java class this semester, it's an interface for a small commercial airliner, and I'm having trouble with the GUI (swing).

Here's the way it works. When I load up the program, it's supposed to read from a file some Passengers, Flights, and accounts information. It does this successfully. They have been written to a file with the writeObject() method beforehand.

At this point, they're all read until the end of the file, and then added to an ArrayList<Passenger> inside the FlightManager class. It successfully does this, because then FlightManager calls a boot() function which uses a forEach loop to call the addSelf() function of the passengers, so they'll add appropriate information based on their passenger type to whatever flights they're on.

However, when I go to the display button on my GUI, they don't display. It only displays passengers created during the current session. Somehow the objects, I think, are being ejected from the ArrayList in FlightManager, because the ones that don't come up are not saved to file for the next program start to even try to read.

There are 2 kinds of passengers: OneWay passengers, and RoundTrip passengers.

Here's some code.
from Auditor, the file manager class.
	public void readPassengers(ArrayList<Passenger> passengers, FlightManager f)
	{
		File file = new File(passengerFile);
		
		try
		{
			ObjectInputStream input = new ObjectInputStream(new FileInputStream(passengerFile));
			
			try
			{
				while(true)
				{
					Passenger p = (Passenger)input.readObject();
					(f.getPassengers()).add(p);
				}
			}
			catch(Exception e)
			{
				JOptionPane.showMessageDialog(null, e.getMessage() + "FAIL: Adding passengers");
				input.close();
			}
		}
		catch(Exception e)
		{
			JOptionPane.showMessageDialog(null, e.getMessage() + "FAIL: Finding File");
		}
	}
From guiLoader, the GUI class, though I'm pretty certain this is not where the problem is
    	else if(buttonString.equals("Passengers"))
    	{
    		JFrame frame = new JFrame("Display Passengers");
    		frame.setSize(300,500);
    		frame.setVisible(true);
    		JTextArea text = new JTextArea();
    		text.setVisible(true);
    		text.setEditable(false);
    		
    		//ArrayList<Passenger> passengers = new ArrayList<Passenger>(20);
    		
    		/*JMenu exitDisplayPassengers = new JMenu ("Exit");
    		JMenuItem exitDisplayPassengerButton = new JMenuItem("Exit");
    		exitDisplayPassengers.add(exitDisplayPassengerButton);
    		exitDisplayPassengerButton.addActionListener(this);
    		
    		JMenuBar newBar = new JMenuBar();
    		newBar.add(exitDisplayPassengers);
    		frame.add(newBar);*/
    		frame.add(text);
    		
    		String theText = "";
    		
    		for(Passenger element : (f.getPassengers()))
    		{
    			JOptionPane.showMessageDialog(null, element.getName());
    			theText += element.display();
    		}

    		
    		text.setText(theText);
From FlightManager.


    public void boot()
    {    	
    	for(Passenger element : passengers)
    	{
    		element.addSelf(this);
    	}
    }

    public void addPassenger(String number, Passenger p)
    {
    	if(!passengers.contains(p))
    	{
    		JOptionPane.showMessageDialog(null, "I'm RETARDED");
    		passengers.add(p);
    	}
    	
    	JOptionPane.showMessageDialog(null, p.getName() + " Add Passenger method called");
    	if(!this.flightExists(number))
    	{
    		JOptionPane.showMessageDialog(null, "Creating Flight");
    		createFlight(number);
    	}
    	if(this.flightFull(number))
    	{
    		JOptionPane.showMessageDialog(null, "Flight full- passenger not added.");
    	}
    	else
    	{
    		for(int i = 0; i < flights.size(); i++)
    		{
    			if(number.equals((flights.get(i)).getNumber()))
    			{
    				(flights.get(i)).addPassenger(p);
    			}
    		}
    	}
From OneWay, which extends Passenger
	public void addSelf(FlightManager f)
	{
		f.addPassenger(getNumber(), this);
		JOptionPane.showMessageDialog(null, "I'M BEING READ AS A ONE-WAY: " + getName());
	}

Please excuse my message boxes, I don't have a proper debugger on my compiler, so thats what i'm using to debug.

I'm thinking this. I don't create new objects, I just read them from file. This may be where my problem is, but on the other hand, how could I read from the file and upcast from Passenger to OneWay or RoundTrip in order to properly create the new object and add it?

And if it is a problem like that, then why does it make it all the way into boot(), where it's obviously in the ArrayList, what happens to it to take it out of the ArrayList?

Rend on

Posts

  • DeswaDeswa Registered User regular
    edited May 2008
    By added in the current session, you mean non read from file passengers, right? If thats the situation, it may be that you are creating a new ArrayList after having read the file.

    Deswa on
    gobassgo wrote:
    "It ain't rape, it's surprise sex!"
    wii : 3788 3264 2419 8070
  • RendRend Registered User regular
    edited May 2008
    Deswa wrote: »
    By added in the current session, you mean non read from file passengers, right? If thats the situation, it may be that you are creating a new ArrayList after having read the file.

    Yes, but the only time I instantiate an array list in FlightManager is when it's constructed, which is before any file reading begins, though I'll go check it once more.

    Rend on
Sign In or Register to comment.