Reputation:
my assignment is simple really. someone enter a number for their income, if its under 9000, its taxed at 10%, over that, its 15% etc.
but then one of the requirements says "you will organize the income ranges as an array in each method and select an array record to compute specific tax in a tax bracket"
how would that work? doesn't array only take specific values and not ranges of values??
this is my code so far
public static int calcTax(int status, int income)
{
if (status == 0)
{
return singleFiling(income);
}
if (status == 1)
{
return jointFiling(income);
}
if (status == 2)
{
return headFiling(income);
}
else
System.out.println("Error: invalid status. Enter a number between 0 to 2 only.");
return income;
}
static int ninek = 9075;
static int thrirtysixk = 36900;
static int eightyninek = 89350;
static int oneeightysixk = 186350;
static int fourofivek = 405100;
static int fourosixk = 406750;
static int eighteenk = 18150;
static int seventythreek = 73800;
static int onefortyeightk = 148850;
static int twotwentysixk = 226850;
static int fourfivesevenk = 457600;
static int twelveninefifty = 12950;
static int fourninek = 49400;
static int onetwentysevenk = 127550;
static int twoosixk = 206600;
static int fpurthreetwok = 432200;
static double tax = 0;
static int singleFiling(double userIncome)
{
if (userIncome >= 1 && userIncome <= ninek )
tax = userIncome * 0.10;
System.out.println("Your payable Federal tax is: " + tax);
return 0;
}
static int jointFiling(double userIncome)
{
if (userIncome >= 1 && userIncome <= oneeightysixk )
tax = userIncome * 0.10;
System.out.println("Your payable Federal tax is: " + tax);
return 0;
}
static int headFiling(double userIncome)
{
if (userIncome >= 1 && userIncome <= twelveninefifty )
tax = userIncome * 0.10;
System.out.println("Your payable Federal tax is: " + tax);
return 0;
}
}
Upvotes: 1
Views: 58
Reputation: 405
Taking the comments into account, your code could look like this:
static int[] incomeLevel = new int[]{9075, 186350, 12950};
static double[] taxRate = new double[]{.1, .15, .2}
static double tax = 0.0;
public static int calcTax(int status, int income) {
if (status < 0 || status > 2) {
System.out.println("Error: invalid status. Enter a number between 0 to 2 only.")
return income;
}
if (income > 0 && income <= incomeLevel[status])
tax = income * taxRate[status];
System.out.println("Your payable Federal tax is: " + tax)
return 0;
}
Of course the other values for the income levels have to been added, same for the tax rates.
Instead of computing every value in another method, choosing by status
, you can use the status as the array index. That has the advantage that only need the code once instead of three times but with some other values.
Now you only need to check if your status is in your wanted range.
Upvotes: 1