Q1: [BusRouteSearchEngine Mini-Project] Create an Itinerary class. This class wi
ID: 3807379 • Letter: Q
Question
Q1: [BusRouteSearchEngine Mini-Project] Create an Itinerary class. This class will represent a travel itinerary containing one or two bus routes in our system. It should contain two constructors, two instance variables (two different BusRoutes) and seven methods (getFirstBusRoute, getSecondBusRoute, hasConnection, getTotalCost, getDeparture, getArrival, toString).
overloaded constructor: will take one BusRoute object and use it to set up the instance variables. The second BusRoute variable should be set to null.
overloaded constructor: will take two BusRoute objects and use them to set up the instance variables.
getFirstBusRoute (): returns the first BusRoute in the itinerary.
getSecondBusRoute(): returns the second BusRoute in the itinerary.
hasConnection(): returns true if the itinerary contains two BusRoute objects.
getTotalCost(): returns the total cost of all BusRoutes in the itinerary.
getDeparture(): returns the departure time of the first bus route in the itinerary.
getArrival(): returns the arrival time of the last bus route in the itinerary.
toString(): will return the total cost of the itinerary and the "detailed string" for the BusRoute or BusRoutes in the itinerary.
Itinery Test:
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() + " " + duration / 60 + "h:" + duration % 60 + "m";
output += " " + bus.getBusType() + " " + sourceBusStation + "-" + destinationBusStation;
return output;
}
public String toDetailedString() {
String output =
getDeparture() + " - " + getArrival() + " " + BusStation.getBusStationCity(sourceBusStation) + " (" +
sourceBusStation + ") - " + BusStation.getBusStationCity(destinationBusStation) + " (" + destinationBusStation + ")" +
" " + bus.getBusType() + " " + busNum;
return output;
}
}
Explanation / Answer
public class ItineraryTest {
public static void main(String[] args) {
BusRoute b1 = new BusRoute(new Bus(BusType.Greyhound),"21", 75, new Time(8, 20), 440, BusStation.PHX,
BusStation.SAN);
BusRoute b2 = new BusRoute(new Bus(BusType.BoltBus),"22", 65, new Time(17, 50), 746, BusStation.SAN, BusStation.SFO);
Itinerary i1 = new Itinerary(b1);
Itinerary i2 = new Itinerary(b1, b2);
System.out.println(i1);
System.out.println();
System.out.println(i2);
}
}
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;
}
}
-------------------------------------------------output----------------------------------------------------------
Total cost : 75.0
First Bus Route Details :
----------------
8:20 AM - 3:40 PM
Phoenix (PHX) - San Diego (SAN)
Greyhound 21
Total cost : 140.0
First Bus Route Details :
----------------
8:20 AM - 3:40 PM
Phoenix (PHX) - San Diego (SAN)
Greyhound 21
Second Bus Route Details :
--------------
5:50 PM - 18:16 PM
San Diego (SAN) - San Francisco (SFO)
BoltBus 22
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.