Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Q1: The first part of the assignment involves creating very basic GUI controls:

ID: 3818700 • Letter: Q

Question

Q1: The first part of the assignment involves creating very basic GUI controls: a frame, panel, and slider.

Create a JFrame. Add a title to the frame called "Bus Route Reservation". Set the frame size to 475 by 500 pixels. Create a JPanel inside of the frame. You will be adding all of your UI components to this JPanel. [6 pts]

Inside of the panel, create a label that says "Choose Color." Right below it, make a slider that changes the background color of the panel when the slider is moved to a particular color. The slider values should be RGB color values from 0 to 255 (or black to white). Assume that the RGB values for red, green, and blue are the same value - for instance: (240, 240, 240). The slider label's text should be replaced with the RGB value that the slider is currently on. [8 pts]

Add functionality to search for an Itinerary.

Create labels and text fields that allow the user to enter a bus type, a source bus station, and a destination bus station, as well as the departure time and the arrival time to search for one or more available bus routes in an Itinerary. Also, create a ComboBox (see Chapter 11) that will allow the user to select from a list of bus route Itineraries. Each item in the ComboBox represents an Itinerary element, e.g. the first comboBox element is the first Itinerary in the array. [15 pts]

Methods:

Your BusFrame class should include the following methods (The methods that check for validation must have message dialogs indicating to the user if the information entered in each text field is invalid):

initComponents() : Method used to initialize UI components and controls

default constructor : Inside the default constructor, call the initComponents() method

convertTime(String input) : converts time string (from text input -- ie departure time text field) to a Time object and returns the Time object

checkValidBusType() : returns true if entered bus type is valid and exists in BusType enum and can be converted to a BusType object

checkValidBusStations(String src, String dest) : will return true if both the source and destination bus stations entered in the text fields exist in BusStation enum

checkValidTime(String depTime, String arriveTime) : checks if the time is in valid format (hh:mm a) and returns true if it is in the correct format

Make sure all text fields have error checking and exception handling for invalid input. For example, if the user enters an integer as a departure time instead of a String (ie 1200 instead of 12:00 PM), a JOptionPane error message should appear stating, "Incorrect format for departure time." If the bus type they entered is not in the BusType enum, then the option pane should say, "Bus type unavailable. Please choose a different bus type." If the bus station they entered is not in the BusStation enum, then the option pane should say, "Unknown city." Make sure the times are in hh:mm a format. [8 pts]

Create a button that says Search and a button that says View. When the user clicks Search, the combobox will get populated. When the user clicks View, if all the fields are filled out and have valid input, a JOptionPane with a message dialog should appear stating, "Bus route search successful!" The frame should open a new JFrame with the title, "Bus Route Information," and a size of 475 by 500 pixels. This frame has the bus route information displayed in a JTextArea. [6 pts]

Explanation / Answer

1 import java.util.Scanner; 2 import java.util.Date; 3 4 public class reservation { 5 6     // Create an array of 12 seats, 6 window and 6 aisle. 7     private static int[] seats = new int[12]; 8 9     public static void main(String args[]) { 10         System.out.println("Welcome to the DIC lovin train reservation system!"); 11         System.out.println("Code ninjas, code newbies, one fabulous DIC ride!"); 12         System.out.println(); 13           14 15         // Lets start by setting all seats equal to 0 (aka Empty) 16         for (int i = 0; i < 12; i++) { 17             seats[i] = 0; 18         } 19 20         // Setup our scanner and default the choice to window. 21         Scanner s = new Scanner(System.in); 22         int choice = 1; 23 24         // Ask user for a window or an aisle seat and store their coice. 25         System.out.print("Please enter 1 for window, 2 for aisle, or 0 to exit: "); 26         choice = s.nextInt(); 27 28 29         // While their choice is not the one for exit, execute our booking. 30         while (choice != 0) { 31             int seatnumber = 0; 32 33 34             // If they chose a window seat, attempt to book it. 35             if (choice == 1) { 36                 seatnumber = bookWindow(); 37 38 39                 // No window seats available, try booking an aisle seat for them instead. 40                 if (seatnumber == -1) { 41                     seatnumber = bookAisle(); 42                   43                     if (seatnumber != -1) { 44                         System.out.println("Sorry, we were not able to book a window seat. But do have an aisle seat."); 45                         printBoardingPass(seatnumber); 46                     } 47                 } 48                 else { 49                     // Booking a window seat was successful. 50                     System.out.println("You are in luck, we have a window seat available!"); 51                     printBoardingPass(seatnumber); 52                 } 53             } 54             else if (choice == 2) { 55 56                 // If they chose booking an isle, check to see if it is available. 57                 seatnumber = bookAisle(); 58               59                 // If not available, see if we have window seats available. 60                 if (seatnumber == -1) { 61                     seatnumber = bookWindow(); 62 63                     if (seatnumber != -1) { 64                         System.out.println("Sorry, we were not able to book an aisle seat. But do have a window seat."); 65                         printBoardingPass(seatnumber); 66                     } 67                 } 68                 else { 69                     // Booking an aisle seat was successful. 70                     System.out.println("You are in luck, we have an aisle seat available!"); 71                     printBoardingPass(seatnumber); 72                 } 73             } 74             else { 75                 // Print an error message if they did not choose 1, 2, or 0 for their choice. 76                 System.out.println("Invalid choice made. Please try again!"); 77                 choice = 0; 78             } 79 80 81             // No window or aisle seats were available. 82             if (seatnumber == -1) { 83                 System.out.println("We are sorry, there are no window or aisle seats available."); 84                 System.out.println(); 85             } 86 87 88             // Reprompt for a choice 89             System.out.print("Please enter 1 for window, 2 for aisle, or 0 to exit: "); 90             choice = s.nextInt(); 91         } 92 93               94     } 95 96 97     // This function checks for window seats and returns seat number or -1 if full. 98     private static int bookWindow() { 99         for (int i = 0; i < 6; i++) { 100             if (seats[i] == 0) { 101                 seats[i] = 1; 102                 return i + 1; 103             } 104         } 105         return -1; 106     } 107 108 109     // This function checks to see if aisle seats were available, -1 if full. 110     private static int bookAisle() { 111         for (int i = 6; i < 12; i++) { 112             if (seats[i] == 0) { 113                 seats[i] = 1; 114                 return i + 1; 115             } 116         } 117         return -1; 118 119     } 120 121 122     // This simply prints out a nice little boarding pass message with their seat number and date of issue. 123     private static void printBoardingPass(int seatnumber) { 124         Date timenow = new Date(); 125         System.out.println(); 126         System.out.println("Date: " + timenow.toString()); 127         System.out.println("Boarding pass for seat number: " + seatnumber); 128         System.out.println("This ticket is non-refundable and non-transferable."); 129         System.out.println("Please be curteous, do not smoke. Enjoy your trip.");