Created
September 23, 2016 01:17
-
-
Save jjlumagbas/9b9ee6ccdf5f7b0180b4a914d847c9de to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public interface Chargeable { | |
public double cost(); | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | |
} | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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