This is direction: Modifying the calculateJButto nActionPerforme d method.
Modify the if statement
that starts in line 217 to check if the user has not entered a name and/or
selected any JCheckBoxes. Also add code to the
calculateJButto nActionPerforme d
method (starting in line 252) that determines whether the new JCheckBoxes
have been selected. This can be done using if statements that are similar to
the ones
already in the method. Use the if statements to update the bill amount. The
bill
amount should be displayed in the totalPriceJText Field with the format
"$0.00".
//Source Code
// Exercise 5 DentalPayment.j ava
// This application calculates the total cost of the bill for a
// patient at a dental office.
import java.awt.*;
import java.awt.event. *;
import java.text.*;
import javax.swing.*;
public class DentalPayment extends JFrame
{
// JLabel that displays header on application window
private JLabel dentalPaymentFo rmJLabel;
// JLabel and JTextField for patient name
private JLabel patientNameJLab el;
private JTextField patientNameJTex tField;
// JCheckBox and JLabel for cleaning
private JCheckBox cleaningJCheckB ox;
private JLabel cleaningPriceJL abel;
// JCheckBox and JLabel for cavity filling
private JCheckBox cavityFillingJC heckBox;
private JLabel cavityFillingPr iceJLabel;
// JCheckBox and JLabel for X-Ray
private JCheckBox xRayJCheckBox;
private JLabel xRayPriceJLabel ;
// JCheckBox and JLabel for fluoride
private JCheckBox fluorideJCheckB ox;
private JLabel fluoridePriceJL abel;
// JCheckBox and JLabel for root canal
private JCheckBox rootCanalJCheck Box;
private JLabel rootCanalPriceJ Label;
// JCheckBox, JLabel and JTextField for other services
private JCheckBox otherJCheckBox;
private JLabel otherPriceJLabe l;
private JTextField otherPriceJText Field;
// JLabel and JTextField for total fee
private JLabel totalJLabel;
private JTextField totalJTextField ;
// JButton to initiate calculation of fee
private JButton calculateJButto n;
// no-argument constructor
public DentalPayment()
{
createUserInter face();
}
// create and position GUI components; register event handlers
private void createUserInter face()
{
// get content pane for attaching GUI components
Container contentPane = getContentPane( );
// enable explicit positioning of GUI components
contentPane.set Layout( null );
// set up dentalPaymentFo rmJLabel
dentalPaymentFo rmJLabel = new JLabel();
dentalPaymentFo rmJLabel.setBou nds( 19, 19, 235, 28 );
dentalPaymentFo rmJLabel.setTex t( "Dental Payment Form" );
dentalPaymentFo rmJLabel.setFon t(
new Font( "Default", Font.PLAIN, 22 ) );
dentalPaymentFo rmJLabel.setHor izontalAlignmen t(
JLabel.CENTER );
contentPane.add ( dentalPaymentFo rmJLabel );
// set up patientNameJLab el
patientNameJLab el = new JLabel();
patientNameJLab el.setBounds( 19, 65, 91, 21 );
patientNameJLab el.setText( "Patient name:" );
contentPane.add ( patientNameJLab el );
// set up patientNameJTex tField
patientNameJTex tField = new JTextField();
patientNameJTex tField.setBound s( 132, 65, 117, 21 );
contentPane.add ( patientNameJTex tField );
// set up cleaningJCheckB ox
cleaningJCheckB ox = new JCheckBox();
cleaningJCheckB ox.setBounds( 16, 112, 140, 24 );
cleaningJCheckB ox.setText( "Cleaning" );
contentPane.add ( cleaningJCheckB ox );
// set up cleaningPriceJL abel
cleaningPriceJL abel = new JLabel();
cleaningPriceJL abel.setBounds( 211, 112, 38, 24 );
cleaningPriceJL abel.setText( "$35" );
cleaningPriceJL abel.setHorizon talAlignment( JLabel.RIGHT );
contentPane.add ( cleaningPriceJL abel );
// set up cavityFillingJC heckBox
cavityFillingJC heckBox = new JCheckBox();
cavityFillingJC heckBox.setBoun ds( 16, 144, 140, 24 );
cavityFillingJC heckBox.setText ( "Cavity Filling" );
contentPane.add ( cavityFillingJC heckBox );
// set up cavityFillingPr iceJLabel
cavityFillingPr iceJLabel = new JLabel();
cavityFillingPr iceJLabel.setBo unds( 211, 144, 38, 24 );
cavityFillingPr iceJLabel.setTe xt( "$150" );
cavityFillingPr iceJLabel.setHo rizontalAlignme nt(
JLabel.RIGHT );
contentPane.add ( cavityFillingPr iceJLabel );
// set up xRayJCheckBox
xRayJCheckBox = new JCheckBox();
xRayJCheckBox.s etBounds( 16, 178, 140, 24 );
xRayJCheckBox.s etText( "X-Ray" );
contentPane.add ( xRayJCheckBox );
// set up xRayPriceJLabel
xRayPriceJLabel = new JLabel();
xRayPriceJLabel .setBounds( 211, 178, 38, 24 );
xRayPriceJLabel .setText( "$85" );
xRayPriceJLabel .setHorizontalA lignment( JLabel.RIGHT );
contentPane.add ( xRayPriceJLabel );
// set up fluorideJCheckB ox
fluorideJCheckB ox = new JCheckBox();// Line 127
fluorideJCheckB ox.setBounds(16 ,210,140,24);
fluorideJCheckB ox.setText( "Fluoride" );
contentPane.add ( fluorideJCheckB ox );
// set up fluoridePriceJL abel
fluoridePriceJL abel = new JLabel();
fluoridePriceJL abel.setBounds( 211, 210, 38, 24 );//Line 134
fluoridePriceJL abel.setText("$ 50");
fluoridePriceJL abel.setHorizon talAlignment( JLabel.RIGHT );
contentPane.add ( fluoridePriceJL abel );
// set up rootCanalJCheck Box
rootCanalJCheck Box = new JCheckBox();//Line 140
rootCanalJCheck Box.setBounds(1 6,242,140,24);
rootCanalJCheck Box.setText("Ro ot Canal");
contentPane.add ( rootCanalJCheck Box );
// set up rootCanalPriceJ Label
rootCanalPriceJ Label = new JLabel();
rootCanalPriceJ Label.setBounds ( 211, 242, 38, 24 );//Line 148
rootCanalPriceJ Label.setText(" $225");
rootCanalPriceJ Label.setHorizo ntalAlignment( JLabel.RIGHT );
contentPane.add ( rootCanalPriceJ Label );
// set up otherJCheckBox
otherJCheckBox = new JCheckBox();//line 153
otherJCheckBox. setBounds(16,27 4,140,24);
otherJCheckBox. setText("Other" );
contentPane.add ( otherJCheckBox );
// set up otherPriceJLabe l
otherPriceJLabe l = new JLabel();//Line 159
otherPriceJLabe l .setBounds(206, 274,32,24);
otherPriceJLabe l .setText("$");
contentPane.add ( otherPriceJLabe l );
// set up otherPriceJText Field
otherPriceJText Field = new JTextField();//line 165
otherPriceJText Field.setBounds (214,274,34,24) ;
otherPriceJText Field.setHorizo ntalAlignment( JLabel.RIGHT );
contentPane.add ( otherPriceJText Field );
// set up totalJLabel
totalJLabel = new JLabel();
totalJLabel.set Bounds( 144, 314, 41, 21 );
totalJLabel.set Text( "Total:" );
contentPane.add ( totalJLabel );
// set up totalJTextField
totalJTextField = new JTextField();
totalJTextField .setBounds( 192, 314, 56, 21 );
totalJTextField .setEditable( false );
totalJTextField .setHorizontalA lignment( JTextField.CENT ER );
contentPane.add ( totalJTextField );
// set up calculateJButto n
calculateJButto n = new JButton();
calculateJButto n.setBounds( 155, 346, 94, 24 );
calculateJButto n.setText( "Calculate" );
contentPane.add ( calculateJButto n );
calculateJButto n.addActionList ener(
new ActionListener( ) // anonymous inner class
{
// event handler called when user clicks calculateJButto n
public void actionPerformed ( ActionEvent event )
{
calculateJButto nActionPerforme d( event );
}
} // end anonymous inner class
); // end call to addActionListen er
// set properties of application's window
setTitle( "Dental Payment" ); // set title bar string
setSize( 272, 409 ); // set window size
setVisible( true ); // display window
} // end method createUserInter face
// calculate cost of patient's visit
private void calculateJButto nActionPerforme d( ActionEvent event )
{
// get patient's name
String patient = patientNameJTex tField.getText( );
// Line 216 display error message if no name entered or no box
selected
if ( ( patient.equals( "" ) ) ||
( !cleaningJCheck Box.isSelected( ) &&
!cavityFillingJ CheckBox.isSele cted() &&
!xRayJCheckBox. isSelected() ) )
{
// display error message
JOptionPane.sho wMessageDialog( null,
"Please enter a name and check at least one item.",
"Missing information", JOptionPane.WAR NING_MESSAGE );
}
else // otherwise, do calculations
{
double total = 0.0; // sum of all services provided
// if patient had a cleaning
if ( cleaningJCheckB ox.isSelected() )
{
total += 35; // add 35 to total
}
// if patient had cavity filled
if ( cavityFillingJC heckBox.isSelec ted() )
{
total += 150; // add 150 to total
}
// if patient had x-ray taken
if ( xRayJCheckBox.i sSelected() )
{
total += 85; // add 85 to total
}
// Line 252 if patient had fluoride cleaning
if ( fluorideJCheckB ox.isSelected() )
{
total += 50; // add 50 to total
}
// if patient had root canal
if ( rootCanalJCheck Box.isSelected( ) )
{
total += 225; // add 225 to total
}
// if patient had other service performed
if ( otherJCheckBox. isSelected() )
{
total += ; // add 50 to total ------ ###ERROR###
}
// specify display format
DecimalFormat dollars = new DecimalFormat( "$0.00" );
// display total
totalJTextField .setText( dollars.format( total ) );
} // end else
} // end method calculateJButto nActionPerforme d
// main method
public static void main( String[] args )
{
DentalPayment application = new DentalPayment() ;
application.set DefaultCloseOpe ration( JFrame.EXIT_ON_ CLOSE );
} // end method main
} // end class DentalPayment