请使用Java语言实现如下税率计算: 1~5000 税率 0 5001~8000 3% 8001~17000 10% 17001~30000 20% 30001~40000 25% 40001~60000 30% 60001~85000 35% 85001~ 45% 要求 ⅰ. 逻辑正确,代码优雅 ⅱ. 可扩展性,考虑区间的变化,比如说起征点从5000变成10000等等,或者说85000以上的征税50%。 ⅲ. 推荐使用合适的设计模式 这里举个例子,比如说税前10000元,5000部分是不扣税,后面5000,3000扣税3%,2000扣税10%。 public class TaxCalculator { public double calculateTax(int amount) { // TODO } } 不用if语句
时间: 2024-02-14 12:12:19 浏览: 113
使用Java计算个人所得税
5星 · 资源好评率100%
可以使用责任链模式来实现税率计算,如下所示:
首先定义一个责任链节点抽象类TaxHandler,用来处理税率计算的各个阶段:
```java
public abstract class TaxHandler {
protected TaxHandler successor;
public void setSuccessor(TaxHandler successor) {
this.successor = successor;
}
public abstract double handle(double income);
}
```
然后定义具体的责任链节点类,分别对应税率区间:
```java
public class TaxHandler0 extends TaxHandler {
@Override
public double handle(double income) {
if (income <= 5000) {
return 0.0;
} else {
return successor.handle(income);
}
}
}
public class TaxHandler3 extends TaxHandler {
@Override
public double handle(double income) {
if (income <= 8000) {
return 0.03 * (income - 5000);
} else {
return successor.handle(income);
}
}
}
public class TaxHandler10 extends TaxHandler {
@Override
public double handle(double income) {
if (income <= 17000) {
return 0.1 * (income - 8000) + new TaxHandler3().handle(8000);
} else {
return successor.handle(income);
}
}
}
public class TaxHandler20 extends TaxHandler {
@Override
public double handle(double income) {
if (income <= 30000) {
return 0.2 * (income - 17000) + new TaxHandler10().handle(17000);
} else {
return successor.handle(income);
}
}
}
public class TaxHandler25 extends TaxHandler {
@Override
public double handle(double income) {
if (income <= 40000) {
return 0.25 * (income - 30000) + new TaxHandler20().handle(30000);
} else {
return successor.handle(income);
}
}
}
public class TaxHandler30 extends TaxHandler {
@Override
public double handle(double income) {
if (income <= 60000) {
return 0.3 * (income - 40000) + new TaxHandler25().handle(40000);
} else {
return successor.handle(income);
}
}
}
public class TaxHandler35 extends TaxHandler {
@Override
public double handle(double income) {
if (income <= 85000) {
return 0.35 * (income - 60000) + new TaxHandler30().handle(60000);
} else {
return successor.handle(income);
}
}
}
public class TaxHandler45 extends TaxHandler {
@Override
public double handle(double income) {
return 0.45 * (income - 85000) + new TaxHandler35().handle(85000);
}
}
```
其中,每个节点都有一个successor属性,表示下一个处理节点,如果当前节点无法处理,则会将请求传递给下一个节点来处理。
最后,在TaxCalculator中组装责任链,并调用责任链节点来计算税率:
```java
public class TaxCalculator {
private static final int THRESHOLD = 5000; // 起征点
private TaxHandler taxHandler;
public TaxCalculator() {
TaxHandler0 handler0 = new TaxHandler0();
TaxHandler3 handler3 = new TaxHandler3();
TaxHandler10 handler10 = new TaxHandler10();
TaxHandler20 handler20 = new TaxHandler20();
TaxHandler25 handler25 = new TaxHandler25();
TaxHandler30 handler30 = new TaxHandler30();
TaxHandler35 handler35 = new TaxHandler35();
TaxHandler45 handler45 = new TaxHandler45();
handler0.setSuccessor(handler3);
handler3.setSuccessor(handler10);
handler10.setSuccessor(handler20);
handler20.setSuccessor(handler25);
handler25.setSuccessor(handler30);
handler30.setSuccessor(handler35);
handler35.setSuccessor(handler45);
taxHandler = handler0;
}
public double calculateTax(int amount) {
double income = amount - THRESHOLD; // 扣除起征点后的收入
if (income <= 0) {
return 0.0;
} else {
return taxHandler.handle(income);
}
}
}
```
这样,我们就可以根据税前收入来计算税率,而且可以方便地扩展税率区间,并且可以调整起征点。
阅读全文