FP – calculate the invoice price Invoice Price = (markedPrice * (100 – discountRate)/100) + deliveryCharge Example
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 | package com.fp.eg3; import java.util.function.Function; public class DiscountCalcFPMain { public static void main(String[] args) { double markedPrice = 300.00; double discountRate = 10.00 ; //10% double deliveryCharge = 50.00; DiscountCalaculator dc = new DiscountCalaculator(); Function<Double, Double> f = dc.currySecondArg((100 - discountRate)/100); Function<Double, Double> g = x -> x + deliveryCharge; Function<Double, Double> h = f.andThen(g); System.out.println("Invoice Price = " + h.apply(markedPrice)); // 320.00 } } |
“f.andThen(g)” is the composition of functions. This can be described as g(f()) where f() is 270.0 (i.e. 300.00 * (100 – 10)/100),…