fork download
  1. import java.util.stream.Stream;
  2. import java.util.stream.DoubleStream;
  3. import java.util.Arrays;
  4. import java.util.function.Function;
  5.  
  6. public class Main {
  7. static final double[] percents = {0.10, 0.15, 0.25, 0.28, 0.33, 0.35};
  8. static final double[] thresholds = {8350, 33950, 82250, 171550, 372950};
  9.  
  10. public static void main(String[] args) {
  11. double[] partialSums = Stream.iterate(0, n -> n + 1).limit(5).mapToDouble(
  12. (i) -> thresholds[i] * (percents[i] - percents[i + 1])
  13. ).toArray();
  14.  
  15. Function<Double, Double> computeTax = (income) -> {
  16. int level = (int)Arrays.stream(thresholds).reduce(0, (x, y) -> x + new Boolean(income > y).compareTo(false));
  17.  
  18. return Arrays.stream(partialSums).limit(level).reduce(0, (x, y) -> x + y) + income * percents[level];
  19. };
  20.  
  21. double tax = computeTax.apply(100000.0);
  22. double controlValue = 8350 * 0.10 + (33950 - 8350) * 0.15 +
  23. (82250 - 33950) * 0.25 + (100000 - 82250) * 0.28;
  24. System.out.println("Tax is " + (int)(tax * 100) / 100.0);
  25. System.out.println("Control value is " + (int)(controlValue * 100) / 100.0);
  26. }
  27. }
  28.  
Success #stdin #stdout 0.19s 37180KB
stdin
Standard input is empty
stdout
Tax is 21720.0
Control value is 21720.0