About This Solution
This is one way to solve the Vacation Calculator assignment. It is not the one and only way.
Expense.java
// The Expense interface
/**
* A common interface for all vacation-related expenses.
*/
public interface Expense {
/**
* @return The cost associated with this expense.
*/
float getCost();
}
Lodging.java
/**
* The Lodging class handles expenses related to staying at our chosen destination.
* <p>
* @author Brother Falin
* @version 1.0
* @since 2016-12-08
* @see Expense
*/
public class Lodging implements Expense {
Destination _dest;
int _totalNights;
/**
* @param dest Where we'll be going on vacation.
* @param nights How long we'll be staying there.
*/
public Lodging(Destination dest, int nights) {
_dest = dest;
_totalNights = nights;
}
/**
* @return The total cost of lodging at our destination.
*/
public float getCost() {
switch (_dest) {
case Mexico:
return 100.00f * _totalNights;
case Europe:
return (200.00f * _totalNights) * 1.10f;
case Japan:
return (300.00f * _totalNights) * 1.30f;
default:
return 0.0f;
}
}
}
Dining.java
/**
* The Dining class handles expenses related to food.
* <p>
* @author Brother Falin
* @version 1.0
* @since 2016-12-08
* @see Expense
*/
public class Dining implements Expense {
Destination _dest;
int _totalNights;
/**
* @param dest Where we'll be going on vacation.
* @param nights How long we'll be staying there.
*/
public Dining(Destination dest, int nights) {
_dest = dest;
_totalNights = nights;
}
/**
* @return The total cost of dining at our destination.
*/
public float getCost() {
switch (_dest) {
case Mexico:
return 10.00f * _totalNights;
case Europe:
return 20.00f * _totalNights;
case Japan:
return 30.00f * _totalNights;
default:
return 0.0f;
}
}
}
Destination.java
// An enum (short for enumeration) allows us to restrict a value
// to a specific list of strongly-typed options.
//
// Note that while enums are found in most languages, you can do
// much more with them in Java than you can in other languages.
// For more details, see: https://docs.oracle.com/javase/tutorial/java/javaOO/enum.html
/**
* Destinations that can be chosen for a vacation.
*/
public enum Destination {
Mexico, Europe, Japan
}
Cruise.java
/**
* The Cruise class handles expenses related to the cruise.
* <p>
* @author Brother Falin
* @version 1.0
* @since 2016-12-08
* @see Expense
*/
public class Cruise implements Expense {
// Store the enum value
Destination _dest;
/**
* @param dest Where we'll be going on vacation.
*/
public Cruise(Destination dest) {
_dest = dest;
}
/**
* @return The cost of the round-trip cruise to our destination.
*/
public float getCost() {
// Enums work great with switch statements
switch (_dest) {
case Mexico:
return 1000.00f;
case Europe:
return 2000.00f;
case Japan:
return 3000.00f;
default:
return 0.0f;
}
}
}
VacationCalculator.java
import java.util.List;
import java.util.ArrayList;
/**
* This class is used to calculate the cost of a vacation based on where we're going,
* how long we'll be there, and what we're planning to do.
* <p>
* @author Brother Falin
* @version 1.0
* @since 2016-12-08
*/
public class VacationCalculator {
/**
* This is the main method we use for testing the vacation code.
* @param args Unused.
*/
public static void main(String[] args) {
// Create an instance of the VacationCalculator class so we
// can access its methods
VacationCalculator vc = new VacationCalculator();
float japanCost = vc.calculateVacationCost(Destination.Japan, 5);
float mexicoCost = vc.calculateVacationCost(Destination.Mexico, 3);
float europeCost = vc.calculateVacationCost(Destination.Europe, 7);
// Note the use of System.out.format here, instead of println
// This allows us to use format specifiers, but requires us to insert the
// new-line (%n) ourselves.
// For details, see:
// https://docs.oracle.com/javase/tutorial/essential/io/formatting.html
System.out.format(String.format("Total cost for trip to Japan: $%.2f%n", japanCost));
System.out.format(String.format("Total cost for trip to Mexico: $%.2f%n", mexicoCost));
System.out.format(String.format("Total cost for trip to Europe: $%.2f%n", europeCost));
}
/**
* This is the main method we use for testing the vacation code.
* @param dest Where we're going on vacation.
* @param totalNights How long we're staying there.
* @return The total cost of the vacation.
*/
public float calculateVacationCost(Destination dest, int totalNights) {
// Build the itenerary. Note that we insantiate an ArrayList, but
// store it as a List. This is another example of "programming
// to an interface."
//
// For a list of classes that implement the List interface,
// see the "All Known Implementing Classes" section of:
// http://docs.oracle.com/javase/8/docs/api/java/util/List.html
List<Expense> itenerary = new ArrayList<Expense>();
itenerary.add(new Cruise(dest));
itenerary.add(new Dining(dest, totalNights));
itenerary.add(new Lodging(dest, totalNights));
float totalCost = tallyExpenses(itenerary);
return totalCost;
}
// The tallyExpenses() function has no idea what kind of expenses these
// are, or how they are related. All it cares about is that they
// all implement the expenses interface, which means they are guaranteed
// to have a getCost() function.
private float tallyExpenses(List<Expense> expenses) {
float totalCost = 0;
for(Expense e : expenses) {
totalCost += e.getCost();
}
return totalCost;
}
}