Question
Q2: [BusRouteSearchEngine Mini-Project] Create a RouteManager class. This class will store instances of the BusRoute class, and support generating a list of potential itineraries. It
Q2: [BusRouteSearchEngine Mini-Project] Create a RouteManager class. This class will store instances of the BusRoute class, and support generating a list of potential itineraries. It should contain one constructor, two instance variables (an array of BusRoutes, and something that counts the number of bus routes in the system), and four methods (addBusRoute, increaseSize, findItineraries, shrinkItineraries).
default constructor: will set up the instances variables with zero bus routes.
addBusRoute(...): Adds a bus route to the bus routes array. Uses the increaseSize() method if the array is full.
increaseSize(): Doubles the size of the bus routes array, while keeping whatever is already there. Make this a private method.
findItineraries(...): Searches the bus routes based on source bus station, destination bus station, and departure time, to find 1-bus route and 2-bus route itineraries meeting those criteria. When searching for 2-bus route itineraries, checks that the bus routes have a connecting city, and that the first arrives in time for the second. Your method should create an array of potential itineraries that can be returned. (If it helps, you may assume that this method will never find more than 100 itineraries.) Use the shrinkItineraries() method to "clean up" up the potential itineraries array; see below. Hint: use loops to search for all the bus routes in the bus routes variable, check if they meet the criteria, and then create a new Itinerary object if the criteria is met.
shrinkItineraries(...). Takes an array of itineraries, where some of the later indices are unused (null), and returns a new array of itineraries where there are no empty indices. Make this a private method.
RouteManager Test:
public class RouteManagerTest { RouteManager manager; public static void main(String[] args) { RouteManagerTest rmt = new RouteManagerTest(); rmt.run(); } public RouteManagerTest() { manager = new RouteManager(); loadBusRoutes(); } public void run() { Itinerary[] itineraries = manager.findItineraries(BusStation.PHX, BusStation.SFO, new Time(8, 0)); for (Itinerary i : itineraries) { System.out.println(i); } } private void loadBusRoutes() { manager.addBusRoute(new BusRoute(new Bus(BusType.Greyhound), 45, new Time(10, 50), 230, BusStation.PHX, BusStation.YUM)); manager.addBusRoute(new BusRoute(new Bus(BusType.Megabus), 46, new Time(11, 5), 360, BusStation.LAX, BusStation.LVS)); manager.addBusRoute(new BusRoute(new Bus(BusType.Greyhound), 75, new Time(8, 20), 440, BusStation.PHX, BusStation.SAN)); manager.addBusRoute(new BusRoute(new Bus(BusType.BoltBus), 50, new Time(7, 10), 192, BusStation.SAN, BusStation.LAX)); manager.addBusRoute(new BusRoute(new Bus(BusType.Megabus), 50, new Time(12, 50), 445, BusStation.LAX, BusStation.SFO)); manager.addBusRoute(new BusRoute(new Bus(BusType.BoltBus), 65, new Time(17, 50), 746, BusStation.SAN, BusStation.SFO)); } }
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;
} }
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started