package Strategy.Holding; public class PriceChangerLambdas { // using lambda expressions with the PriceChanger interface // can use lambdas with any interface that requires only one method public static void main(String[] args) { // use lambda expression to create one price changer // variation(double x) returns x * .3 PriceChanger p = x -> x * .3; // use that pricechanger's variation method System.out.println(p.variation(9)); // whole array of price changers created with lambda expressions PriceChanger options[] = { p, x -> Math.random() -.5, x -> Math.random() * x - (x/2), x -> Math.random() * (x/2) -(x-4), x -> Math.random() * (x/10), x -> Math.random() * 1.25 -.5 }; // result of each pricechanger in the array on a value for (PriceChanger pc : options) { double value = 100; value += pc.variation(value); System.out.println(value); } // use a random priceChanger from the array double value = 100; value += options[(int)(Math.random() * options.length)].variation(value); // if we had our Holding class with a nice parameterized constructor // make one with a random PriceChanger Holding h = new Holding(10, 100, .01, false, options[(int)(Math.random() * options.length)]); } }