Skip to content

Instantly share code, notes, and snippets.

@Raouf25
Created July 31, 2024 14:35
Show Gist options
  • Save Raouf25/3a7518cf0274f3d82813b49b116a7d3b to your computer and use it in GitHub Desktop.
Save Raouf25/3a7518cf0274f3d82813b49b116a7d3b to your computer and use it in GitHub Desktop.
import java.util.List;
public class TaxCalculator {
// Define a list of tax brackets
private static final List<TaxBracket> taxBrackets = List.of(
new TaxBracket(0, 10225, 0.0),
new TaxBracket(10225, 26070, 0.11),
new TaxBracket(26070, 74545, 0.30),
new TaxBracket(74545, 160336, 0.41),
new TaxBracket(160336, Double.MAX_VALUE, 0.45)
);
// Method to calculate the tax based on income
public double calculateTax(double income) {
return taxBrackets.stream()
.map(bracket -> bracket.calculateTax(income))
.reduce(0.0, Double::sum);
}
// Main method to test the tax calculation
public static void main(String[] args) {
TaxCalculator calculator = new TaxCalculator();
System.out.println(calculator.calculateTax(50000));
}
// Inner class representing a tax bracket
private static class TaxBracket {
double lowerLimit;
double upperLimit;
double rate;
// Constructor for TaxBracket
TaxBracket(double lowerLimit, double upperLimit, double rate) {
this.lowerLimit = lowerLimit;
this.upperLimit = upperLimit;
this.rate = rate;
}
// Method to calculate the tax for a given income based on this bracket
double calculateTax(double income) {
if (income > lowerLimit) {
return (Math.min(income, upperLimit) - lowerLimit) * rate;
}
return 0;
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment