用户从用户界面输入税单。然后税板存储在数据库中。之后从数据库中取出来计算用户的所得税。
@Getter
@Setter
public class TaxSlabDto {
private String maritalStatus;
private int lowerLimit;
private int upperLimit;
private double percent;
public TaxSlabDto(String maritalStatus, int lowerLimit, int upperLimit, double percent) {
this.maritalStatus = maritalStatus;
this.lowerLimit = lowerLimit;
this.upperLimit = upperLimit;
this.percent = percent;
}
}
下面的代码是不完整的代码。我想要完成的。我们必须从列表 taxSlabDtos 的值中替换以下代码中的静态值(上限、下限、税收百分比)。
public static void main(String[] args) {
List<TaxSlabDto> taxSlabDtos = new ArrayList();
taxSlabDtos.add(new TaxSlabDto("Un Married", 0, 400000, 1));
taxSlabDtos.add(new TaxSlabDto("Un Married", 400001, 500000, 10));
taxSlabDtos.add(new TaxSlabDto("Un Married", 500001, 700000, 20));
taxSlabDtos.add(new TaxSlabDto("Un Married", 700001, 2000000, 30));
taxSlabDtos.add(new TaxSlabDto("Un Married", 2000000, 1000000000, 36));
double tax = 0, income;
Scanner sc = new Scanner(System.in);
System.out.println("Enter income ");
income = sc.nextDouble();
for (TaxSlabDto taxSlabDto : taxSlabDtos) {
if (income <= 400000) {
tax = 1/100 * income;
} else if (income <= 500000) {
tax = (10/100 * (income - 400000)) + (1/100 * 400000);
} else if (income <= 700000) {
tax = (20/100 * (income - 500000)) + ((500000 - 400000) * 10/100) + (1/100 * 400000);
} else if (income <= 2000000) {
tax = ((income - 700000) * 30/100) + (20/100 * (700000 - 500000)) + ((500000 - 400000) * 10/100) + (1/100 * 400000);
} else {
tax = ((income - 2000000) * 36/100) + ((2000000 - 700000) * 30/100) + (20/100 * (700000 - 500000)) + ((500000 - 400000) * 10/100) + (1/100 * 400000);
}
}
System.out.println("Total Income Tax " + tax);
}
目前税收计算不是动态的。哪位Java天才可以帮我根据list taxSlabDtos的值动态计算所得税。
答案 0 :(得分:0)
public double calculateIncomeTax(List<TaxSlabDto> taxSlabDtos, double taxableSalary) {
double tax = 0.0;
for (TaxSlabDto taxSlabDtoOuter : taxSlabDtos) {
for (TaxSlabDto taxSlabDto : taxSlabDtos) {
Map<String, Double> map = calculateTax(taxableSalary, taxSlabDto);
taxableSalary = map.get("Income");
tax = tax + map.get("Tax");
double flagValue = map.get("Flag");
if (flagValue > 0) {
break;
}
}
if (taxableSalary == 0) {
break;
}
}
return tax;
}
private static Map<String, Double> calculateTax(Double taxableSalary, TaxSlabDto taxSlabDto) {
Double tax = 0.0;
Boolean flag = false;
Map<String, Double> map = new HashMap<>();
if (taxableSalary <= taxSlabDto.getUpperLimit()) {
tax = calculateTax(taxSlabDto, taxableSalary);
taxableSalary = remainingIncome(taxableSalary, taxSlabDto);
flag = true;
}
map.put("Tax", tax);
map.put("Income", taxableSalary);
if (flag) {
map.put("Flag", 1.0);
} else {
map.put("Flag", 0.0);
}
return map;
}
private static Double remainingIncome(Double taxableSalary, TaxSlabDto taxSlabDto) {
if (taxSlabDto.getLowerLimit() == 0) {
taxableSalary = taxableSalary - (taxableSalary - taxSlabDto.getLowerLimit());
} else {
taxableSalary = taxableSalary - (taxableSalary - taxSlabDto.getLowerLimit() + 1);
}
return taxableSalary;
}
private static Double calculateTax(TaxSlabDto taxSlabDto, Double taxableSalary) {
Double tax;
if (taxSlabDto.getLowerLimit() == 0) {
tax = (taxSlabDto.getPercent() / 100 * (taxableSalary - (taxSlabDto.getLowerLimit())));
} else {
tax = (taxSlabDto.getPercent() / 100 * (taxableSalary - (taxSlabDto.getLowerLimit() - 1)));
}
return tax;
}