Skip to content

Instantly share code, notes, and snippets.

@dwickstrom
Last active February 21, 2019 13:11
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save dwickstrom/82a85e9cba49d021b2f9a6fb98c22962 to your computer and use it in GitHub Desktop.
Save dwickstrom/82a85e9cba49d021b2f9a6fb98c22962 to your computer and use it in GitHub Desktop.
class Movie {
enum Type {
REGULAR(PriceService::getRegularPrice),
NEW_RELEASE(PriceService::getNewReleasePrice),
CHILDREN(PriceService::getChildrensPrice);
public final BiFunction<PriceService, Integer, Double> priceAlgo;
private Type(BiFunction<PriceService, Integer, Double> priceService) {
this.priceAlgo = priceAlgo;
}
}
private final Type type;
public Movie(Type type) {
this.type = type;
}
public double computePrice(int days) {
return type.computePrice(days)
}
}
class PriceService {
public double getRegularPrice(int days) {
return days * 2;
}
public double getNewReleasePrice(int days) {
return days * 3;
}
public double getChildrensPrice(int days) {
return days * 1.5;
}
public double computePrice(Movie.Type type, int days) {
return type.priceAlgo.apply(this, days);
}
}
class Main {
public static void main(String[] args) {
PriceService priceService = new PriceService();
System.out.println(priceService.computePrice(Movie.Type.REGULAR, 2));
System.out.println(priceService.computePrice(Movie.Type.NEW_RELEASE, 2));
System.out.println(priceService.computePrice(Movie.Type.CHILDREN, 2));
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment