Question
I need following: Create JavaFX (GUI) application which can be used to calculate exactly the amount of deductions that should be taken from their salary.
I need following:
Create JavaFX (GUI) application which can be used to calculate exactly the amount of deductions that should be taken from their salary.
The GUI at a minimum should include the following controls: Textfield, TextArea, Radio Button, Combo Boxes, Buttons and Slider.
The application will accept:
- Gross salary
- Marital status (Default: Single),
- Number of dependants / children (Default: 0),
- Upper and lower rates of tax (these should be set at defaults of 20% for salary up to 33800 and 40% for above 33800 but the user may change them when required).
The application should load and display all the relevant controls in an aesthetically pleasing layout and use the Grid layout manager.
Specific controls should contain default values, for example the upper and lower rates of tax.
The application will allow for different tax free allowances for single (3300) and married people (4950).
These allowances should be displayed.
Each number of dependents will increase the tax free allowance by 100.
On pressing a Button named calculate, the amount of tax an individual pays should be calculated and displayed in the relevant text area.
The net pay will also be displayed.
When the Reset button is pressed the application should reset all control values to their default values.
So far I have this, calculations are not working as expected. Can someone help please?
Code so far:
package taxcalc;
import javafx.application.Application;
import javafx.event.ActionEvent;
import javafx.event.EventHandler;
import javafx.geometry.Insets;
import javafx.geometry.Pos;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.scene.control.ComboBox;
import javafx.scene.control.Label; import javafx.scene.control.RadioButton; import javafx.scene.control.Slider; import javafx.scene.control.TextArea;
import javafx.scene.control.TextField; import javafx.scene.control.ToggleGroup;
import javafx.scene.layout.GridPane;
import javafx.scene.layout.HBox;
import javafx.scene.text.*;
import javafx.stage.Stage; import javafx.util.StringConverter;
/** * * @author petranew */ public class TaxCalc extends Application{
public static void main(String[] args) {
launch(args);
}
@Override
public void start(Stage primaryStage) throws Exception {
primaryStage.setTitle("Tax Calculation");
primaryStage.show();
GridPane grid = new GridPane();
grid.setAlignment(Pos.CENTER);
grid.setHgap(10);
grid.setVgap(10);
grid.setPadding(new Insets(25, 25, 25, 25));
Scene scene = new Scene(grid, 800, 600);
Text scenetitle = new Text("Tax Calculation Sytem");
scenetitle.setFont(Font.font("Calibri", FontWeight.BOLD, 20));
grid.add(scenetitle, 0, 0, 2, 1);
//add gross salary
Label grossSalary = new Label("Gross Salary:");
grid.add(grossSalary, 0, 1);
final TextField grossSalaryField = new TextField();
grid.add(grossSalaryField, 1, 1);
//Marital status (Default: Single) Label maritalStatus = new Label("Marital Status:"); grid.add(maritalStatus, 0, 2); Slider slider = new Slider(0, 2, 0); slider.setMin(0); slider.setMax(2); slider.setValue(0); slider.setMinorTickCount(0); slider.setMajorTickUnit(1); slider.setSnapToTicks(true); slider.setShowTickMarks(true); slider.setShowTickLabels(true);
slider.setLabelFormatter(new StringConverter
return "Divorced"; }
@Override public Double fromString(String s) { switch (s) { case "Single": return 0d; case "Married": return 1d;
default: return 3d; } } });
slider.setMinWidth(350);
HBox layout = new HBox(slider); layout.setPadding(new Insets(30));
primaryStage.setScene(new Scene(layout)); primaryStage.show(); grid.add(slider, 1, 2);
//Number of dependants / children (Default: 0)
Label NumOfDep = new Label("Number of dependants / children:");
grid.add(NumOfDep, 0, 3);
final ComboBox NumOfDepComboBox = new ComboBox(); NumOfDepComboBox.getItems().addAll( "0", "1", "2", "3", "4", "5", "6" );
NumOfDepComboBox.setValue("0");
grid.add(NumOfDepComboBox, 1,3);
//Upper rates of tax
Label upperTaxRate = new Label("Upper rates of tax :");
grid.add(upperTaxRate, 0, 4);
final TextField upperTaxRateField = new TextField();
upperTaxRateField.setText("40");
grid.add(upperTaxRateField, 1, 4);
//Lower rates of tax
Label lowerTaxRate = new Label("Lower rates of tax :");
grid.add(lowerTaxRate, 0, 5);
final TextField lowerTaxRateField = new TextField();
lowerTaxRateField.setText("20");
grid.add(lowerTaxRateField, 1, 5);
//Tax allowance
Label taxAllow = new Label("Tax allowance: "); grid.add(taxAllow, 0, 6); ToggleGroup group = new ToggleGroup(); RadioButton button1 = new RadioButton(" 3300"); button1.setToggleGroup(group); button1.setSelected(true); RadioButton button2 = new RadioButton(" 4950"); button2.setToggleGroup(group); grid.add(button1, 1, 6); grid.add(button2, 1, 7);
// Add Text Area TextArea theArea = new TextArea(); theArea.setEditable(false); theArea.setWrapText(true); // New line of the text exceeds the text area theArea.wrapTextProperty(); //theArea.setPrefRowCount(0); theArea.setPrefColumnCount(0); grid.add(theArea, 1, 10);
// Add label for displaying tax
final Label taxLbl = new Label();
// Add label for displaying net salary
final Label netSalLbl = new Label();
//Add Calculate Button
Button btnCalc = new Button("Calculate");
HBox hbBtn = new HBox(10);
hbBtn.setAlignment(Pos.BOTTOM_RIGHT);
hbBtn.getChildren().add(btnCalc);
grid.add(hbBtn, 0, 8);
//Add Reset Button
Button btnReset = new Button("Reset");
HBox resetBtn = new HBox(10);
resetBtn.setAlignment(Pos.BOTTOM_LEFT);
resetBtn.getChildren().add(btnReset);
grid.add(resetBtn, 1, 8);
btnReset.setOnAction(new EventHandler
@Override
public void handle(ActionEvent e) {
grossSalaryField.setText("");
//cmb.setValue("Single");
NumOfDepComboBox.setValue("0");
upperTaxRateField.setText("40");
lowerTaxRateField.setText("20");
taxLbl.setText("");
netSalLbl.setText("");
}
});
btnCalc.setOnAction(new EventHandler
@Override
public void handle(ActionEvent e) {
int upper=0;
int lower = 0;
int grossSal = 0;
int dependants = 0;
int taxableSal = 0;
double tax = 0.00;
double netSal = 0.00;
// actiontarget.setText("Sign in button pressed");
if ((null!=upperTaxRateField.getText() && !upperTaxRateField.getText().isEmpty())) {
upper = Integer.parseInt(upperTaxRateField.getText());
}
if ((null!=lowerTaxRateField.getText() && !lowerTaxRateField.getText().isEmpty())) {
lower = Integer.parseInt(lowerTaxRateField.getText());
}
int avg = (upper+lower)/2;
//if ((null!=NumOfDepField.getText() && !NumOfDepField.getText().isEmpty())) {
//dependants = Integer.parseInt(NumOfDepField.getText());
//}
if ((null!=lowerTaxRateField.getText() && !grossSalaryField.getText().isEmpty())) {
grossSal = Integer.parseInt(grossSalaryField.getText());
taxableSal = grossSal - dependants*100;
tax = taxableSal*avg/100;
netSal = (int) (grossSal - tax);
}
theArea.setText("The tax calculated is: "+tax); theArea.appendText(" The net salary is: "+netSal);
}
});
primaryStage.setScene(scene);
}
}
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started