In: Computer Science
Bus ticket reservation project in java using class ,inheritance,interface
The following is the code for Ticket Reservation System in Java..
import java.util.Date;
public class reservation {
// Create an array of 20 seats, 14 window and 6 aisle.
private static int[] seats = new int[12];
public static void main(String args[]) {
System.out.println("Welcome to the KSTDC eservation
system!");
System.out.println("Tourism, freebies, enjoyable
destinations!");
System.out.println();
// Lets start by setting all seats equal to 0 (aka Empty)
for (int i = 0; i < 12; i++) {
seats[i] = 0;
}
// Setup our scanner and make the default the choice to
window.
Scanner s = new Scanner(System.in);
int choice = 1;
// Ask user for a window or an aisle seat and store their
coice.
System.out.print("Please enter 1 for window, 2 for aisle, or 0 to
exit: ");
choice = s.nextInt();
// While their choice is not the one for exit, execute our
booking.
while (choice != 0) {
int seatnumber = 0;
// If they chose a window seat, attempt to book it.
if (choice == 1) {
seatnumber = bookWindow();
// No window seats available, try booking an aisle seat for them
instead.
if (seatnumber == -1) {
seatnumber = bookAisle();
if (seatnumber != -1) {
System.out.println("Sorry, we were not able to book a window seat.
But do have an aisle seat.");
printBoardingPass(seatnumber);
}
}
else {
// Booking a window seat was successful.
System.out.println("You are ilucky we have a window seat
available!");
printBoardingPass(seatnumber);
}
}
else if (choice == 2) {
// If they chose booking an isle, check to see if it is
available.
seatnumber = bookAisle();
// If not available, see if we have window seats available.
if (seatnumber == -1) {
seatnumber = bookWindow();
if (seatnumber != -1) {
System.out.println("Sorry, we were not able to book an aisle seat.
But do have a window seat.");
printBoardingPass(seatnumber);
}
}
else {
// Booking an aisle seat was successful.
System.out.println("You are in luck, we have an aisle seat
available!");
printBoardingPass(seatnumber);
}
}
else {
// Print an error message if they did not choose 1, 2, or 0 for
their choice.
System.out.println("Invalid choice made. Please try again!");
choice = 0;
}
// No window or aisle seats were available.
if (seatnumber == -1) {
System.out.println("We are sorry, there are no window or aisle
seats available.");
System.out.println();
}