My program keeps looping when asking for seat letter. Can anyone help me out as to why that is?
import java.io.*;
import java.util.*;
public class AirplaneSeating
{
static Scanner console = new Scanner(System.in);
public static void main(String[] args) throws IOException
{
char[][] seatPlan = new char[13][6];
char response;
initializeSeatPlan(seatPlan);
showMenu(seatPlan);
System.out.print("\nWould you like to reserve a seat Y(Yes) or N(No): ");
response = console.next().charAt(0);
System.out.println();
while(response == 'y' || response == 'Y')
{
assignSeat(seatPlan);
showMenu(seatPlan);
System.out.println("Reserve another seat Y(Yes) or N(No): ");
response = console.next().charAt(0);
System.out.println();
}
}
public static void initializeSeatPlan(char[][] sPlan)
{
int i, j;
for(i = 0; i < sPlan.length; i++)
for(j = 0; j < sPlan[0].length; j++)
sPlan[i][j] = '*';
}
public static void showSeatAssignments(char[][] sPlan)
{
int i, j;
System.out.println(" A B C D E F");
for(i = 0; i < sPlan.length; i++)
{
if(i < 9)
System.out.print("Row " + (i+1) + " ");
else
System.out.print("Row " + (i+1) + " ");
for(j = 0; j < sPlan[0].length; j++)
{
System.out.print(sPlan[i][j] + " ");
if(j == 2)
System.out.print(" ");
}
System.out.println();
}
System.out.println("* -- available seat");
System.out.println("X -- occupied seat");
System.out.println();
}
public static void assignSeat(char[][] sPlan) throws IOException
{
char ticketType;
char response;
System.out.print("Enter ticket type: F(first class) "
+ " E(Economy class): ");
ticketType = console.next().charAt(0);
System.out.println();
switch(ticketType)
{
case 'f':
case 'F':
if(!isFirstClassFull(sPlan))
assignFirstClassSeat(sPlan);
else
{
System.out.println("No first class seats are available");
System.out.print("Press Y(Yes) to continue: ");
response = console.next().charAt(0);
System.out.println();
}
break;
case 'e':
case 'E':
if(!isNonSmokingEconomicFull(sPlan) ||!isSmokingEconomicFull(sPlan))
assignEconomicSeat(sPlan);
else
{
System.out.println("No economy class seats are available");
System.out.print("Press Y(Yes) to continue: ");
response = console.next().charAt(0);
System.out.println();
}
}
showSeatAssignments(sPlan);
}
public static void showMenu(char[][] sPlan)
{
System.out.println("The current seat assignments is as follows.");
showSeatAssignments(sPlan);
System.out.println("Rows 1 and 2 are for first class passengers");
System.out.println("Rows 1 through 7 are nonsmoking");
}
public static boolean isFirstClassFull(char[][] sPlan)
{
int i, j;
int assignedSeats = 0;
for(i = 0; i < 2; i++)
for(j = 0; j < sPlan[0].length; j++)
if(sPlan[i][j] == 'X')
assignedSeats++;
return (assignedSeats == 2 * sPlan[0].length);
}
public static boolean isNonSmokingEconomicFull(char[][] sPlan)
{
int i, j;
int assignedSeats = 0;
for(i = 2; i < 7; i++)
for(j = 0; j < sPlan[0].length; j++)
if(sPlan[i][j] == 'X')
assignedSeats++;
return (assignedSeats == 5 * sPlan[0].length);
}
public static boolean isSmokingEconomicFull(char[][] sPlan)
{
int i, j;
int assignedSeats = 0;
for(i = 7; i < 13; i++)
for(j = 0; j < sPlan[0].length; j++)
if(sPlan[i][j] == 'X')
assignedSeats++;
return (assignedSeats == 6 * sPlan[0].length);
}
public static void selectSeatNumber(int startRows, int endRows, int rowNo, char columnNo)
throws FileNotFoundException, IOException
{
System.out.print("Enter Row number " + startRows + " - " + endRows + ": ");
rowNo = console.nextInt();
System.out.println();
while(rowNo < startRows || rowNo > endRows)
{
System.out.print("Enter Row number " + startRows + " - " + endRows + ": ");
rowNo = console.nextInt();
System.out.println();
}
System.out.print("Enter seat letter (A - F): ");
columnNo = console.next().charAt(0);
System.out.println();
while(columnNo <= 'A' || columnNo >= 'F')
{
System.out.print("Enter seat letter (A - F): ");
columnNo = console.next().charAt(0);
System.out.println();
}
}
public static void assignFirstClassSeat(char[][] sPlan) throws FileNotFoundException, IOException
{
int rowNum = 0;
char columnPos = 0;
if(!isFirstClassFull(sPlan))
{
selectSeatNumber(1, 2, rowNum, columnPos);
while(sPlan[rowNum - 1][(int)columnPos - 65] != '*')
{
System.out.println("*This seat is occupied*");
System.out.println("Make another selection");
showSeatAssignments(sPlan);
selectSeatNumber(1, 2, rowNum, columnPos);
}
sPlan[rowNum - 1][(int)columnPos - 65] = 'X';
System.out.println("This seat is reserved for you");
}
else
System.out.println("First Class is now full");
}
public static void assignEconomicSeat(char[][] sPlan) throws IOException
{
char seatType;
if(!isNonSmokingEconomicFull(sPlan) && isSmokingEconomicFull(sPlan))
assignSeatNonSmokingEconomic(sPlan);
else
if(isNonSmokingEconomicFull(sPlan) && !isSmokingEconomicFull(sPlan))
assignSeatSmokingEconomic(sPlan);
else
if(!isNonSmokingEconomicFull(sPlan) && !isSmokingEconomicFull(sPlan))
{
System.out.print("Enter seat type: S(Smoking)or N(nonsmoking):");
seatType = console.next().charAt(0);
System.out.println();
while(seatType != 'S' && seatType != 's' && seatType != 'N' && seatType != 'n')
{
System.out.println("Invalid seat type.");
System.out.print("Enter S(Smoking)or N(nonsmoking): ");
seatType = console.next().charAt(0);
System.out.println();
}
if(seatType == 'N' || seatType == 'n')
assignSeatNonSmokingEconomic(sPlan);
else
assignSeatSmokingEconomic(sPlan);
}
else
System.out.println("Economic Class is now Full");
}
public static void assignSeatNonSmokingEconomic(char[][] sPlan) throws IOException
{
int rowNum = 0;
char columnPos = 0;
if(!isNonSmokingEconomicFull(sPlan))
{
selectSeatNumber(3, 7, rowNum, columnPos);
while(sPlan[rowNum - 1][(int)columnPos - 65] != '*')
{
System.out.println("*This seat is occupied *");
System.out.println("Make another selection");
showSeatAssignments(sPlan);
selectSeatNumber(3, 7, rowNum, columnPos);
}
sPlan[rowNum - 1][(int)columnPos - 65] = 'X';
System.out.println("This seat has been reserved for you");
}
else
System.out.println("Non smoking section of economic class is Full");
}
public static void assignSeatSmokingEconomic(char[][] sPlan) throws IOException
{
int rowNum = 0;
char columnPos = 0;
if(!isSmokingEconomicFull(sPlan))
{
selectSeatNumber(8, 13, rowNum, columnPos);
while(sPlan[rowNum - 1][(int)columnPos - 65] != '*')
{
System.out.println("* This seat is occupied *");
System.out.println("Make another selection");
showSeatAssignments(sPlan);
selectSeatNumber(8, 13, rowNum, columnPos);
}
sPlan[rowNum - 1][(int)columnPos - 65] = 'X';
System.out.println("This seat is reserved for you");
}
else
System.out.println("Smoking section of economic class is Full");
}
}