Skip to content

Instantly share code, notes, and snippets.

@jjlumagbas
Created September 23, 2016 01:17
Show Gist options
  • Save jjlumagbas/9b9ee6ccdf5f7b0180b4a914d847c9de to your computer and use it in GitHub Desktop.
Save jjlumagbas/9b9ee6ccdf5f7b0180b4a914d847c9de to your computer and use it in GitHub Desktop.
public class BoatRide implements Chargeable {
private double baseFare;
private double distance;
public BoatRide(double baseFare, double distance) {
this.baseFare = baseFare;
this.distance = distance;
}
public double cost() {
return baseFare * distance;
}
}
// IS-A
public interface Chargeable {
public double cost();
}
public class PlaneRide implements Chargeable, Comparable<PlaneRide> {
private double fare;
private double discount;
public PlaneRide(double fare, double discount) {
this.fare = fare;
this.discount = discount;
}
public double cost() {
return fare - (fare * discount);
}
public int compareTo(PlaneRide o) {
return fare > o.fare ? 1 : (fare < o.fare ? -1 : 0);
/*
if (fare > o.fare) {
return 1;
} else if (fare < o.fare) {
return -1;
} else {
return 0;
}
*/
}
public String toString() {
return String.valueOf(fare);
}
}
public class TravelItinerary {
public static double cost(Chargeable[] items) {
double total = 0;
for (Chargeable item : items) {
total += item.cost();
}
return total;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment