10/11/2018
package ticketbookingx;
import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.util.Scanner;
public class TicketBookingX {
public static void main(String[] args) {
BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
int x = 0x2713;
String emptySeat = " [" + (char) x + "] ";
String bookedSeat = " [x] ";
String savedPassword = "1234";
Scanner sn = new Scanner(System.in);
int passwordAttempt = 3;
//login area
while (passwordAttempt > 0) {
System.out.print("Enter your Password: ");
String password = sn.nextLine();
if (password.equals(savedPassword)) {
break;
}
passwordAttempt--;
if (passwordAttempt == 0) {
System.out.println("No attempt remaining, system suspended");
return;
}
System.out.println("Wrong password, remaining attempt: " + passwordAttempt);
}
System.out.println("Welcome to Hell Hall Booking system!");
//infinite loop for main area
int choice;
boolean[][] seats; // reference of a two dimension array
//allocating memory for seats array
seats = new boolean[10][10];
//initialising front array using loop and inner loop
for (int i = 0; i < seats.length; i++) {
for (int j = 0; j < seats[i].length; j++) {
seats[i][j] = true;
}
}
do {
System.out.println("Main Menu");
System.out.println("1. View Hall");
System.out.println("2. Book Ticket");
System.out.println("3. Exit");
try {
System.out.println("Enter your choice: ");
choice = Integer.parseInt(br.readLine());
switch (choice) {
case 1:
System.out.println("Showing front area:");
char rowID = 'J';
System.out.print(" ");
for (int i = 0; i < seats[0].length; i++) {
System.out.print("\t [" + (i + 1) + "] ");
}
System.out.println("");
for (int i = 0; i < seats.length; i++) {
System.out.print(rowID--);
for (int j = 0; j < seats[i].length; j++) {
if (seats[i][j]) {
System.out.print("\t" + emptySeat);
} else {
System.out.print("\t" + bookedSeat);
}
}
System.out.println("");
}
System.out.println("\n\n\t|--------------------------------S-C-R-E-E-N--------------------------------|");
break;
case 2:
char rowType=' ';
do {
System.out.println("f. Front");
System.out.println("r. Rear");
System.out.println("d. Deluxe");
System.out.println("x. Goto Main Menu");
System.out.print("Enter Seat Type: ");
try {
rowType = sn.next().toLowerCase().charAt(0);
if(!(rowType=='f' || rowType=='r' || rowType=='d' || rowType=='x')){
throw new Exception("Wrong input");
}
} catch (Exception e) {
System.out.println(e.getMessage());
continue;
}
if (rowType == 'x') {
break;
}
System.out.println("Your selection is: " + rowType);
System.out.println("Number of empty seats are: " + getNumberOfEmptySeats(seats, rowType));
System.out.println("Enter number of seats");
int numberOfSeatsRequired = sn.nextInt();
if (numberOfSeatsRequired > getNumberOfEmptySeats(seats, rowType)) {
System.out.println("Not enough seats");
continue;
}
System.out.println("Total Cost is: " + (numberOfSeatsRequired * getRateByRowByType(rowType)));
int temp = numberOfSeatsRequired;
int temp_row = 0;
if (rowType == 'f') {
temp_row = 7;
}
if (rowType == 'r') {
temp_row = 3;
}
if (rowType == 'd') {
temp_row = 0;
}
int temp_col = 9;
while (temp > 0) {
if (seats[temp_row][temp_col] == false) {
temp_col--;
if (temp_col < 0) {
temp_row++;
temp_col = 9;
}
continue;
}
seats[temp_row][temp_col] = false;
String seatNumber = "" + (char) (74 - temp_row);
System.out.println(seatNumber + (temp_col + 1));
temp--;
temp_col--;
if (temp_col < 0) {
temp_row++;
temp_col = 9;
}
}
} while (true);
break;
case 3:
System.out.println("Thank you for using Hell Booking System");
return; // return will close the program.
default:
System.out.println("Wrong input, Enter again");
}
} catch (Exception e) {
System.out.println("Error in input, " + e.getMessage());
choice = 0;
}
} while (true);
}
public static int getRateByRowByType(char rowType) {
switch (rowType) {
case 'f':
return 100;
case 'r':
return 150;
case 'd':
return 300;
default:
return -1;
}
}
public static int getNumberOfEmptySeats(boolean[][] seats, char rowType) {
int count = 0;
int startRow, endRow;
if (rowType == 'f') {
startRow = 7;
endRow = 9;
} else if (rowType == 'r') {
startRow = 3;
endRow = 6;
} else if (rowType == 'd') {
startRow = 0;
endRow = 2;
} else {
return -1;
}
for (int i = startRow; i