Discuss / Java / 20%和20%的税率 交税2900 对吗老铁们

20%和20%的税率 交税2900 对吗老铁们

Topic source
public class Main {

	public static void main(String[] args) {
		// TODO: 用抽象类给一个有工资收入和稿费收入的小伙伴算税:
		Income[] incomes = new Income[] { new SalaryIncome(7500), new RoyaltyIncome(12000) };
		double total = 0;
		// TODO:
		for (Income income:incomes) {
			total+= income.getTax();
		}
		System.out.println(total);
	}
}
abstract class Income{
	protected double income;
	public  Income(double income) {
		this.income = income;
	}
	public abstract double getTax();
}
class SalaryIncome extends Income{
	public SalaryIncome(double income) {
		super(income);
	}
	@Override
	public  double getTax() {
		if (this.income <= 5000) {
			return 0;
		}
		return (this.income-5000)*0.2;
	}
}
class RoyaltyIncome extends Income{
	public RoyaltyIncome(double income) {
		super(income);
	}
	@Override
	public double getTax() {
		return this.income*0.2;
	}	
}

错了,应该是3200

//Main
public class Main {

	public static void main(String[] args) {
		// TODO: 用抽象类给一个有工资收入和稿费收入的小伙伴算税:
		Income[] incomes = new Income[] { new BaseIncome(3000), new SalaryIncome(7500), new RoyaltyIncome(12000) };
		double total = 0;
		// TODO:
		for (Income income : incomes) {
			total += income.getTax();
		}
		System.out.println(total);
	}
}
//抽象类Income
public abstract class Income {

	// TODO
	protected int income;
	public Income(int income) {
		// TODO Auto-generated constructor stub
		this.income = income;
	}
	
	public abstract Double getTax();
	
}
//BaseIncome
public class BaseIncome extends Income{
	
	public BaseIncome(int income) {
		// TODO Auto-generated constructor stub
		super(income);
	}
	@Override
	public Double getTax() {
		
		return this.income  * 0.1;
	}
}
//SalaryIncome
public class SalaryIncome extends Income{

	// TODO
	
	public SalaryIncome(int income) {
		// TODO Auto-generated constructor stub
		super(income);
	}
	@Override
	public Double getTax() {
		if (this.income<5000) {
			return 0.0;
		}
		return (this.income - 5000) * 0.2;
	}
}
//RoyaltyIncome
public class RoyaltyIncome extends Income{
	
	// TODO
	public RoyaltyIncome(int income) {
		// TODO Auto-generated constructor stub
		super(income);
	}
	@Override
	public Double getTax() {
		return this.income*0.2;
	}
}


  • 1

Reply