If statements.
Im writing an airline reservation program. The first two elements of the array are reserved for the smoking clients and the other two elements are reserved for non smoking clients. The number 1 is entered to indicate that the resevation is for a smoking clients and number 2 for a non smoking client.If the smoking section is full, the client must be given a choice to be booked in the non - smoking section or to be told when the next flight leaves if the flight is full or if the client does not want to seat in the non-smoking section, vice versa.
Problem : The program works partially but the line "The next line leaves in 3 hours"? Could someone tell me why?
import javax.swing.*;
import java.awt.*;
publicclass Reservation5extends JApplet
{
String sNumber, output, sNumber2, answer;
int number,index, seats[];
JTextArea area;
publicvoid init()
{
Container c = getContentPane();
c.setLayout(new FlowLayout());
area =new JTextArea(10, 25);
c.add(area);
seats =newint[4];
output ="Seating Arrangements before any reservations\n";
for(int i = 0; i < seats.length; i++)
output += i +"\t" + seats[i] +"\n";
output +="Seating Arrangements after any reservations\n";
sNumber = JOptionPane.showInputDialog("Please enter 1 for smoking and 2 for non smoking");
number = Integer.parseInt(sNumber);
for(int i = 0; i < seats.length; i++)
{
switch(number){
case 1:index = smoking(seats,number);
if (index == -1)
{
JOptionPane.showMessageDialog(null,"The smoking section is full.\n Can you seat in the non smoking section?");
JOptionPane.showInputDialog("Enter Yes to be seated in the non smoking section");
index = nonSmoking(seats,number);
if ((index == -1) && (answer !="Yes"))
{
JOptionPane.showMessageDialog(null,"The next flight leaves in 3 hours!");//"This line is not executing"
i = i -1;
}
else
{
seats[index] = number;
output +="Res: " + i +" for smoking section to non smoking : seats [" + index +"]" + number+"\n";
}
}
else{
seats[index] = number;
output +="Reservation " + i +" in smoking section : seats [" + index +"]" + number+"\n";
}
break;
case 2 : index = nonSmoking(seats,number);
if (index == -1)
{
JOptionPane.showMessageDialog(null,"The non smoking section is full.\n Can you seat in the smoking section?");
JOptionPane.showInputDialog("Enter Yes to be seated in the smoking section");
index = smoking(seats,number);
if ((index == -1) && (answer !="Yes"))
{
JOptionPane.showMessageDialog(null,"The next flight leaves in 3 hours!");//"This line is not executing";
i = i -1;
}
else
{
seats[index] = number;
output +="Res: " + i +" for non smoking to smoking : seats [" + index +"]" + number+"\n";
}
}
else{
seats[index] = number;
output +="Reservation " + i +" in non smoking section : seats [" + index +"]" + number+"\n";
}
break;
default: JOptionPane.showMessageDialog(null,"Invalid Entry");
}
sNumber = JOptionPane.showInputDialog("Please enter 1 for smoking and 2 for non smoking");
number = Integer.parseInt(sNumber);
}
area.setText(output);
}
publicint smoking(int a[],int num)
{
int middle = (seats.length)/2;
for(int n = 0; n < middle; n++)
if(a[n] != num)
return n;
return -1;
}
publicint nonSmoking(int a[],int num)
{
int middle = (seats.length)/2;
for(int n = middle; n < seats.length; n++)
if(a[n] != num)
return n;
return -1;
}
}
Here is my html tag
HTML>
<TITLE> Airline Reservation System Applet</TITLE>
<HR>
<APPLET CODE = "Reservation5.class" WIDTH = 300 HEIGHT = 200>
</APPLET>
<HR>
</HTML>

