/*
* To change this template, choose Tools | Templates
* and open the template in the editor.
*/
package lab08;
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import javax.swing.border.*;
/**
*
* @author wichert22
*/
public class TaxpayerAryApplicEx extends JFrame {
// Prompts for user input
private JLabel label1, label2, label3, label4, label6, label7, label8;
// Mechanisms for user input
private JTextField textfield1, textfield2, textfield3, textfield4, textfield5;
private JComboBox typeJCB, typeJCB2; // The JComboBox for type selection
// Mechanism for output display
private JTextArea textDisplay; // Output display area
private int textDisplayWidth = 25; // Width of the JTextArea
private int textDisplayHeight = 18; // Height of the JTextArea
private boolean cannotAddFlag = false; // Indicates whether taxpayer can be added
private final int MAX_TAXPAYERS = 10; // The maximum number of taxpayers allowed
private int countTaxpayers = 0; // The number of taxpayers added to array
private Taxpayer taxpayerArray [] = new Taxpayer[MAX_TAXPAYERS];
private double grossPay = 0.0;
private String typeNames[] = {"Weekly","Bi-weekly","Monthly"};
private String selectedType = "Weekly";
private String typeOccupation[] = {"Accountant", "Contractor", "Dentist", "Doctor", "Electrician",
"Food Service", "Lawyer", "Mechanic", "Plumber", "Programmer", "Salesman", "Teacher"};
private String selectedOccupation = "Accountant";
// The main method required for an application program
public static void main( String[] args ) {
JFrame frame = new TaxpayerAryApplicEx(); // Construct the window
frame.setSize( new Dimension( 375, 500 ) ); // Set its size
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setVisible( true ); // Make the window visible
}
public TaxpayerAryApplicEx() {
super("GUI Demonstration");
//Container c = getContentPane();
setLayout( new BorderLayout() );
JPanel jpan = new JPanel();
jpan.setLayout(new GridLayout(7,2));
jpan.setBorder(new EtchedBorder());
add(jpan,BorderLayout.NORTH);
// Create prompt and input mechanism to get taxpayer number from user
label1 = new JLabel("Number of Taxpayers:");
jpan.add( label1);
textfield1 = new JTextField( 3 );
textfield1.setEditable(false);
textfield1.setText(Integer.toString(countTaxpayers));
jpan.add( textfield1 ); // put input JTextField on JPanel
label2 = new JLabel("Max # of Taxpayers:");
jpan.add( label2 );
textfield2 = new JTextField( 3);
textfield2.setText(Integer.toString(MAX_TAXPAYERS));
textfield2.setEditable(false);
jpan.add( textfield2 ); // put input JTextField on JPanel
// Create prompt and input mechanism to get taxpayer type from user
label6 = new JLabel("Taxpayer Type:");
jpan.add( label6 );
typeJCB = new JComboBox(typeNames);
typeJCB.setMaximumRowCount(3);
jpan.add( typeJCB ); // put input JTextField on JPanel
// Create prompt and input mechanism to get taxpayer name from user
label3 = new JLabel("Name:");
jpan.add( label3 );
textfield3 = new JTextField( 15 );
jpan.add( textfield3 ); // put input JTextField on JPanel
// Create prompt and input mechanism to get taxpayer number from user
label4 = new JLabel("Taxpayer SSN Number:");
jpan.add( label4 );
textfield4 = new JTextField( Integer.toString(countTaxpayers+2), 15 );
jpan.add( textfield4 ); // put input JTextField on JPanel
//Gets the occupation informnation
label7 = new JLabel("Occupation:");
jpan.add( label7 );
typeJCB2 = new JComboBox(typeOccupation);
typeJCB2.setMaximumRowCount(12);
jpan.add( typeJCB2);
//Gets gross pay information
label8 = new JLabel("Gross Pay:");
jpan.add( label8);
textfield5 = new JTextField( Double.toString(grossPay), 15 );
jpan.add( textfield5 );
// Set up JTextArea to display information on all the taxpayers
textDisplay = new JTextArea( getDataStringAllTaxpayers(),
textDisplayHeight,textDisplayWidth);
textDisplay.setLineWrap( true );
textDisplay.setWrapStyleWord( true );
textDisplay.setBorder(new TitledBorder("TAXPAYER LIST"));
add(textDisplay,BorderLayout.CENTER);
// Listener will respond to a user hitting Enter in any JTextField
MyActionListener myActListener = new MyActionListener();
textfield3.addActionListener( myActListener );
textfield4.addActionListener( myActListener );
textfield5.addActionListener( myActListener );
// Listener will respond to a user selecting from JComboBox
MyItemListener myItemListener = new MyItemListener();
typeJCB.addItemListener(myItemListener);
typeJCB2.addItemListener(myItemListener);
// Display data on all Taxpayers in the JTextArea
displayTaxpayerData();
}
private void displayTaxpayerData(){
setDisplayFields();
textDisplay.setText(getDataStringAllTaxpayers());
}
// Create and insert the new Taxpayer into array
private void addTaxpayerToArray( String ssN, double gp, String nam, String typ, String occ ){
String gp1 = Double.toString(gp);
if ( selectedType == "Weekly" ) {
WeeklyTaxpayer tp = new WeeklyTaxpayer(nam, (int) Double.parseDouble(gp1), Integer.parseInt(ssN) , typ, occ );
taxpayerArray[countTaxpayers] = tp;
countTaxpayers++;
}
else if (selectedType == "Bi-Weekly"){
BiweeklyTaxpayer tp = new BiweeklyTaxpayer(nam, (int) Double.parseDouble(gp1), Integer.parseInt(ssN), typ, occ);
taxpayerArray[countTaxpayers] = tp;
countTaxpayers++;
}
else if (selectedType == "Monthly"){
MonthlyTaxpayer tp = new MonthlyTaxpayer(nam, (int) Double.parseDouble(gp1), Integer.parseInt(ssN), typ, occ);
taxpayerArray[countTaxpayers] = tp;
countTaxpayers++;
}
}
// Return the taxpayer instance (object) correponding to the tpNumb parameter
private Taxpayer getTaxpayerFromArray( int tpNumb ) {
Taxpayer tp = null;
if ( tpNumb < countTaxpayers ) {
tp = taxpayerArray[tpNumb];
}
return tp;
}
// Set the contents of the JTextFields
private void setDisplayFields() {
textfield1.setText( Integer.toString(countTaxpayers) );
textfield2.setText( Integer.toString(MAX_TAXPAYERS) );
textfield3.setText("");
textfield4.setText("");
textfield5.setText( Double.toString(grossPay) );
}
// Get the string comprised of all the data on all taxpayers
private String getDataStringAllTaxpayers() {
String displayStr = "";
// Add the information on all taxpayers to the display string
for ( int i = 0; i < countTaxpayers; i++ ) {
displayStr += (i+1) + ". " + taxpayerArray[i].toString() + "\n ";
}
if ( cannotAddFlag )
displayStr += "\nCannot add taxpayers - the array is full\n";
return displayStr;
}
class MyActionListener implements ActionListener {
// Process user's action in JTextField input
public void actionPerformed( ActionEvent e) {
if ( countTaxpayers < MAX_TAXPAYERS ) {
String gp1 = Double.toString(grossPay);
String name = textfield3.getText();
String ssnNumber = textfield4.getText();
String occ = textfield5.getText();
addTaxpayerToArray(name, Double.parseDouble(gp1), ssnNumber, occ, selectedType );
} else {
cannotAddFlag = true;
}
displayTaxpayerData();
}
}
class MyItemListener implements ItemListener {
public void itemStateChanged(ItemEvent e) {
// If one of the items in the JComboBox was selected,
// set the selectedDept variable to the user's selection
if (e.getSource() == typeJCB) {
selectedType = typeNames[typeJCB.getSelectedIndex()];
}
}
}
}
/*
* To change this template, choose Tools | Templates
* and open the template in the editor.
*/
package lab08;
/**
*
* @author wichert22
*/
public class BiweeklyTaxpayer extends Taxpayer {
public BiweeklyTaxpayer(String nam, int ssN, double gp, String typ, String occ) {
super(nam, ssN, gp, typ, occ);
}
public double computeStateTax() {
double biweekly = 0;
if (grossPay >= 0 && grossPay < 308) {
double aPay = grossPay - 0;
biweekly = aPay * .04 + 0;
} else if (grossPay >= 308 && grossPay < 423) {
double aPay = grossPay - 308;
biweekly = aPay * .045 + 12.31;
} else if (grossPay >= 423 && grossPay < 500) {
double aPay = grossPay - 423;
biweekly = aPay * .0525 + 17.50;
} else if (grossPay >= 500 && grossPay < 769) {
double aPay = grossPay - 500;
biweekly = aPay * .059 + 21.54;
} else if (grossPay >= 769 && grossPay < 3462) {
double aPay = grossPay - 769;
biweekly = aPay * .0685 + 37.42;
} else if (grossPay >= 3462 && grossPay < 3846) {
double aPay = grossPay - 3462;
biweekly = aPay * .0764 + 221.85;
} else if (grossPay >= 3846 && grossPay < 5769) {
double aPay = grossPay - 3846;
biweekly = aPay * .0814 + 251.23;
} else if (grossPay >= 5769) {
double aPay = grossPay - 5769;
biweekly = aPay * .0935 + 407.85;
}
return biweekly;
}
public double computeFederalTax() {
double federal = 0;
double aPay = grossPay - 130.77;
if (aPay > 0 && aPay <= 51) {
double newaPay = aPay - 0;
federal = newaPay * 0 + 0;
} else if (aPay > 51 && aPay <= 195) {
double newaPay = aPay - 51;
federal = newaPay * .1 + 0;
} else if (aPay > 195 && aPay <= 645) {
double newaPay = aPay - 195;
federal = newaPay * .15 + 14.40;
} else if (aPay > 645 && aPay <= 1482) {
double newaPay = aPay - 645;
federal = newaPay * .25 + 81.90;
} else if (aPay > 1482 && aPay <= 3131) {
double newaPay = aPay - 1482;
federal = newaPay * .28 + 291.15;
} else if (aPay > 3131 && aPay <= 6763) {
double newaPay = aPay - 3131;
federal = newaPay * .33 + 752.87;
} else if (aPay > 6763) {
double newaPay = aPay - 6763;
federal = newaPay * .35 + 1951.43;
}
return federal;
}
public double computeNetPay() {
double net = 0;
net = grossPay - computeFederalTax() - computeStateTax();
return net;
}
public String toString() {
return ", Net Pay: " + prec2.format(computeNetPay() + ", biweekly state tax: "
+ prec2.format(computeStateTax() + ", biweekly federal tax: "
+ prec2.format(computeFederalTax())));
}
}
/*
* To change this template, choose Tools | Templates
* and open the template in the editor.
*/
package lab08;
/**
*
* @author wichert22
*/
public class MonthlyTaxpayer extends Taxpayer {
public MonthlyTaxpayer(String nam, int ssN, double gp, String typ, String occ) {
super(nam, ssN, gp, typ, occ);
}
public double computeStateTax() {
double monthly = 0;
if (grossPay >= 0 && grossPay < 667) {
double aPay = grossPay - 0;
monthly = aPay * .04 + 0;
} else if (grossPay >= 667 && grossPay < 917) {
double aPay = grossPay - 667;
monthly = aPay * .045 + 26.27;
} else if (grossPay >= 917 && grossPay < 1083) {
double aPay = grossPay - 917;
monthly = aPay * .0525 + 37.92;
} else if (grossPay >= 1083 && grossPay < 1667) {
double aPay = grossPay - 1083;
monthly = aPay * .059 + 46.67;
} else if (grossPay >= 1667 && grossPay < 7500) {
double aPay = grossPay - 1667;
monthly = aPay * .0685 + 81.08;
} else if (grossPay >= 7500 && grossPay < 8333) {
double aPay = grossPay - 7500;
monthly = aPay * .0764 + 480.67;
} else if (grossPay >= 8333 && grossPay < 12500) {
double aPay = grossPay - 8333;
monthly = aPay * .0814 + 544.33;
} else if (grossPay >= 12500) {
double aPay = grossPay - 12500;
monthly = aPay * .0935 + 883.67;
}
return monthly;
}
public double computeFederalTax() {
double federal = 0;
double aPay = grossPay - 283.33;
if (aPay > 0 && aPay <= 51) {
double newaPay = aPay - 0;
federal = newaPay * 0 + 0;
} else if (aPay > 51 && aPay <= 195) {
double newaPay = aPay - 51;
federal = newaPay * .1 + 0;
} else if (aPay > 195 && aPay <= 645) {
double newaPay = aPay - 195;
federal = newaPay * .15 + 14.40;
} else if (aPay > 645 && aPay <= 1482) {
double newaPay = aPay - 645;
federal = newaPay * .25 + 81.90;
} else if (aPay > 1482 && aPay <= 3131) {
double newaPay = aPay - 1482;
federal = newaPay * .28 + 291.15;
} else if (aPay > 3131 && aPay <= 6763) {
double newaPay = aPay - 3131;
federal = newaPay * .33 + 752.87;
} else if (aPay > 6763) {
double newaPay = aPay - 6763;
federal = newaPay * .35 + 1951.43;
}
return federal;
}
public double computeNetPay() {
double net = 0;
net = grossPay - computeFederalTax() - computeStateTax();
return net;
}
public String toString() {
return ", Net Pay: " + prec2.format(computeNetPay() + ", monthly state tax: "
+ prec2.format(computeStateTax() + ", monthly federal tax: "
+ prec2.format(computeFederalTax())));
}
}
/*
* To change this template, choose Tools | Templates
* and open the template in the editor.
*/
package lab08;
/*
* To change this template, choose Tools | Templates
* and open the template in the editor.
*/
import java.text.*;
/**
*
* @author Tim
*/
public class Taxpayer {
protected String name;
protected int ssNumber;
protected double grossPay;
protected String occupation;
protected String type;
protected DecimalFormat prec2 = new DecimalFormat("$#.00");
public Taxpayer(String nam, int ssN, double gp, String typ, String occ ) {
this.name = nam;
this.ssNumber = ssN;
this.grossPay = gp;
this.type = typ;
this.occupation = occ;
}
public String getOcc(){
return occupation;
}
public String getName() {
return name;
}
public String getSSNumber() {
String SSNumber = "";
SSNumber = Integer.toString(ssNumber);
return SSNumber;
}
public double getGrossPay() {
return grossPay;
}
public String getTyp(){
return type;
}
public double computeTax() {
return 0.0;
}
public String toString() {
return "name: " + name + ", SSN: " + ssNumber + "occupation: " + occupation + "Taxpayer Type: "
+ type + " grosspay: " + prec2.format(grossPay);
}
}
/*
* To change this template, choose Tools | Templates
* and open the template in the editor.
*/
package lab08;
/**
*
* @author wichert22
*/
public class WeeklyTaxpayer extends Taxpayer {
public WeeklyTaxpayer(String nam, int ssN, double gp, String typ, String occ) {
super (nam, ssN, gp, typ, occ);
}
public double computeStateTax() {
double weekly = 0;
if (grossPay >= 0 && grossPay < 154) {
double aPay = grossPay - 0;
weekly = aPay * .04 + 0;
} else if (grossPay >= 154 && grossPay < 212) {
double aPay = grossPay - 154;
weekly = aPay * .045 + 6.15;
} else if (grossPay >= 212 && grossPay < 250) {
double aPay = grossPay - 212;
weekly = aPay * .0525 + 8.75;
} else if (grossPay >= 250 && grossPay < 385) {
double aPay = grossPay - 250;
weekly = aPay * .059 + 10.77;
} else if (grossPay >= 385 && grossPay < 1731) {
double aPay = grossPay - 385;
weekly = aPay * .0685 + 18.71;
} else if (grossPay >= 1731 && grossPay < 1923) {
double aPay = grossPay - 1731;
weekly = aPay * .0764 + 110.92;
} else if (grossPay >= 1923 && grossPay < 2885) {
double aPay = grossPay - 1923;
weekly = aPay * .0814 + 125.62;
} else if (grossPay >= 2885) {
double aPay = grossPay - 2885;
weekly = aPay * .0935 + 203.92;
}
return weekly;
}
public double computeFederalTax() {
double federal = 0;
double aPay = grossPay - 65.38;
if (aPay > 0 && aPay <= 51) {
double newaPay = aPay - 0;
federal = newaPay * 0 + 0;
} else if (aPay > 51 && aPay <= 195) {
double newaPay = aPay - 51;
federal = newaPay * .1 + 0;
} else if (aPay > 195 && aPay <= 645) {
double newaPay = aPay - 195;
federal = newaPay * .15 + 14.40;
} else if (aPay > 645 && aPay <= 1482) {
double newaPay = aPay - 645;
federal = newaPay * .25 + 81.90;
} else if (aPay > 1482 && aPay <= 3131) {
double newaPay = aPay - 1482;
federal = newaPay * .28 + 291.15;
} else if (aPay > 3131 && aPay <= 6763) {
double newaPay = aPay - 3131;
federal = newaPay * .33 + 752.87;
} else if (aPay > 6763) {
double newaPay = aPay - 6763;
federal = newaPay * .35 + 1951.43;
}
return federal;
}
public double computeNetPay() {
double net = 0;
net = grossPay - computeFederalTax() - computeStateTax();
return net;
}
public String toString() {
return ", tax: " + prec2.format(computeNetPay() + ", weekly state tax: "
+ prec2.format(computeStateTax() + ", weekly federal tax: "
+ prec2.format(computeFederalTax())));
}
}

New Topic/Question
Reply




MultiQuote






|