Last active
February 21, 2019 13:11
-
-
Save dwickstrom/82a85e9cba49d021b2f9a6fb98c22962 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
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