I\'m trying to apply array and loop on this program I wrote to make it efficient
ID: 3645120 • Letter: I
Question
I'm trying to apply array and loop on this program I wrote to make it efficiently calculate more than 30 tax brackets, meaning 30 different tax rate depending on the gross income minus deductions.Please help!
import javax.swing.JOptionPane;
public class TaxGUI {
public static void main(String[] args) {
final double Dependant_Deduction = 2000.00;
final double Senior_Deduction = 0.50;
final double Minor_Deduction = 0.25;
final double Single_Deduction = 500.0;
final double Married_Deduction = 750.0;
//Variables
double grossIncome;//User's input
int numDependant;//User's input
int age; //User's input
int maritalStatus; // User's input
double amountPaid; //User's input
//Request input from the user
//Calculate Tax Rate
String grossIncomeString = JOptionPane.showInputDialog("Enter your gross income: ");
grossIncome = Double.parseDouble(grossIncomeString);
double taxrate;
if (grossIncome <= 10000)
{
taxrate = 0.18;
}
else if (grossIncome <= 50000)
{
taxrate = 0.31;
}
else
{
taxrate = 0.40;
}
//Request input from the user
//Calculate Deduction for children
String deduction1String = JOptionPane.showInputDialog("Enter your dependants: ");
numDependant = Integer.parseInt(deduction1String);
double deduction1;
deduction1 = numDependant * Dependant_Deduction;
//Request input from the user
//Calculate Deduction for age
String deduction2String = JOptionPane.showInputDialog("Enter your age: ");
age = Integer.parseInt(deduction2String);
double deduction2;
if(age >= 65)
{
deduction2 = (grossIncome - deduction1) * Senior_Deduction;
}
else if(age <= 17)
{
deduction2 = (grossIncome - deduction1) * Minor_Deduction;
}
else
{
deduction2 = 0;
}
//Request input from the user
//Calculate Deduction for martial status
String deduction3String = JOptionPane.showInputDialog("Enter your marital status(1=single 2=married): ");
maritalStatus = Integer.parseInt(deduction3String);
double deduction3 = 0;
if(maritalStatus == 1)
{
deduction3 = Single_Deduction;
}
else if(maritalStatus == 2)
{
deduction3 = Married_Deduction;
}
//Request input from the user for the tax paid
String PaidString = JOptionPane.showInputDialog("Enter the amount you paid in taxes during the year: ");
amountPaid = Double.parseDouble(PaidString);
//Calculate Tax
double TaxableIncome = grossIncome - deduction1 - deduction2 - deduction3;
double TaxObligation = TaxableIncome * taxrate;
double NetIncome = TaxableIncome - TaxObligation;
double TaxesDue = TaxObligation - amountPaid;
//Displays the results
String output = "Total Income: $" + grossIncome + " Deduction for Children: $" + deduction1 + " Deduction for Age: $" + deduction2 + " Deduction for Marital Status: $" + deduction3 + " Taxable Income: $" + TaxableIncome + " Tax Obligation: $" + TaxObligation + " Net Income: $" + NetIncome + " Taxes Paid: $" + amountPaid + " Taxes Due: $" + TaxesDue;
JOptionPane.showMessageDialog(null, output);
}
}
Explanation / Answer
Please rate...
This is the rectified program:
===================================================================
import java.util.Scanner;
import javax.swing.JOptionPane;
public class TaxGUI {
public static void main(String[] args) {
final double Dependant_Deduction = 2000.00;
final double Senior_Deduction = 0.50;
final double Minor_Deduction = 0.25;
final double Single_Deduction = 500.0;
final double Married_Deduction = 750.0;
Scanner reader = new Scanner(System.in);
//Variables
double grossIncome;//User's input
int numDependant;//User's input
int age; //User's input
int maritalStatus; // User's input
double amountPaid; //User's input
//Request input from the user
//Calculate Tax Rate
String grossIncomeString = JOptionPane.showInputDialog("Enter your gross income: ");
grossIncome = Double.parseDouble(grossIncomeString);
double taxrate;
if (grossIncome <= 10000)
{
taxrate = 0.18;
}
else if (grossIncome <= 50000)
{
taxrate = 0.31;
}
else
{
taxrate = 0.40;
}
//Request input from the user
//Calculate Deduction for children
String numDependantString = JOptionPane.showInputDialog("Enter your dependants: ");
numDependant = Integer.parseInt(numDependantString);
double deduction1 = numDependant * Dependant_Deduction;
//Request input from the user
//Calculate Deduction for age
String ageString = JOptionPane.showInputDialog("Enter your age: ");
age = Integer.parseInt(ageString);
double deduction2;
if(age >= 65)
{
deduction2 = (grossIncome - deduction1) * Senior_Deduction;
}
else if(age <= 17)
{
deduction2 = (grossIncome - deduction1) * Minor_Deduction;
}
else
{
deduction2 = 0;
}
//Request input from the user
//Calculate Deduction for martial status
String maritalStatusString = JOptionPane.showInputDialog("Enter your marital status(1=single 2=married): ");
maritalStatus = Integer.parseInt(maritalStatusString);
double deduction3=0;
if(maritalStatus == 1)
{
deduction3 = Single_Deduction;
}
else if(maritalStatus == 2)
{
deduction3 = Married_Deduction;
}
//Request input from the user for the tax paid
String amountPaidString = JOptionPane.showInputDialog("Enter the amount you paid in taxes during the year: ");
amountPaid = Double.parseDouble(amountPaidString);
//Calculate Tax
double TaxableIncome = grossIncome - deduction1 - deduction2 - deduction3;
double TaxObligation = TaxableIncome * taxrate;
double NetIncome = TaxableIncome - TaxObligation;
double TaxesDue = TaxObligation - amountPaid;
//Displays the results
String output = "Total Income :" + grossIncome + " Deduction for Children :" + deduction1 + " Deduction for Age :" + deduction2 + " Deduction for Marital Status: " + deduction3 + " Taxable Income: " + TaxableIncome + " Tax Obligation: " + TaxObligation + " Net Income: " + NetIncome + " Taxes Paid: " + amountPaid + " Taxes Due: " + TaxesDue;
JOptionPane.showMessageDialog(null,output);
}
}