In: Computer Science
Java 14-8
Finish the JInsurance application that allows the user to choose insurance options in JCheckBoxes. Use a ButtonGroup to allow the user to select only one of two insurance types—HMO (health maintenance organization) or PPO (preferred provider organization). Use regular (single) JCheckBoxes 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 $200per month, the PPO costs $600 per month, the dental coverage adds $75 per month, and the vision care adds $20 per month. When a user deselects an item, make the text field blank.
----------------------------------------------------------Code given-----------------------------------------------------
import java.awt.*;
import javax.swing.*;
import java.awt.event.*;
public class JInsurance extends JFrame implements ItemListener
{
FlowLayout flow = new FlowLayout();
ButtonGroup insGrp = new ButtonGroup();
JCheckBox hmo = new JCheckBox("HMO", false);
JCheckBox ppo = new JCheckBox("PPO", false);
JCheckBox dental = new JCheckBox("Dental", false);
JCheckBox vision = new JCheckBox("Vision", false);
JTextField insChoice = new JTextField(20);
String output, insChosen;
public JInsurance() {
// Write your code here
}
public static void main(String[] arguments) {
JInsurance iFrame = new JInsurance();
iFrame.setSize(400, 100);
iFrame.setVisible(true);
}
@Override
public void itemStateChanged(ItemEvent check) {
// Write your code here
}
}
/*
* Java program that display a gui window that contains
* four check boxes. As user select the check boxes.
* the text boxes will display the price of the service
* */
import java.awt.*;
import javax.swing.*;
import java.awt.event.*;
public class JInsurance extends JFrame implements
ItemListener
{
FlowLayout flow = new FlowLayout();
ButtonGroup insGrp = new ButtonGroup();
JCheckBox hmo = new JCheckBox("HMO", false);
JCheckBox ppo = new JCheckBox("PPO", false);
JCheckBox dental = new JCheckBox("Dental",
false);
JCheckBox vision = new JCheckBox("Vision",
false);
JTextField insChoice = new JTextField(20);
String output, insChosen;
//set contant values
final int HMO_PRICE = 200;
final int PPO_PRICE = 600;
final int DENTAL_PRICE = 75;
final int VISION_PRICE = 20;
/*Constructor */
public JInsurance()
{
//set title
setTitle("INSURANCE");
//set flow as
layout
setLayout(flow);
//add hmo and ppo to the
button group
insGrp.add(hmo);
insGrp.add(ppo);
//Add ItemListener to the
dental and vision check boxes
hmo.addItemListener(this);
ppo.addItemListener(this);
//Add hmo and ppo to
frame
add(hmo);
add(ppo);
//Add dental and vision to
frame
add(dental);
add(vision);
//Add ItemListener to the
dental and vision check boxes
dental.addItemListener(this);
vision.addItemListener(this);
//add the text field
to the frame
add(insChoice);
}
public static void main(String[] arguments)
{
JInsurance iFrame = new
JInsurance();
iFrame.setSize(400, 100);
iFrame.setVisible(true);
}
/*Implement the itemStateChanged method
*/
public void itemStateChanged(ItemEvent check)
{
output="";
//checking if hmi is selected
if(hmo.isSelected())
output=String.valueOf("HMO $:"+HMO_PRICE);
//checking if ppo is selected
if(ppo.isSelected())
output=String.valueOf("PPO $:"+PPO_PRICE);
//checking if dental is
selected
if(dental.isSelected())
output+=String.valueOf("Dental $:"+DENTAL_PRICE);
//checking if vision is
selected
if(vision.isSelected())
output+=String.valueOf("Vision $:"+VISION_PRICE);
/*Set set output to the insChoice
*/
insChoice.setText(output);
}
}
Sample Output: