I only need help with the toOverviewString and toDetailedString. I can\'t get it
ID: 3805211 • Letter: I
Question
I only need help with the toOverviewString and toDetailedString. I can't get it to return properly. This is the sample output and what I have so far. I am not trying to use stringbuilder.
Q5:
Create a BusRoute class that uses the Bus and Time class. This class will represent a bus route between two bus stations, using a specific Bus, and departing at a specific Time. It should contain a constructor, 7 instance variables (bus, bus number, cost, departure, duration, source, destination), and 9 methods (see below). [25 points]
overloaded constructor: Creates a BusRoute object that is setup up with a Bus, a bus number, a cost, a departure Time, a duration time, a source BusStation, and a destination BusStation.
getBus(): Returns the Bus that operates this bn rusoute.
getNumber(): Returns the bus number as a String.
getCost(): Returns the bus route cost.
getDestination(): Returns the destination BusStation.
getDeparture(): Returns the departure Time.
getArrival(): Returns a Time object with the arrival time (computed from the departure time and duration).
getSource(): Returns a Bus Station object for the departure location.
toOverviewString(): Returns a String representing an overview of the bus route. Use NumberFormat to display the price. See the sample output for an example.
toDetailedString(): Returns a String representing the bus route's detailed information. See the sample output for an example.
import java.text.NumberFormat;
public class BusRoute {
private Bus bus;
private String busNumber;
private double cost;
private Time departure;
private int duration;
private BusStation source;
private BusStation destination;
public BusRoute(Bus bus, String busNumber, double cost,
Time departure, int duration, BusStation source,
BusStation destination){
this.bus = bus;
this.busNumber = busNumber;
this.cost = cost;
this.departure = departure;
this.duration = duration;
this.source = source;
this.destination = destination;
}
public Bus getBus(){
return bus;
}
public String getNumber(){
return busNumber;
}
public double getCost(){
return cost;
}
public BusStation getDestination(){
return destination;
}
public Time getDeparture(){
return departure;
}
public Time getArrival(){
Time arrival = departure.getCopy();
arrival.addHours(duration);
return arrival;
}
public BusStation getSource(){
return source;
}
public String toOverviewString(){
NumberFormat fmt1 = NumberFormat.getCurrencyInstance();
}
Thanks in advance! Also I am using eclipse.
Explanation / Answer
class BusRoute{
private Bus bus;
private String number;
private int cost;
private Time departure;
private int duration;
private BusStation source;
private BusStation destination;
public BusRoute(Bus bus,String busNumber, int cost, Time time, int duration,BusStation source, BusStation destination){
this.setBus(bus);
this.setNumber(busNumber);
this.setCost(cost);
this.setDeparture(time);
this.setDuration(duration);
this.setSource(source);
this.setDestination(destination);
}
public Bus getBus() {
return bus;
}
public void setBus(Bus bus) {
this.bus = bus;
}
public String getNumber() {
return number;
}
public void setNumber(String number) {
this.number = number;
}
public int getCost() {
return cost;
}
public void setCost(int cost) {
this.cost = cost;
}
public Time getDeparture() {
return departure;
}
public void setDeparture(Time departure) {
this.departure = departure;
}
public Time getArrival() {
int time = departure.getHour()*60 + departure.getMinute();
time = time + duration;
return new Time(time/60,time%60);
}
public int getDuration() {
return duration;
}
public void setDuration(int duration) {
this.duration = duration;
}
public BusStation getSource() {
return source;
}
public void setSource(BusStation source) {
this.source = source;
}
public BusStation getDestination() {
return destination;
}
public void setDestination(BusStation destination) {
this.destination = destination;
}
public String toDetailedString(){
String str = "";
str += getDeparture()+" - "+getArrival()+" "+getSource()+" - "+getDestination()+" "+getBus().getBusType()+" "+getNumber()+" ";
return str;
}
public String toOverviewString(){
String str = "";
int h = duration/60;
int m = duration%60;
str += "$"+getCost()+" "+getDeparture()+" - "+getArrival()+" "+h+":"+m+" Hours "+getBus().getBusType()+" "+getSource()+" - "+getDestination()+" ";
return str;
}
}