Last active
July 17, 2018 14:17
-
-
Save drmkraja/0ffc7dcf226f4828016dd63c08c4dff1 to your computer and use it in GitHub Desktop.
I'm working on a Plane Reservation System in Java, Tha plane has 5 Rows. Each Row has 10 seats. There is a Aisle after 5th seat. In my case, The 1st & last seats are already booked. Consider a person has to book 3 seats next to each other (seats across aisle not allowed). Now I need to return the available seats like (2,3,4 & 3,4,5 & 6,7,8 & 7,8,9)
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Welcome to the seat reservation system! | |
1 ABCDE FGHIJK | |
2 ABCDE FGHIJK | |
3 ABCDE FGHIJK | |
4 ABCDE FGHIJK | |
5 ABCDE FGHIJK | |
6 ABCDE FGHIJK | |
7 ABCDE FGHIJK | |
8 ABCDE FGHIJK | |
9 ABCDE FGHIJK | |
10 ABCDE FGHIJK | |
There are XX number of seats available. | |
Enter seat (e.g. 1A) or zero to quit the program. | |
1A | |
1 XBCDE FGHIJK | |
2 ABCDE FGHIJK | |
3 ABCDE FGHIJK | |
4 ABCDE FGHIJK | |
5 ABCDE FGHIJK | |
6 ABCDE FGHIJK | |
7 ABCDE FGHIJK | |
8 ABCDE FGHIJK | |
9 ABCDE FGHIJK | |
10 ABCDE FGHIJK | |
There are XX number of seats available. | |
Enter seat to assign (e.g., '1A'), or zero to quit: |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import java.util.ArrayList; | |
import java.util.Scanner; | |
public class Plane { | |
public static void main(String[] args) { | |
System.out.println("Welcome to the seat reservation system!"); | |
char[][] seats = new char [10][11]; | |
ArrayList<String> reservedSeats = new ArrayList<>(); | |
for (int i=0;i<10;i++){ | |
seats[i][0] = 'A'; | |
seats[i][1] = 'B'; | |
seats[i][2] = 'C'; | |
seats[i][3] = 'D'; | |
seats[i][4] = 'E'; | |
seats[i][5] = 'F'; | |
seats[i][6] = 'G'; | |
seats[i][7] = 'H'; | |
seats[i][8] = 'I'; | |
seats[i][9] = 'J'; | |
seats[i][10] = 'K'; | |
} | |
Scanner console = new Scanner(System.in); | |
int filled = 0; | |
printSeats(seats); | |
System.out.println("Enter seat (e.g. 1A) or zero to quit the program.");//How to make 0 the exit key? | |
String input = console.nextLine(); | |
while ((filled <48) &&(input.length() >0)) { | |
int row = input.charAt(0) - '1'; | |
int col = input.charAt(1) - 'A'; | |
if (row<0 || row>11 || col<0 || col>10) { | |
System.out.println("Input error. Enter seat to assign (e.g., '1A'), " + | |
"or zero to quit."); | |
input = console.nextLine(); | |
} else { | |
if (seats[row][col] != 'X') { | |
seats[row][col] = 'X'; | |
filled++; | |
System.out.println(); | |
printSeats(seats); | |
} | |
if (filled < 48) { | |
System.out.println("Enter seat to assign (e.g., '1A'), " + | |
"or zero to quit:"); | |
input = console.nextLine(); | |
} | |
} | |
} | |
System.out.println("Final seat assignments: "); | |
printSeats(seats); | |
} | |
private static void printSeats(char[][] seats) { | |
for (int i = 0; i < seats.length; i++) { | |
System.out.println((i + 1) + " " + | |
seats[i][0] + seats[i][1] + seats[i][2] + seats[i][3] + seats[i][4] + " " + | |
seats[i][5] + seats[i][6] + seats[i][7]+ seats[i][8] + seats[i][9]+ seats[i][10]); | |
} | |
System.out.println("There are XX number of seats available."); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment