PART A: AIRLINE SEATING Millions of commercial airline flights cross the world\'
ID: 3755770 • Letter: P
Question
PART A: AIRLINE SEATING Millions of commercial airline flights cross the world's cities every year, and each one of these flights must solve what seems like a simple problem: how do you arrange where each passenger on one of those flights sits? In this assignment, we will solve a variation on that problem, where we will observe one restriction on the seats: people who book a flight together will be - if possible-seated together. Your program will process data from an array of strings (call this the "bookings" array). The first array element will contain a number indicating how many seats the aircraft has. The remaining elements will contain information about groups of passengers who have booked seats on the flight. The first element of this grouping will be a number that indicates how many people are in the group. The remaining elements identify the individual passengers in that group by their last and first name For example, the array might contain the following 10 strings: 8 Nobbly, Greg Nobbly, Jo-Anne 1 Lee, Sook Lukas, Stephie Lukas, Cambridge Lukas, ogden which means there are 8 seats on the flight, the first group of passengers has 2 people (with their names below), the second group has 1 person, and the third group has 3 people. Normally this data would come from input, such as lines from a text file, not from an array. That's the next assignment.Explanation / Answer
/**
* This class implements the seat booking assignment as mentioned
*/
public class Bookings {
// Creating an enum of type of booking types, add more types and
// create booking array according to the type
enum FLIGHT_TYPE{TYPE1, TYPE2, TYPE3}
/**
* This method takes a booking type and returns the booking array accordingly.
* add more types and array to test with more bookings
* @param type
* @return
*/
public static String[] getBookings(FLIGHT_TYPE type){
String[] bookings = null;
// Assign Booking array based on passed type
switch (type){
case TYPE1:
bookings = new String[]{"8", "2", "Nobbly, Greg", "Nobbly, Jo-Anne", "1", "Lee, Sook", "3",
"Lukas, Stephie", "Lukas, Cambridge", "Lukas, Ogden"};
break;
case TYPE2:
bookings = new String[]{"12", "2", "Nobbly, Greg", "Nobbly, Jo-Anne", "1", "Lee, Sook", "3",
"Lukas, Stephie", "Lukas, Cambridge", "Lukas, Ogden",
"4", "LastName1 Passenger1", "LastName2 Passenger2", "LastName3 Passenger3", "LastName4, Passenger4"};
break;
case TYPE3:
bookings = new String[]{"9", "2", "Nobbly, Greg", "Nobbly, Jo-Anne",
"4", "LastName1 Passenger1", "LastName2 Passenger2", "LastName3 Passenger3", "LastName4, Passenger4",
"1", "Lee, Sook", "3",
"Lukas, Stephie", "Lukas, Cambridge", "Lukas, Ogden"};
break;
}
return bookings;
}
/**
* This method does the seating arrangement part.
* It assigns the seat based on mentioned in Part A.
* And Returns the seating array with seat number and
* name of the assigned person on that seat.
* @param bookings
* @return
*/
public static String[] seating(String[] bookings){
String[] seats = null;
if(bookings != null){
// Checking if booking array contains some data
if(bookings.length > 1){
int index = 0;
// Fetching first element i.e. number of seats in the plane.
int num_seats = Integer.parseInt(bookings[index]);
// this we are using to try max attempts
// to find the number of seats available sequentially(for group)
// change this number accordingly
final int MAX_NUM_ATTEMPTS = num_seats*100;
int num_filled_seats = 0;
seats = new String[num_seats];
// Looping through the booking array to assign seat
for(index = 1; index < bookings.length;){
int current_group_size = 0;
if(current_group_size == 0){
// Get the group number and convert it to integer from string
current_group_size = Integer.parseInt(bookings[index++]);
int attempts = 0;
// Check if adding this group will exceeds the max available seats
if(num_seats >= (num_filled_seats + current_group_size)){
boolean groupAssignedSequentially = false;
// Keep trying to find seat for the group until found or max attempts reached
while (attempts < MAX_NUM_ATTEMPTS) {
attempts++;
int random_num = (int) (Math.random() * (num_seats - 1)) + 0;
boolean found = true;
for(int i = 0; i < current_group_size; i++){
if( (i + random_num) >= num_seats || seats[i+random_num] != null){
found = false;
break;
}
}
// If found the seats for the entire group then assign the seats
if(found){
for(int i = 0; i < current_group_size; i++){
seats[i+random_num] = bookings[index++];
num_filled_seats++;
}
groupAssignedSequentially = true;
break;
}
}
// If maximum attempts reached but still could not found seat for the group
// sequentially then split the group and assign seat
// for each individuals randomly
if(!groupAssignedSequentially) {
for (int k = 0; k < current_group_size; k++) {
while (true) {
int random_num = (int) (Math.random() * (num_seats - 1)) + 0;
boolean found = true;
for (int i = 0; i < 1; i++) {
if (seats[i + random_num] != null) {
found = false;
break;
}
}
if (found) {
for (int i = 0; i < 1; i++) {
seats[i + random_num] = bookings[index++];
num_filled_seats++;
}
break;
}
}
}
}
}else{
System.out.println("Not enough seats are available, " +
"can't assign seats to the group size of "+ current_group_size);
break;
}
}
}
}
}
return seats;
}
/**
* This method prints the current seating arrangements
* in the specified format
* @param seats
*/
public static void printSeats(String[] seats){
int index = 1;
System.out.println(" ");
for(String str: seats){
if(str == null){
str = "-empty-";
}
System.out.printf("Seat %d: %s ", index++, str);
}
}
/**
* Main driver of the program
* @param str
*/
public static void main(String[] str){
// Calling getBooking to get booking array,
// then passing that array to seating method and finally
// printing the seating arrangement
printSeats(seating(getBookings(FLIGHT_TYPE.TYPE1)));
printSeats(seating(getBookings(FLIGHT_TYPE.TYPE2)));
printSeats(seating(getBookings(FLIGHT_TYPE.TYPE3)));
}
}
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.