/**
* Program for Calculation Progressive Income Tax
*
* @author tkrogers
* @version 10-29-12
*/
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
// Do not alter the following code //////////////////////////////////////
public class ProgressiveIncomeTax extends JPanel implements ActionListener {
private JTextField input ;
private JTextArea output ;
private int onSwitch1,
onSwitch2 ;
private double incomeDollars,
taxableIncome,
tax1,
tax2,
totalTax,
nominalTaxRate ;
public static void main ( ) {
createAndShowGUI( ) ;
}
// constructor
public ProgressiveIncomeTax ( ) {
setLayout( new GridLayout ( 2,1 ) ) ;
input = new JTextField( " Enter income in $ ", 40 ) ;
input.addActionListener ( this ) ;
output = new JTextArea ( " Income " + incomeDollars + "\n",5, 40 ) ;
output.setEditable ( false ) ;
JScrollPane scrollPane = new JScrollPane ( input ) ;
add ( scrollPane ) ;
add ( output ) ;
}
// Creates a graphical user interface.
private static void createAndShowGUI ( ) {
//Create and set up the window.
JFrame frame = new JFrame ( "Progressive Tax Calculator" ) ;
frame.setDefaultCloseOperation ( JFrame.EXIT_ON_CLOSE ) ;
//Add contents to the window.
frame.add(new ProgressiveIncomeTax ( ) ) ;
//Display the window.
frame.pack ( ) ;
frame.setVisible ( true ) ;
}
// Outputs results when <enter> is pressed after inputting an income
public void actionPerformed ( ActionEvent e ) {
String s = input.getText ( ) ;
s = s.substring( 21 ).trim ( ) ;
incomeDollars = Double.parseDouble ( s );
input.setText( " Enter income in $ " );
calculateTax ( ) ;
taxableIncome = decimalPlaces ( 2, taxableIncome ) ;
output.setText ( " Income $" + incomeDollars +
"\n Taxable Income $" + taxableIncome +
"\n Total Tax $" + totalTax +
"\n Nominal Tax Rate " + nominalTaxRate + "%" ) ;
}
// Sets the number f decmal places show for doubles.
public double decimalPlaces ( int decimals, double x) {
double d = Math.pow ( 10, decimals ) ;
x = ( ( int ) ( (x + .5 / d ) * d ) ) / d ;
return x ;
}
//Calculates the tax
public void calculateTax ( ) {taxableIncome = ( incomeDollars - 19999.99 ) * onSwitch1 ( ) ;
tax1 = taxableIncome *.25 ;
tax2 = ( taxableIncome-10000 ) *.10 * onSwitch2 ( ) ;
totalTax = tax1 + tax2 ;
roundUpTax ( ) ;
calculateNominalTaxRate ( ) ;}
//Calculates the on-switch for the 1st tax rate.
public int onSwitch1 ( ) {
int pennies = ( int ) ( incomeDollars * 100 ) ;
int s = 1 - ( pennies % 2000000 ) / pennies ;
return s;
}