Notice: This page requires JavaScript to function properly.
Please enable JavaScript in your browser settings or update your browser.
Apprendre Challenge: Income Tax Calculation | Fundamentals and Functional Capabilities of Stream API
Stream API

book
Challenge: Income Tax Calculation

Tâche

Swipe to start coding

Implement a program that calculates taxes based on income brackets, applying different tax rates depending on the income level.

  • If the income is over 100,000, the tax rate is 25%.
  • If the income is between 50,000 and 100,000, the tax rate is 15%.
  • If the income is below 50,000, the tax rate is 5%.

  1. Set the appropriate values in if and else if for the income conditions.
  2. Multiply the amount by the appropriate percentage in each case to get the correct tax amount.
  3. In the calculateTaxes method, iterate through the income array using a for-each loop.
  4. Add the tax for each income to the result list by using the taxFunction functional interface passed as a parameter.
  5. Store the result of the calculateTaxes method in the taxes variable.

Solution

java

solution

package com.example;

import java.util.List;
import java.util.Arrays;
import java.util.ArrayList;
import java.util.function.Function;

public class Main {
public static void main(String[] args) {
// List of incomes
List<Double> incomes = Arrays.asList(120000.0, 80000.0, 40000.0, 150000.0);

// Function to calculate tax
Function<Double, Double> calculateTax = income -> {
if (income > 100000) {
return income * 0.25; // 25% tax
} else if (income >= 50000) {
return income * 0.15; // 15% tax
} else {
return income * 0.05; // 5% tax
}
};

// Apply the function to all incomes
List<Double> taxes = calculateTaxes(incomes, calculateTax);

// Print the results
taxes.forEach(tax -> System.out.println("Tax: " + tax));
}

public static List<Double> calculateTaxes(List<Double> incomes, Function<Double, Double> taxFunction) {
List<Double> result = new ArrayList<>();
for (Double income : incomes) {
result.add(taxFunction.apply(income)); // Apply the tax calculation function
}
return result;
Tout était clair ?

Comment pouvons-nous l'améliorer ?

Merci pour vos commentaires !

Section 1. Chapitre 7
package com.example;

import java.util.List;
import java.util.Arrays;
import java.util.ArrayList;
import java.util.function.Function;

public class Main {
public static void main(String[] args) {
// List of incomes
List<Double> incomes = Arrays.asList(120000.0, 80000.0, 40000.0, 150000.0);

// Function to calculate tax
Function<Double, Double> calculateTax = income -> {
if (income > ___) {
return income * ___; // 25% tax
} else if (income >= ___) {
return income * ___; // 15% tax
} else {
return income * ___; // 5% tax
}
};

// Apply the function to all incomes
List<Double> taxes = ___;

// Print the results
taxes.forEach(tax -> System.out.println("Tax: " + tax));
}

public static List<Double> calculateTaxes(List<Double> incomes, Function<Double, Double> taxFunction) {
List<Double> result = new ArrayList<>();
for (Double income : ___) {
result.___(taxFunction.___(___)); // Apply the tax calculation function
}
return result;
toggle bottom row
some-alt