ALLLLLL ABOARD! Buy your tickets right here for the DIC train of love. In this episode we talk about a problem that pops up once in awhile on the boards, a seat reservation system. People have their preferences and the DIC train is no different. Some people like the windows, some people like the aisle seats and some even like to hang out there in the caboose (Yeah you know who you are). We talk about how to setup a basic system for reserving window and aisle seats and knowing when a seat type is available. We do all this in a simple little Java program. So kick back and enjoy the ride on this entry of the Programming Underground!
Questions about a train reservation system (sometimes its a bus or a plane) has appeared a few times on the boards in the last couple of months and I thought I would do all those students out there a favor by putting together a simple little example in Java for them. The classic example involves asking the user to input whether or not they want a window or aisle seat and knowing when the train is out of that seat type or when it is full altogether.
There are many methods to solving this problem and this is one example of it. At the heart of it is two simple methods that loop through an array of seats. These two functions check a certain number of array subscripts to see if any spots are available, and if there is, it marks it as taken. One function takes the first half of the array and the second function takes the second half of the array. These functions then return a seat number if one has been booked or a -1 if the seat type is taken. Nothing fancy, nothing too out there or hard to grasp. It is a great example of breaking a problem down into some simple understandable helper functions… the goal for any complex programming problem.
So lets take a look at what we have put together for this….
import java.util.Scanner; import java.util.Date; public class reservation { // Create an array of 12 seats, 6 window and 6 aisle. private static int[] seats = new int[12]; public static void main(String args[]) { System.out.println("Welcome to the DIC lovin train reservation system!"); System.out.println("Code ninjas, code newbies, one fabulous DIC ride!"); 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 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 in luck, 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(); } // Reprompt for a choice System.out.print("Please enter 1 for window, 2 for aisle, or 0 to exit: "); choice = s.nextInt(); } } // This function checks for window seats and returns seat number or -1 if full. private static int bookWindow() { for (int i = 0; i < 6; i++) { if (seats[i] == 0) { seats[i] = 1; return i + 1; } } return -1; } // This function checks to see if aisle seats were available, -1 if full. private static int bookAisle() { for (int i = 6; i < 12; i++) { if (seats[i] == 0) { seats[i] = 1; return i + 1; } } return -1; } // This simply prints out a nice little boarding pass message with their seat number and date of issue. private static void printBoardingPass(int seatnumber) { Date timenow = new Date(); System.out.println(); System.out.println("Date: " + timenow.toString()); System.out.println("Boarding pass for seat number: " + seatnumber); System.out.println("This ticket is non-refundable and non-transferable."); System.out.println("Please be curteous, do not smoke. Enjoy your trip."); System.out.println(); } }
The first step we do is import a scanner class for taking in some input and another for using the Date in our printBoardingPass function. We then start our class with a private static array called “seats” which holds both our window and aisle seat assignments. In the main function we then initialize these seats to a value of 0, meaning that they are empty. Whenever we want to book a seat, we will put the value 1 in that spot of the array. You could use letters or other numbers or whatever. You could even keep the array null and only put numbers in it to mark them as taken. As long as you can check them later to see if they have been marked as taken already or not.
We continue by prompting the user for a choice. 1 if they have a window preference, 2 if they have an aisle preference or 0 to exit. We use this choice to determine how we go about booking a seat. This choice and booking mechanism is in a while loop that will continue until they type 0.
If the user chose a window seat preference, we call our bookWindow() function to see if we get a seat assignment or -1 indicating that no window seats are available. If there was a seat, we go and print out a boarding pass by calling printBoardingPass(). If there were no seats, we attempt to book them an aisle seat and let them know a window seat was not available.
We do a similar procedure for if they chose an aisle seat. If none are available, we attempt to book them a window seat. In either case we print out a boarding pass for them.
Both of these functions are very similar. They loop through looking for a empty spot in the array (one marked as zero) and if it finds one, it changes it to 1 and returns the seatnumber (remember to add one to the index since the index starts at 0 in the array and array item 0 is seat number 1). If for some reason we were not able to find a window or an aisle seat (despite which choice they made to begin with) then seatnumber will be -1. We can then inform them that no window or aisle seats are available. They can then exit the program at that time.
The main bulk of the program is decision statements checking if seats are available and booking alternatives. After a few if-then statements and you can easily see how booking seats using an array is not too bad and now I am sure you can share the love with all your classmates. Perhaps if you ask skyhawk133 for a “I rode the DIC train” T-shirt he might be able to pull some strings and make it happen.
Thanks for reading!