所得税计算器

时间:2014-03-05 05:29:19

标签: java calculator

package edu.westga.taxcalculator.model;

/**
 * Creates a taxReturn object
 */
public class TaxReturn {
    private double income;

    /**
     * Constructor for the TaxReturn class
     * 
     * @param income
     *            the income of the person.
     */
    public TaxReturn(double income) {
        if (income < 0) {
            throw new IllegalArgumentException(
                    "Income can't be less than zero.");
        }
        this.income = income;
    }

    public void getTax() {
        if (income <= 50000) {
            income *= 0.01;
        } else if (income <= 75000) {
            income *= 0.02;
        } else if (income <= 100000) {
            income *= 0.03;
        } else if (income <= 250000) {
            income *= 0.04;
        } else if (income <= 500000) {
            income *= 0.05;
        } else
            income *= 0.06;

    }
}

package edu.westga.taxcalculator.controller;

import java.util.Scanner;
import edu.westga.taxcalculator.model.TaxReturn;

public class TaxCalculatorController {
    public static void main(String[] args) {
        System.out.println("Please enter your income: ");
        Scanner theScanner = new Scanner(System.in);
        double income = theScanner.nextDouble();
        TaxReturn theCalculator = new TaxReturn(income);
        System.out.println("The amount of tax is: " + taxReturn.getTax());
    }
}

我正在为所得税计算器编写一个程序,该项目有一个类和一个测试器类。假设计算我输入的金额的所得税,但它没有那么好。我会感激任何帮助,因为我肯定会坚持这个。

1 个答案:

答案 0 :(得分:1)

开始更改

    TaxReturn theCalculator = new TaxReturn(income);
    System.out.println("The amount of tax is: " + taxReturn.getTax());

    TaxReturn theCalculator = new TaxReturn(income);
    System.out.println("The amount of tax is: " + theCalculator .getTax());

此外,您的Constructor会引发Exception,但不会声明它会发生。

所以改为

public TaxReturn(double income) throw IllegalArgumentException { ....