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

In this assignment you will continue the Train Route Search Engine mini-project

ID: 3847155 • Letter: I

Question

In this assignment you will continue the Train Route Search Engine mini-project by creating a small user interface application. The classes you create must be compatible with either the classes for the past two assignments (the ones you submitted last week and the week before), or our posted solution. Please indicate which in a comment at the top of the classes. Hint: If you have NetBeans installed, then you can approach this homework by using the Design tab. With the Design tab, you can create your UI by dragging and dropping the swing controls. Once you are done with the UI, you can copy/paste your source code into a new Java file to work on for the remaining tasks. Please see the "Creating Simple GUI in NetBeans" video under Week 4 for a quick example on how to create a user interface in NetBeans using drag and drop.

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 "Train 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]

Q2: Add functionality to search for an Itinerary.

Create labels and text fields that allow the user to enter a train type, a source train station, and a destination train station, as well as the departure time and the arrival time to search for one or more available train routes in an Itinerary. Also, create a ComboBox (see Chapter 11) that will allow the user to select from a list of train 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 TrainFrame 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() : converts time string to a Time object and returns the Time object

checkValidTrainType() : returns true if entered train type is valid and exists in TrainType enum and can be converted to a TrainType object

checkValidTrainStations(String src, String dest) : will return true if both the source and destination train stations entered in the text fields exist in TrainStation 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 train type they entered is not in the TrainType enum, then the option pane should say, "Train type unavailable. Please choose a different train type." If the train station they entered is not in the TrainStation enum, then the option pane should say, "Unknown city." Make sure the times are in hh:mm a format. [8 pts]

Q3: 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, "Train route search successful!" The frame should open a new JFrame with the title, "Train Route Information," and a size of 475 by 500 pixels. This frame has the train route information displayed in a JTextArea. [6 pts]

Q4: In the RouteFrame class, you will need to display information for each TrainRoute in the Itinerary [7 points].

The addTo() method shown in the UML diagram is used to add information to the JTextArea for both one-way and connecting train routes (when applicable). It takes in a String val, which is the string of information that needs to be appended to the JTextArea. (For instance, if you would like to append the String "Train", that is the String that needs to be passed in inside your addTo() method call in the RouteFrame class). You need to display the following information in a JTextArea:

Train Type: Display the train type

Departure Train Station: Display the source train station

Departure City: Display the departure city based on the departure train station (use the getTrainStationCity() method you implemented in the Unit 2/3 assignment to get the train station city)

Destination Train Station: Display the destination train station

Destination City: Display the destination city based on the destination train station (use the getTrainStationCity() method again)

Departure Time: Display the departure time

Arrival Time: Display the arrival time

Train Number : Display the train number

Cost: Display the total cost, which should be computed by the Itinerary object.

Use the UML diagram below as a guide on what methods and variables you need to create:

Note: This UML diagram does not include UI controls and action and change listeners, which are GUI components that are also needed for this project.

Attached is a base file (TrainFrame.java) that will help you organize your code, show you where the UI code needs to be declared and placed in your file, what listeners are needed, and and includes method signatures for each required methods.

When you run your code, your UI should look similar to the output below. Your GUI may look slightly different due to the positioning of elements, or the visual scheme of your computer.

Explanation / Answer

question 1

import javax.swing.JFrame;  
import javax.swing.JLabel;
import javax.swing.JSlider;
import javax.swing.JPanel;
import java.awt.Font;
import java.awt.Container;
import javax.swing.event.ChangeEvent;
import javax.swing.event.ChangeListener;
import java.awt.Color;
public class TrainReservation extends JFrame implements ChangeListener
{
   JPanel panel;
   JSlider slider;
   JLabel label;
   Container c;
   TrainReservation(){
   setSize(475,500);
   setVisible(true);
   setTitle("Train Reservation");
   setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

c=getContentPane();

panel =new JPanel();

label=new JLabel("Choose Color");panel.add(label);
label.setFont(new Font("Arial",Font.BOLD,50));
slider=new JSlider(JSlider.HORIZONTAL, 0, 255, 10);
slider.setMinorTickSpacing(10);
slider.setMajorTickSpacing(50);
  
slider.setPaintTicks(true);
slider.setPaintLabels(true);
panel.add(slider);
c.add(panel);
validate();
slider.addChangeListener(this);
   }
public void stateChanged(ChangeEvent ce)
   {
   int pos=slider.getValue();
panel.setBackground(new Color(pos,pos,pos));//panel background color
   // label.setForeground(new Color(pos,pos,pos));//slider label color
   }
   public static void main(String args[]){
   new TrainReservation();
   }

}