fork(1) 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, controlValue;
  22.  
  23. tax = computeTax.apply(5000.0);
  24. controlValue = 5000 * 0.10;
  25. System.out.println("Tax is " + (int)(tax * 100) / 100.0);
  26. System.out.println("Control value is " + (int)(controlValue * 100) / 100.0);
  27.  
  28.  
  29. tax = computeTax.apply(10000.0);
  30. controlValue = 8350 * 0.10 + (10000 - 8350) * 0.15;
  31. System.out.println("Tax is " + (int)(tax * 100) / 100.0);
  32. System.out.println("Control value is " + (int)(controlValue * 100) / 100.0);
  33.  
  34.  
  35. tax = computeTax.apply(50000.0);
  36. controlValue = 8350 * 0.10 + (33950 - 8350) * 0.15 +
  37. (50000 - 33950) * 0.25;
  38. System.out.println("Tax is " + (int)(tax * 100) / 100.0);
  39. System.out.println("Control value is " + (int)(controlValue * 100) / 100.0);
  40.  
  41.  
  42. tax = computeTax.apply(100000.0);
  43. controlValue = 8350 * 0.10 + (33950 - 8350) * 0.15 +
  44. (82250 - 33950) * 0.25 + (100000 - 82250) * 0.28;
  45. System.out.println("Tax is " + (int)(tax * 100) / 100.0);
  46. System.out.println("Control value is " + (int)(controlValue * 100) / 100.0);
  47.  
  48.  
  49. tax = computeTax.apply(200000.0);
  50. controlValue = 8350 * 0.10 + (33950 - 8350) * 0.15 +
  51. (82250 - 33950) * 0.25 + (171550 - 82250) * 0.28 +
  52. (200000 - 171550) * 0.33;
  53. System.out.println("Tax is " + (int)(tax * 100) / 100.0);
  54. System.out.println("Control value is " + (int)(controlValue * 100) / 100.0);
  55.  
  56.  
  57. tax = computeTax.apply(500000.0);
  58. controlValue = 8350 * 0.10 + (33950 - 8350) * 0.15 +
  59. (82250 - 33950) * 0.25 + (171550 - 82250) * 0.28 +
  60. (372950 - 171550) * 0.33 + (500000 - 372950) * 0.35;
  61. System.out.println("Tax is " + (int)(tax * 100) / 100.0);
  62. System.out.println("Control value is " + (int)(controlValue * 100) / 100.0);
  63.  
  64. }
  65. }
  66.  
Success #stdin #stdout 0.12s 37124KB
stdin
Standard input is empty
stdout
Tax is 500.0
Control value is 500.0
Tax is 1082.5
Control value is 1082.5
Tax is 8687.5
Control value is 8687.5
Tax is 21720.0
Control value is 21720.0
Tax is 51142.5
Control value is 51142.5
Tax is 152683.5
Control value is 152683.5