Discuss / Java / hello cao

hello cao

Topic source

#1 Created at ... [Delete] [Delete and Lock User]

public class CalTax {

    public static void main(String[] args) {

        //给一个有工资收入和稿费收入的小伙伴算税

        Income[] incomes = new Income[] {

            new Income(3000),

            new Salary(7500),

            new StateCouncilSpecialAllowance(15000),

            new WriteAllowance(10000),

        };

        System.out.println(totalTax(incomes));

    }

    public static double totalTax(Income... incomes) {

        double total = 0;

        for (Income income: incomes) {

            total = total + income.getTax();

        }

        return total;

    }

}

class Income {

    protected double income;

    public Income(double income) {

        this.income = income;

    }

    public double getTax() {

        return income * 0.1; // 税率10%

    }

}

class Salary extends Income {

    public Salary(double income) {

        super(income);

    }

    @Override

    public double getTax() {

        if (income <= 5000) {

            return 0;

        }

        return (income - 5000) * 0.2;

    }

}

class StateCouncilSpecialAllowance extends Income {

    public StateCouncilSpecialAllowance(double income) {

        super(income);

    }

    @Override

    public double getTax() {

        return 0;

    }

}

class WriteAllowance extends Income {

public WriteAllowance(double income) {

super(income);

}

// TODO Auto-generated constructor stub

@Override

    public double getTax() {

        return income * 0.25;

}

}


  • 1

Reply