javacalculator

Income tax 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());
    }
}

I am writing a program for an income tax calculator and the project has a class and a tester class. It is suppose to calculate the income tax of the amount I enter but it is not working out so well. I would appreciate any help because I am definitely stuck on this.


Solution

  • For a start change

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

    to

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

    Also your Constructor throws an Exception, but does not declare that it is going to.

    so change to

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