public static void main(String[] args) {
Address address = new Address(1, "a", "a", "a", "a", 1);
ResidentTaxPayer a1 = new ResidentTaxPayer("a", "a", "a", 50000, address,
State.NSW);
ForeignResidentTaxPayer b1 = new ForeignResidentTaxPayer("a", "a", "a",
10000, address, "a");
WorkingHolidayTaxPayer c1 = new WorkingHolidayTaxPayer("a", "a", "a",
150000, address, "a");
ArrayList array = new ArrayList<>();
array.add(a1);
array.add(b1);
array.add(c1);
double totalTaxPayable = 0;
for (Object d : array) {
totalTaxPayable =+ d.calcTax(TaxPayer.getIncome());
}
System.out.println(totalTaxPayable);
}
ForeignResidentTaxPayer Calctax:
public static double calcTax(double income) {
double Tax = 0;
if (income < 90001) {
Tax = income * (32.5 / 100);
} else if (income >= 90001 && income < 180001) {
Tax = 29250 + income * (37 / 100);
} else {
Tax = 62550 + income * (45 / 100);
}
return Tax;
}
@Override
public double calcTax() {
double Tax = 0;
if (getIncome() < 90001) {
Tax = getIncome() * (32.5 / 100);
} else if (getIncome() >= 90001 && getIncome() < 180001) {
Tax = 29250 + getIncome() * (37 / 100);
} else {
Tax = 62550 + getIncome() * (45 / 100);
}
return Tax;
}
ResidentTaxPayer:`
public static double calcTax(double income) {
double Tax = 0;
if (income < 18201) {
Tax = 0;
} else if (income >= 18201 && income < 37001) {
Tax = income - 18200 * (19 / 100);
} else if (income >= 37001 && income < 90001) {
Tax = 3572 + (income - 37000) * (32.5 / 100);
} else if (income >= 90001 && income < 180001) {
Tax = 20797 + (income - 90000) * (37 / 100);
} else {
Tax = 54097 + (income - 180000) * (45 / 100);
}
return Tax;
}
@Override
public double calcTax() {
double Tax = 0;
if (getIncome() < 18201) {
Tax = 0;
} else if (getIncome() >= 18201 && getIncome() < 37001) {
Tax = getIncome() - 18200 * (19 / 100);
} else if (getIncome() >= 37001 && getIncome() < 90001) {
Tax = 3572 + (getIncome() - 37000) * (32.5 / 100);
} else if (getIncome() >= 90001 && getIncome() < 180001) {
Tax = 20797 + (getIncome() - 90000) * (37 / 100);
} else {
Tax = 54097 + (getIncome() - 180000) * (45 / 100);
}
return Tax;
}
WorkingHolidayTaxPayer:
public static double calcTax(double income) {
double Tax = 0;
if (income < 37001) {
Tax = income * (15 / 100);
} else if (income >= 37001 && income < 90001) {
Tax = 5550 + income * (32.5 / 100);
} else if (income >= 90001 && income < 180001) {
Tax = 22775 + income * (37 / 100);
} else {
Tax = 56075 + income * (45 / 100);
}
return Tax;
}
@Override
public double calcTax() {
double Tax = 0;
if (getIncome() < 37001) {
Tax = getIncome() * (15 / 100);
} else if (getIncome() >= 37001 && getIncome() < 90001) {
Tax = 5550 + getIncome() * (32.5 / 100);
} else if (getIncome() >= 90001 && getIncome() < 180001) {
Tax = 22775 + getIncome() * (37 / 100);
} else {
Tax = 56075 + getIncome() * (45 / 100);
}
return Tax;
}
我的问题是试图将这些对象的所有应纳税额加在一起。我已经尝试过for循环,但是由于它们不是静态的而无法从其他类中调用方法,或者当它们处于静态状态时,它带有“在对象d中找不到变量收入/方法getIncome()”。
感谢您的帮助!
答案 0 :(得分:0)
有多种解决方法。首先,您可以这样做:
ArrayList<Double> array = new ArrayList<>();
double income = TaxPayer.getIncome();
array.add(a1.calcTax(income));
array.add(b1.calcTax(income));
array.add(c1.calcTax(income));
double totalTaxPayable = 0;
for (int i = 0; i < array.size(); i++) {
totalTaxPayable=+ array.get(i);
}
System.out.println(totalTaxPayable);
或者甚至忘记了ArrayList和 for 循环:
double totalTaxPayable = a1.calcTax(income) + b1.calcTax(income) + c1.calcTax(income);
System.out.println(totalTaxPayable);
您还可以向每个类( taxValue )添加双精度类型的Member变量,并在每个类中使用 Getter 方法( getTaxValue())来保存税额,然后从实例而不是方法调用中获得所需的值(必须由构造函数执行方法调用),只需将收入传递给每个Class构造函数即可。返回的有符号值表示欠您的款项,而无符号的值返回表示您欠的款项:
double totalTaxPayable = a1.getTaxValue() + b1.getTaxValue() + c1.getTaxValue();