Skip to content

Instantly share code, notes, and snippets.

@snarkbait
Last active August 29, 2015 14:15
Show Gist options
  • Save snarkbait/7861ccb5047253db105b to your computer and use it in GitHub Desktop.
Save snarkbait/7861ccb5047253db105b to your computer and use it in GitHub Desktop.
2014 US Federal Tax Calculator
public class TaxCalculator implements TaxLookup
{
public static double getTax(int income, int status)
{
double tax = 0;
for (int i = 0; i < 7; i++)
{
if (income > lookup[status][i][1])
{
tax = lookup[status][i][0] + (income - lookup[status][i][1]) * lookup[status][i][2];
}
}
return tax;
}
public static int getBracket(int income, int status)
{
int bracket = 0;
for (int i = 0; i < 7; i++)
{
if (income > lookup[status][i][1])
{
bracket = (int)(lookup[status][i][2]*100);
}
}
return bracket;
}
}
import java.awt.event.KeyEvent;
import javax.swing.JOptionPane;
public class TaxCalculatorGUI extends javax.swing.JFrame {
public TaxCalculatorGUI() {
initComponents();
}
/**
* This method is called from within the constructor to initialize the form.
* WARNING: Do NOT modify this code. The content of this method is always
* regenerated by the Form Editor.
*/
@SuppressWarnings("unchecked")
// <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
private void initComponents() {
jLabel1 = new javax.swing.JLabel();
cmbFilingStatus = new javax.swing.JComboBox();
jLabel2 = new javax.swing.JLabel();
txtIncome = new javax.swing.JTextField();
btnCalculate = new javax.swing.JButton();
jLabel3 = new javax.swing.JLabel();
lblTax = new javax.swing.JLabel();
jLabel4 = new javax.swing.JLabel();
lblBracket = new javax.swing.JLabel();
jLabel5 = new javax.swing.JLabel();
lblPercent = new javax.swing.JLabel();
setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
setTitle("Tax Calculator Federal 2015");
jLabel1.setFont(new java.awt.Font("Tahoma", 1, 12)); // NOI18N
jLabel1.setText("Enter Filing Status:");
cmbFilingStatus.setFont(new java.awt.Font("Tahoma", 0, 12)); // NOI18N
cmbFilingStatus.setModel(new javax.swing.DefaultComboBoxModel(new String[] { "Single", "Married - Filing Jointly", "Married - Filing Seperately", "Head of Household" }));
cmbFilingStatus.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
cmbFilingStatusActionPerformed(evt);
}
});
jLabel2.setFont(new java.awt.Font("Tahoma", 1, 12)); // NOI18N
jLabel2.setText("Enter Taxable Income (rounded up to nearest dollar):");
txtIncome.setFont(new java.awt.Font("Tahoma", 0, 12)); // NOI18N
txtIncome.addKeyListener(new java.awt.event.KeyAdapter() {
public void keyPressed(java.awt.event.KeyEvent evt) {
txtIncomeKeyPressed(evt);
}
});
btnCalculate.setText("Calculate");
btnCalculate.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
btnCalculateActionPerformed(evt);
}
});
jLabel3.setFont(new java.awt.Font("Tahoma", 1, 12)); // NOI18N
jLabel3.setText("Your Income Tax is:");
lblTax.setFont(new java.awt.Font("Tahoma", 1, 14)); // NOI18N
lblTax.setText("$");
jLabel4.setText("Your Income Tax Bracket:");
lblBracket.setFont(new java.awt.Font("Tahoma", 1, 12)); // NOI18N
lblBracket.setText("%");
jLabel5.setText("Your tax as % of income:");
lblPercent.setFont(new java.awt.Font("Tahoma", 1, 12)); // NOI18N
lblPercent.setText("%");
javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
getContentPane().setLayout(layout);
layout.setHorizontalGroup(
layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(layout.createSequentialGroup()
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(layout.createSequentialGroup()
.addGap(63, 63, 63)
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(layout.createSequentialGroup()
.addComponent(jLabel1)
.addGap(18, 18, 18)
.addComponent(cmbFilingStatus, javax.swing.GroupLayout.PREFERRED_SIZE, 247, javax.swing.GroupLayout.PREFERRED_SIZE))
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
.addComponent(jLabel2)
.addComponent(btnCalculate)
.addComponent(txtIncome, javax.swing.GroupLayout.PREFERRED_SIZE, 191, javax.swing.GroupLayout.PREFERRED_SIZE))))
.addGroup(layout.createSequentialGroup()
.addGap(69, 69, 69)
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
.addComponent(jLabel4)
.addComponent(jLabel3)
.addComponent(jLabel5))
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addComponent(lblTax, javax.swing.GroupLayout.PREFERRED_SIZE, 174, javax.swing.GroupLayout.PREFERRED_SIZE)
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING, false)
.addComponent(lblBracket, javax.swing.GroupLayout.Alignment.LEADING, javax.swing.GroupLayout.DEFAULT_SIZE, 70, Short.MAX_VALUE)
.addComponent(lblPercent, javax.swing.GroupLayout.Alignment.LEADING, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)))))
.addContainerGap(55, Short.MAX_VALUE))
);
layout.setVerticalGroup(
layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(layout.createSequentialGroup()
.addGap(63, 63, 63)
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
.addComponent(jLabel1)
.addComponent(cmbFilingStatus, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
.addGap(43, 43, 43)
.addComponent(jLabel2)
.addGap(18, 18, 18)
.addComponent(txtIncome, javax.swing.GroupLayout.PREFERRED_SIZE, 32, javax.swing.GroupLayout.PREFERRED_SIZE)
.addGap(18, 18, 18)
.addComponent(btnCalculate)
.addGap(18, 18, 18)
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
.addComponent(lblTax, javax.swing.GroupLayout.PREFERRED_SIZE, 40, javax.swing.GroupLayout.PREFERRED_SIZE)
.addComponent(jLabel3))
.addGap(18, 18, 18)
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
.addComponent(jLabel4)
.addComponent(lblBracket, javax.swing.GroupLayout.PREFERRED_SIZE, 23, javax.swing.GroupLayout.PREFERRED_SIZE))
.addGap(18, 18, 18)
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
.addComponent(jLabel5)
.addComponent(lblPercent, javax.swing.GroupLayout.PREFERRED_SIZE, 23, javax.swing.GroupLayout.PREFERRED_SIZE))
.addContainerGap(16, Short.MAX_VALUE))
);
pack();
setLocationRelativeTo(null);
}// </editor-fold>//GEN-END:initComponents
private void btnCalculateActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnCalculateActionPerformed
try
{
int income = Integer.parseInt(txtIncome.getText());
double tax = (TaxCalculator.getTax(income, cmbFilingStatus.getSelectedIndex()) * 100) /100;
String formattedTax = String.format("$ %,.2f", tax);
lblTax.setText(formattedTax);
lblBracket.setText(TaxCalculator.getBracket(income, cmbFilingStatus.getSelectedIndex()) + "%");
double percent = (tax / income) * 100;
lblPercent.setText(String.format("%.2f%%", percent));
}
catch (NumberFormatException nfe)
{
JOptionPane.showMessageDialog(rootPane, "Must be a valid number", "Error", JOptionPane.ERROR_MESSAGE);
txtIncome.requestFocusInWindow();
txtIncome.setText("");
}
}//GEN-LAST:event_btnCalculateActionPerformed
private void cmbFilingStatusActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_cmbFilingStatusActionPerformed
if (!txtIncome.getText().isEmpty())
{
btnCalculate.doClick();
}
}//GEN-LAST:event_cmbFilingStatusActionPerformed
private void txtIncomeKeyPressed(java.awt.event.KeyEvent evt) {//GEN-FIRST:event_txtIncomeKeyPressed
int key = evt.getKeyCode();
if (key==KeyEvent.VK_ENTER)
{
btnCalculate.doClick();
}
}//GEN-LAST:event_txtIncomeKeyPressed
/**
* @param args the command line arguments
*/
public static void main(String args[]) {
/* Set the Nimbus look and feel */
//<editor-fold defaultstate="collapsed" desc=" Look and feel setting code (optional) ">
/* If Nimbus (introduced in Java SE 6) is not available, stay with the default look and feel.
* For details see http://download.oracle.com/javase/tutorial/uiswing/lookandfeel/plaf.html
*/
try {
for (javax.swing.UIManager.LookAndFeelInfo info : javax.swing.UIManager.getInstalledLookAndFeels()) {
if ("Nimbus".equals(info.getName())) {
javax.swing.UIManager.setLookAndFeel(info.getClassName());
break;
}
}
} catch (ClassNotFoundException ex) {
java.util.logging.Logger.getLogger(TaxCalculatorGUI.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
} catch (InstantiationException ex) {
java.util.logging.Logger.getLogger(TaxCalculatorGUI.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
} catch (IllegalAccessException ex) {
java.util.logging.Logger.getLogger(TaxCalculatorGUI.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
} catch (javax.swing.UnsupportedLookAndFeelException ex) {
java.util.logging.Logger.getLogger(TaxCalculatorGUI.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
}
//</editor-fold>
/* Create and display the form */
java.awt.EventQueue.invokeLater(new Runnable() {
public void run() {
new TaxCalculatorGUI().setVisible(true);
}
});
}
// Variables declaration - do not modify//GEN-BEGIN:variables
private javax.swing.JButton btnCalculate;
private javax.swing.JComboBox cmbFilingStatus;
private javax.swing.JLabel jLabel1;
private javax.swing.JLabel jLabel2;
private javax.swing.JLabel jLabel3;
private javax.swing.JLabel jLabel4;
private javax.swing.JLabel jLabel5;
private javax.swing.JLabel lblBracket;
private javax.swing.JLabel lblPercent;
private javax.swing.JLabel lblTax;
private javax.swing.JTextField txtIncome;
// End of variables declaration//GEN-END:variables
}
public interface TaxLookup {
double[][][] lookup = {
{
// status 0
{ 0 , 0, 0.10},
{ 907.50, 9075, 0.15},
{ 5081.25, 36900, 0.25},
{ 18193.75, 89350, 0.28},
{ 45353.75, 186350, 0.33},
{ 117541.25, 405100, 0.35 },
{ 118118.75, 406750, 0.396 }
},
{
// status 1
{ 0, 0, 0.10 },
{ 1815, 18150, 0.15 },
{ 10162.50, 73800, 0.25 },
{ 28925, 148850, 0.28 },
{ 50765, 226850, 0.33 },
{ 109587.5, 405100, 0.35 },
{ 127962.5, 457600, 0.396 }
},
{
// status 2
{ 0 , 0, 0.10 },
{ 907.50, 9075, 0.15 },
{ 5081.25, 36900, 0.25 },
{ 14462.5, 74425, 0.28 },
{ 25382.5, 113425, 0.33 },
{ 54793.75, 202550, 0.35 },
{ 63981.25, 228800, 0.396 }
},
{
// status 3
{ 0, 0, 0.10 },
{ 1295, 12950, 0.15 },
{ 6762.5, 49400, 0.25 },
{ 26300, 127550, 0.28 },
{ 48434, 206600 , 0.33 },
{ 113939, 405100, 0.35 },
{ 123424, 432200, 0.396 }
}
};
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment