In: Computer Science
Using NetBeans IDE, write a JavaFX application that allows the user to choose insurance options. Use a ToggleGroup to allow the user to select only one of two insurance types—HMO (health maintenance organization) or PPO (preferred provider organization). Use CheckBoxes for dental insurance and vision insurance options; the user can select one option, both options, or neither option. As the user selects each option, display its name and price in a text field; the HMO costs $200 per month, the PPO costs $600 per month, the dental coverage adds $75 per month, and the vision care adds $20 per month. Save the project as FXInsurance.
//Java code
import javafx.application.Application;
import javafx.event.ActionEvent;
import javafx.event.EventHandler;
import javafx.geometry.Insets;
import javafx.scene.Scene;
import javafx.scene.control.*;
import javafx.scene.layout.HBox;
import javafx.scene.layout.VBox;
import javafx.stage.Stage;
import java.text.DecimalFormat;
public class FXInsurance extends Application {
/**
* HMO costs $200 per month, the PPO costs $600 per month, the dental
* coverage adds $75 per month, and the vision care adds $20 per month.
*/
//constants
private final double HMO = 200,PPO=600;
private final double DENTAL_COVERAGE = 75;
private final double VISION_CARE = 20;
@Override
public void start(Stage primaryStage) throws Exception {
HBox box1 = new HBox();
box1.setPadding(new Insets(10,10,10,10));
HBox box2 = new HBox();
box2.setPadding(new Insets(10,10,10,10));
box2.setSpacing(10);
box1.setSpacing(10);
//Radio Buttons
RadioButton rdbHMO = new RadioButton("HMO");
RadioButton rdbPPO = new RadioButton("PPO");
ToggleGroup group = new ToggleGroup();
rdbHMO.setToggleGroup(group);
rdbPPO.setToggleGroup(group);
box1.getChildren().add(rdbHMO);
box1.getChildren().add(rdbPPO);
//CheckBoxes
CheckBox chkDentalInsurance = new CheckBox("Dental Insurance");
CheckBox chkVisionInsurance = new CheckBox("Vision Insurance");
box2.getChildren().add(chkDentalInsurance);
box2.getChildren().add(chkVisionInsurance);
HBox box3 = new HBox();
box3.setPadding(new Insets(10,10,10,10));
//textArea
TextArea txtResult = new TextArea();
txtResult.setEditable(false);
txtResult.setPrefColumnCount(300);
txtResult.setPrefRowCount(400);
//Button
box3.getChildren().add(txtResult);
HBox box4 = new HBox();
box4.setPadding(new Insets(10,10,10,10));
Button btnCalculate = new Button("Display");
box4.getChildren().add(btnCalculate);
VBox vBox = new VBox();
vBox.setPadding(new Insets(10,10,10,10));
vBox.getChildren().add(box1);
vBox.getChildren().add(box2);
vBox.getChildren().add(box3);
vBox.getChildren().add(box4);
//Scene
Scene scene = new Scene(vBox,350,250);
primaryStage.setScene(scene);
primaryStage.setTitle("FXInsurance");
primaryStage.show();
//button listener
btnCalculate.setOnAction(new EventHandler() {
double price ;
String result ;
@Override
public void handle(ActionEvent event) {
result="";
price =0;
if(rdbHMO.isSelected())
{
result +="Insurance: health maintenance organization, ";
price+= HMO;
}
if(rdbPPO.isSelected())
{
result+="Insurance : preferred provider organization,";
price+=PPO;
}
if(chkDentalInsurance.isSelected())
{
result+="\ndental insurance, ";
price+=DENTAL_COVERAGE;
}
if(chkVisionInsurance.isSelected())
{
result+="\nvision insurance, ";
price+=VISION_CARE;
}
txtResult.setText(result+"\nInsurance Price: "+ DecimalFormat.getCurrencyInstance().format(price));
}
});
}
public static void main(String[] args)
{
launch(args);
}
}
//Output
