Good morning,
I have written the following code for an airline reservation system. The applet will assign the user a seat in either economy or first class. When first class is full, it will offer economy seats. This works - but when it is about to assign the last seat in 'economy', the program stops working. The error messages suggest a problem in the method call to selectSeat() in the 'economy' part of the code. I can't see the problem, though, as this code is identical to the code I used for the 'first class' part.
//Airline booking
//Coded by Eoghain on 16/9/07
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
public class AirlineApplet extends JApplet implements ActionListener {
JButton printPass;
JTextArea outputArea;
String output;
int seatType, seatNum, choice;
boolean[] seatArray = {false, false, false, false, false, false, false, false, false, false};
public void init() {
Container container = getContentPane();
container.setLayout(new FlowLayout());
chooseType();
printPass = new JButton ("Click to print boarding pass");
printPass.addActionListener(this);
outputArea = new JTextArea(40, 40);
container.add(printPass);
container.add(outputArea);
}
public void chooseType() {
seatType = Integer.parseInt(JOptionPane.showInputDialog("Please type 1 first First Class or 2 for Economy"));
selectSeat(seatType);
}
public void actionPerformed(ActionEvent event) {
outputArea.append(output);
chooseType();
}
public void selectSeat(int x) {
//argument selects between first and economy
if (x == 1)
{
if (seatArray[0] && seatArray[1] && seatArray[2] && seatArray[3] && seatArray[4])
{
choice = Integer.parseInt(JOptionPane.showInputDialog("There are no seats available in First Class. Would you like Economy? 1 for Yes, 2 for No."));
if (choice == 1)
selectSeat(2);
else
JOptionPane.showMessageDialog(null, "Next flight leaves in 3 hours.");
}
else
{
//do first stuff and make sure the seat is unoccupied
seatNum = (int)(Math.random()*5);
if (!seatArray[seatNum])
seatArray[seatNum] = true;
else
selectSeat(x);
output = "Your have seat " + seatNum + " in First Class. \n";
}
}
else
{
if (seatArray[5] && seatArray[6] && seatArray[7] && seatArray[8] && seatArray[9])
JOptionPane.showMessageDialog(null, "There are no seats available in Economy. Sorry.");
else
{
//do economy stuff
seatNum = (5 + (int)(Math.random()*4));
if (!seatArray[seatNum])
seatArray[seatNum] = true;
else
selectSeat(x);
output = "Your have seat " + seatNum + " in Economy. \n";
}
}
}
}
Any advice would be greatly appreciated,
-Eoghain