import javax.swing.*;
public class AirPlaneSeatInitializer
{
private static String [] seatLayout = {"4509", "2707", "1606", "1505", "3507", "3306", "3005", "4208"};
private static int layOutController = -1;
/**
* Returns the number of rows in a seat layout string of the form ####
*
* @return int the int value number of rows
*/
public static int getRows(String layOut)
{
return Integer.parseInt(layOut.substring(0, 2));
}
/**
* Returns the number of columns in a seat layout string of the form ####
*
* @return int the int value number of columns
*/
public static int getColumns(String layOut)
{
return Integer.parseInt(layOut.substring(2));
}
/**
* Resets the object so that is may be used again
*/
public static void reset()
{
layOutController = -1;
}
/**
* Checks if the object contains more layout strings.
*
* @return true if there are more Layout Strings. Returns false otherwise.
*/
public static boolean hasMoreLayoutStrings()
{
if (layOutController < seatLayout.length - 1)
return true;
return false;
}
/**
* Returns the next Layout stirng in the object. Must use with hasMoreLayoutStrings()
*
* @return String the Layout String Objects
*/
public static String getNextLayoutString()
{
layOutController++;
return seatLayout[layOutController];
}
/**
* User can pick a plane randomly with a particular layout
*
* @return String the plane's payout in the form ####
*/
public static String pickAPlaneWithLayout()
{
int selected = -1;
while (selected < 0 || selected > 7)
{
double val = Math.random() * 10;
//System.out.println(val);
selected = (int) val;
//System.out.println(selected);
}
return seatLayout[selected];
}
private static void showSeats(char [][]seatArray)
{
for (int i = 0; i < seatArray.length; i++)
{
for (int j = 0; j < seatArray[0].length; j++)
System.out.print("" +seatArray[i][j]+ " ");
System.out.println("\n");
}
}
/**
* Given number of rows and columns, this method will return a 2-D array
* of with some cells filled with 'C' and some filled with 'X'. The number
* of rows must within the range 15 to 45 and the number of columns must
* within the range 5 to 9.
*
* @return char[][] a 2-D character array.
*/
public static char [][]get2DSeatsArrayForAirPlane( int rows, int cols)
{
char [][] seatsArray = new char[rows][cols];
int seatsFilled = 0;
if ((rows > 45 || rows < 15) || (cols < 5 || cols > 9))
{
JOptionPane.showMessageDialog(null, "Your request cannot be granted. Please\n" +
"enter a valid number of rows and/or\n" +
"valid number of seats per row.\n" +
"Rangers allowed - 15 <= rows <= 45 and\n"+
"5 <= seats-per-row <= 9.", "Error",
JOptionPane.ERROR_MESSAGE);
return null;
}
for (int i = 0; i < seatsArray.length; i++)
for (int j = 0; j < seatsArray[0].length; j++)
seatsArray[i][j] = 'X';
seatsArray [0][0] = 'C';
seatsArray [0][cols-1] = 'C';
seatsArray [rows-1][0] = 'C';
seatsArray [rows-1][cols-1] = 'C';
seatsFilled = 4;
if (rows < 28)
{
seatsArray [(rows-1)/2][0] = 'C';
seatsArray [(rows-1)/2][cols-1] = 'C';
seatsFilled += 2;
}
else
if (rows >= 28)
{
seatsArray [(int)(rows* 0.25)][0] = 'C';
seatsArray [(int)(rows* 0.25)][cols-1] = 'C';
seatsArray [(int)(rows* 0.75) - 2][0] = 'C';
seatsArray [(int)(rows* 0.75) - 2][cols-1] = 'C';
seatsFilled += 4;
}
int seed = -1;
//Find a % to dynamically fill the seats
//any percent between 0% and 80%
while (seed < 0 || seed > 80)
{
double val = Math.random() * 100;
//System.out.println(val);
seed = (int) val;
//System.out.println(seed);
}
//calculate the number of sets to be filled
int seatsToBeFilled = seatsFilled + (rows * cols)* seed/100;
int currentFill = 0;
int row_pos = -1 , col_pos = -1;
while (currentFill < seatsToBeFilled)
{
//randomly find a row
row_pos = -1 ;
while (row_pos < 0 || row_pos > rows-1)
{
double val = Math.random() * 100;
//System.out.println(val);
row_pos = (int) val;
}
//Randomly find a col
col_pos = -1;
while (col_pos < 0 || col_pos > cols-1)
{
double val = Math.random() * 100;
//System.out.println(val);
col_pos = (int) val;
}
//Randomly fill a seat
if (seatsArray[row_pos][col_pos] == 'X')
{
seatsArray[row_pos][col_pos] = '*';
currentFill++;
}
}//end outer while
return seatsArray;
}
/** public static void main(String[] args)
{
AirPlaneSeatInitializer.reset();
char ary[][] = get2DSeatsArrayForAirPlane(45, 9);
System.out.println("Rows : " + ary.length + ", Cols : " + ary[0].length);
for (int i = 0; i < ary.length-1; i++)
{
for (int j = 0; j < ary[0].length-1; j++)
{
System.out.print(ary[i][j] + " ");
}
System.out.println("");
}
}
**/
}