Create a class hierarchy that will represent trips, including member variables to store trip details. Create member functions that will:
Study the sample output carefully so you can correctly implement the trip hierarchy.
Input
5
1·Zamboanga·Cagayan·494·5
1·Tagbilaran·Panglao·20·10
2·Cebu·Tagbilaran·90·800
3·Manila·Davao·1500·4500
3·Clark·Iloilo·751·2300
Output
Trip·Constructor
Bus·Constructor
Bus·Trip:·Zamboanga·to·Cagayan,·494·km,·P5·base·fare
Trip·Constructor
Bus·Constructor
Bus·Trip:·Tagbilaran·to·Panglao,·20·km,·P10·base·fare
Trip·Constructor
Boat·Constructor
Boat·Trip:·Cebu·to·Tagbilaran,·90·km,·P800·base·fare
Trip·Constructor
Plane·Constructor
Plane·Trip:·Manila·to·Davao,·1500·km,·P4500·base·fare
Trip·Constructor
Plane·Constructor
Plane·Trip:·Clark·to·Iloilo,·751·km,·P2300·base·fare
Total·Cost·of·trips:·10950
import java.util.*;
class Trip {
private String origin;
private String destination;
protected int distance;
protected int cost;
public Trip() {
System.out.println("Trip Constructor");
}
public Trip(String origin, String destination, int distance, int cost) {
this.origin = origin;
this.destination = destination;
this.distance = distance;
this.cost = cost;
}
public String toString() {
return this.origin + " to " + this.destination + ", " + this.distance + " km, P" + this.getCost()
+ " base fare";
}
/**
* @return the cost
*/
public int getCost() {
return cost;
}
public int computeCostTrip() {
return cost;
}
}
class Bus extends Trip {
public Bus() {
System.out.println("Bus Constructor");
}
public Bus(String origin, String destination, int distance, int cost) {
super(origin, destination, distance, cost);
}
public String toString() {
System.out.println("Trip Constructor");
System.out.println("Bus Constructor");
return "Bus Trip: " + super.toString();
}
@Override
public int computeCostTrip() {
return cost * distance;
}
}
class Boat extends Trip {
public Boat() {
System.out.println("Bus Constructor");
}
public Boat(String origin, String destination, int distance, int cost) {
super(origin, destination, distance, cost);
}
public String toString() {
System.out.println("Trip Constructor");
System.out.println("Boat Constructor");
return "Boat Trip: " + super.toString();
}
@Override
public int computeCostTrip() {
return super.computeCostTrip();
}
}
class Plane extends Trip {
public Plane() {
System.out.println("Plane Constructor");
}
public Plane(String origin, String destination, int distance, int cost) {
super(origin, destination, distance, cost);
}
public String toString() {
System.out.println("Trip Constructor");
System.out.println("Plane Constructor");
return "Plane Trip: " + super.toString();
}
@Override
public int computeCostTrip() {
return super.computeCostTrip();
}
}
class App {
public static void main(String[] args) {
Scanner in = new Scanner(System.in);
int n = in.nextInt();
Trip[] trips = new Trip[n];
int totalCostTrips = 0;
in.nextLine();
for (int i = 0; i < n; i++) {
String values[] = in.nextLine().split(" ");
int type = Integer.parseInt(values[0]);
String origin = values[1];
String destination = values[2];
int distance = Integer.parseInt(values[3]);
int cost = Integer.parseInt(values[4]);
if (type == 1) {
trips[i] = new Bus(origin, destination, distance, cost);
}
if (type == 2) {
trips[i] = new Boat(origin, destination, distance, cost);
}
if (type == 3) {
trips[i] = new Plane(origin, destination, distance, cost);
}
}
for (int i = 0; i < n; i++) {
System.out.println(trips[i].toString());
totalCostTrips += trips[i].computeCostTrip();
}
System.out.println("Total Cost of trips: " + totalCostTrips);
in.close();
}
}
Comments
Leave a comment