Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

Q2: Add functionality to search for an Itinerary. Create labels and text fields that allow the user to enter a bus type, a source bus

Q2: 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() : converts time string 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.

BusType:

public enum BusType { Greyhound, BoltBus, Megabus }

BusStation:

import java.util.Scanner;

public enum BusStation { PHX, LAX, LVS, SAN, SFO, YUM; public static String getBusStationCity(BusStation busStation) {

switch (busStation) { case PHX: return "Phoenix"; case LAX: return "Los Angeles"; case LVS: return "Las Vegas"; case SAN: return "San Diego"; case SFO: return "San Francisco"; default: return "Unknown City"; } } }

Bus:

public class Bus { private BusType busType;

public Bus(BusType busType) { super(); this.busType = busType; }

public BusType getBusType() { return this.busType; }

@Override public String toString() { return "Bus [busType=" + busType + "]"; }

public static void main(String[] args) { Bus bus = new Bus(BusType.Greyhound); System.out.println(bus.toString()); }

}

Time:

public class Time {

//Instance variables private int hour; private int minute; /** * Default constructor */ public Time() { this.hour = 0; this.minute = 0; }

/** * Parameterized constructor * @param hour * @param minute */ public Time(int hour, int minute) { this.hour = hour; this.minute = minute; }

/** * @return the hour */ public int getHour() { return hour; }

/** * @return the minute */ public int getMinute() { return minute; } /** * Updates the object by moving it forward a number of hours. * @param hour */ public void addHours(int hour) { this.hour += hour; } /** * Updates the object by moving it forward a number of minutes. * @param minute */ public void addMinutes(int minute) { this.minute += minute; while (this.minute > 60) { this.minute -= 60; this.hour += 1; } } /** * Updates the object by moving it forward by the hour and minute from another Time object. */ public void addTime(Time another) { addMinutes(another.getMinute()); addHours(another.getHour()); } /** * Returns a new Time object that has the same hour and minute of the existing Time object. */ public Time getCopy() { Time time = new Time(); time.hour = this.hour; time.minute = this.minute; return time; } /** * Returns true if this Time object is earlier than another Time object. */ public boolean isEarlierThan(Time another) { if(this.hour < another.hour) return true; else if((this.hour == another.hour) && (this.minute < another.minute)) return true; return false; } /** * Returns true if this Time object is the same as another Time object. */ public boolean isSameTime(Time another) { if((this == another) || ((this.hour == another.hour) && (this.minute == another.minute))) { return true; } return false; } /** * Returns true if this Time object is later than another Time object. */ public boolean isLaterThan(Time another) { return (!isEarlierThan(another) && !isSameTime(another)); }

/** * Returns a string representing the Time object. * Uses 12 hour AM/PM format and pads minutes to be two digits. See the sample output for an example. */ public String toString() { return ((this.hour > 12) ? (this.hour - 12) : this.hour) + ":" + ((this.minute < 10) ? ("0" + this.minute) : this.minute) + ((this.hour >= 12) ? " PM" : " AM"); } }

BusRoute:

import java.text.NumberFormat;

public class BusRoute { //Instance variables Bus bus; String busNum; double cost; Time departure; int duration; BusStation sourceBusStation; BusStation destinationBusStation;

public BusRoute(Bus bus, String busNum, double cost, Time departure, int duration, BusStation sourceBusStation, BusStation destinationBusStation) { this.bus = bus; this.busNum = busNum; this.cost = cost; this.departure = departure; this.duration = duration; this.sourceBusStation = sourceBusStation; this.destinationBusStation = destinationBusStation; } public Bus getBus() { return bus; } public String getNumber() { return busNum; } public double getCost() { return cost; } public BusStation getDestinationBusStation() { return destinationBusStation; } public Time getDeparture() { return departure; } public Time getArrival() { Time arrivalTime = new Time(departure.getHour(), departure.getMinute()); arrivalTime.addMinutes(this.duration); return arrivalTime; } public BusStation getSourceBusStation() { return sourceBusStation; } public String toOverviewString() { NumberFormat nf = NumberFormat.getCurrencyInstance(); String output = nf.format(cost) + " " + getDeparture() + " - "; output += getArrival() + "\t" + duration / 60 + "h:" + duration % 60 + "m"; output += " " + bus.getBusType() + "\t\t" + sourceBusStation + "-" + destinationBusStation; return output; }

public String toDetailedString() { String output = getDeparture() + " - " + getArrival() + " " + BusStation.getBusStationCity(sourceBusStation) + " (" + sourceBusStation + ") - " + BusStation.getBusStationCity(destinationBusStation) + " (" + destinationBusStation + ")" + " " + bus.getBusType() + " " + busNum; return output;

} }

Itinerary:

package bus;

class Itinerary{ private BusRoute firstBusRoute; private BusRoute secondBusRoute; public Itinerary(BusRoute firstBusRoute) { this.firstBusRoute = firstBusRoute; this.secondBusRoute = null; } public Itinerary(BusRoute firstBusRoute, BusRoute secondBusRoute) { this.firstBusRoute = firstBusRoute; this.secondBusRoute = secondBusRoute; }

/** * @return the firstBusRoute */ public BusRoute getFirstBusRoute() { return firstBusRoute; }

/** * @return the secondBusRoute */ public BusRoute getSecondBusRoute() { return secondBusRoute; }

/** * * @return returns true if the itinerary contains two BusRoute objects. */ public boolean hasConnection(){ return firstBusRoute!=null && secondBusRoute!=null; } /** * * @return total cost of the routes */ public double getTotalCost(){ double totalCost=0.0; if(firstBusRoute!=null){ totalCost+=firstBusRoute.getCost(); } if(secondBusRoute!=null){ totalCost+=secondBusRoute.getCost(); } return totalCost; } /** * * @return departure of first route */ public Time getDeparture(){ return firstBusRoute.getDeparture(); } /** * * @return arrival of second route */ public Time getArrival(){ return secondBusRoute.getArrival(); } /** * return details related to itinerary */ @Override public String toString() { String info = "Total cost : "+getTotalCost()+" "; info+="First Bus Route Details : ---------------- "+firstBusRoute.toDetailedString()+" "; if(secondBusRoute!=null){ info+=" Second Bus Route Details : -------------- "+secondBusRoute.toDetailedString(); } return info; } }

RouteManager:

package bus;

import java.text.NumberFormat; public class RouteManager { BusRoute[] routes; int size; public RouteManager() { size = 0; routes = new BusRoute[size]; } public void addBusRoute(BusRoute busRoute) { if (size == routes.length) increaseSize(); routes[size] = busRoute; size = size + 1; } private void increaseSize() { BusRoute[] tmp; if (size == 0) tmp = new BusRoute[1]; else tmp = new BusRoute[2 * routes.length]; System.arraycopy(routes, 0, tmp, 0, routes.length); this.routes = tmp; } public String[] findItineraries(BusStation sourceStation, BusStation destinationStation, Time departure) { String[] potentialItineraries = new String[100]; //itineraries stored as strings int noOfItineraries = 0; for (int i = 0; i < size; i++) { //find all 1-route if source ,destination and departure match if (routes[i].getSourceBusStation().equals(sourceStation) && routes[i].getDestinationBusStation().equals(destinationStation) && routes[i].getDeparture().isSameTime(departure)) { String itinerary = "1-Bus Route "; itinerary += routes[i].toDetailedString() + " ................................."; if (noOfItineraries < 100) { //if itineraries >100 skip potentialItineraries[noOfItineraries] = itinerary; noOfItineraries++; } } for (int j = 0; j < size; j++) { //find all 2 routes. if (i != j && routes[i].getSourceBusStation().equals(sourceStation) && routes[j].getDestinationBusStation().equals(destinationStation) && routes[i].getDeparture().isSameTime(departure) && (routes[i].getArrival().isEarlierThan(routes[j].getDeparture()))) { String itinerary = "2-Bus Route "; itinerary += routes[i].toDetailedString() + " " + routes[j].toDetailedString() + " ............................"; if (noOfItineraries < 100) { potentialItineraries[noOfItineraries] = itinerary; noOfItineraries++;

} } }

} String[] shrinkedItineraries = shrinkItineraries(potentialItineraries, noOfItineraries); return shrinkedItineraries; } private String[] shrinkItineraries(String[] potential, int length) { String[] temp = new String[length]; //create a new array of length 'length' System.arraycopy(potential, 0, temp, 0, length); //copy to new array and return return temp;

} public static void main(String[] args) { RouteManagerTest rmt = new RouteManagerTest(); rmt.run(); } }

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Students also viewed these Databases questions

Question

Explain two criticisms of the DSM.

Answered: 1 week ago