Write an application that allows the user to choose insurance options in JcheckB
ID: 3557529 • Letter: W
Question
Write an 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 typesHMO(health maintenance organization) or PPO(preferred provider organization). Use regular (single) JcheckBoxes for dental insurance and vision insurance options, the user can select one options, both options, or neither options, As the user selects each options, 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. When a user deselects an items, make the text field blank.
Explanation / Answer
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
public class JInsurance extends
JFrame implements ItemListener
{
int hmoPrice = 200, ppoPrice =
800;
int[] additPrice ={0,20,75};
int basePrice = 0, totalPrice = basePrice;
JCheckBox hmoBox = new
JCheckBox("HMO $" + hmoPrice, false);
JCheckBox ppoBox = new
JCheckBox("PPO $" + ppoPrice, false);
JLabel additLabel = new JLabel ("Additional Coverage: Make a selection");
String[] additNames = {"None $" + additPrice[0], "Vision $" + additPrice[1], "Dental $" + additPrice[2]};
JComboBox additBox = new JComboBox(additNames);
JLabel insOptionsLabel = new JLabel("Insurance Options:");
JTextField totPrice = new JTextField(10);
JTextField message = new JTextField(30);
JLabel optionExplainLabel = new JLabel("Base price for insurance with HMO $" + basePrice + ".");
JLabel optionExplainLabel2 = new JLabel("Optionally, you can choose HMO or PPO only");
public JInsurance()
{
super("Insurance Price Estimator");
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
JPanel pane = new JPanel();
ButtonGroup insGroup = new ButtonGroup();
insGroup.add(hmoBox);
insGroup.add(ppoBox);
pane.add(hmoBox); pane.add(ppoBox);
pane.add(additLabel);
pane.add(additBox);
pane.add(insOptionsLabel);
pane.add (optionExplainLabel);
pane.add (optionExplainLabel2);
pane.add(totPrice);
pane.add(message);
totPrice.setText("$" + totalPrice);
hmoBox.addItemListener (this);
ppoBox.addItemListener (this);
additBox.addItemListener (this);
setContentPane(pane);
}
public static void main(String[] args)
{
JFrame aFrame = new JInsurance();
aFrame.setSize(400,250);
aFrame.setVisible(true);
}
public void itemStateChanged
(ItemEvent event)
{
Object source = event.getSource();
int select = event.getStateChange(); if(source == additBox)
{
int addNum = additBox.getSelectedIndex();
addPrice = additPrice[addNum];
message.setText("Action: Insurance options changed $" + addPrice + " added");
}
else
message.setText("Action: Insurance type changed");
totalPrice = basePrice +additPrice;
if(ppoBox.isSelected())
totalPrice += ppoPrice;
else if (fishBox.isSelected())
totalPrice += fishPrice;
}
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.