(Enhanced Dental Payment Application) Modify the Dental Payment application from this tutorial to include additional services, as shown in Fig. 7.29. Add the proper functionality (using if statements) to determine whether any of the new JCheckBoxes are selected and, if so, add the price of the service to the total bill. As in the original application, a message dialog should be displayed if the patient’s name is missing or if none of the JCheckBoxes are selected.



```

a) Copying the template to your working directory. Copy the C:Examples Tutorial07ExercisesDentalPaymentEnhanced directory to your C:Simply- Java directory.

b) Opening the template file. Open the DentalPayment.java file in your text editor.

c) Customizing the Fluoride JCheckBox and its price JLabel. In line 128 set the bounds property of fluorideJCheckBox to 16, 210, 140, 24. In line 129 set the text property of fluorideJCheckBox to "Fluoride". The price for a fluoride treatment is $50. In line 135 set the text property of fluoridePriceJLabel to "$50".

d) Customizing the Root Canal JCheckBox and its price JLabel. In line 141 set the bounds property of rootCanalJCheckBox to 16, 242, 140, 24. In line 142 set the text property of rootCanalJCheckBox to "Root Canal". The price for a root canal treat- ment is $225. In line 148 set the text property of rootCanalJPriceLabel to "$225".

e) Customizing the Other JCheckBox, its price JLabel and a JTextField for the user to input a price. In l

```
1 // DentalPayment.java
2 // This application calculates the total cost of the bill for a
3 // patient at a dental office.
4 import java.awt.*;
5 import java.awt.event.*;
6 import java.text.*;
7 import javax.swing.*;
8
9 public class DentalPayment extends JFrame
10 {
11 // JLabel that displays header on application window
12 private JLabel dentalPaymentFormJLabel;
13
14 // JLabel and JTextField for patient name
15 private JLabel patientNameJLabel;
16 private JTextField patientNameJTextField;
17
18 // JCheckBox and JLabel for cleaning
19 private JCheckBox cleaningJCheckBox;
20 private JLabel cleaningPriceJLabel;
21
22 // JCheckBox and JLabel for cavity filling
23 private JCheckBox cavityFillingJCheckBox;
24 private JLabel cavityFillingPriceJLabel;
25
26 // JCheckBox and JLabel for X-Ray
27 private JCheckBox xRayJCheckBox;
28 private JLabel xRayPriceJLabel;
29
30 // JCheckBox and JLabel for fluoride
31 private JCheckBox fluorideJCheckBox;
32 private JLabel fluoridePriceJLabel;
33
34 // JCheckBox and JLabel for root canal
35 private JCheckBox rootCanalJCheckBox;
36 private JLabel rootCanalPriceJLabel;
37
38 // JCheckBox, JLabel and JTextField for other services
39 private JCheckBox otherJCheckBox;
40 private JLabel otherPriceJLabel;
41 private JTextField otherPriceJTextField;
42
43 // JLabel and JTextField for total fee
44 private JLabel totalJLabel;
45 private JTextField totalJTextField;
46
47 // JButton to initiate calculation of fee
48 private JButton calculateJButton;
49
50 // no-argument constructor
51 public DentalPayment()
52 {
53 createUserInterface();
54 }
55
56 // create and position GUI components; register event handlers
57 private void createUserInterface()
58 {
59 // get content pane for attaching GUI components
60 Container contentPane = getContentPane();
61
62 // enable explicit positioning of GUI components
63 contentPane.setLayout( null );
64
65 // set up dentalPaymentFormJLabel
66 dentalPaymentFormJLabel = new JLabel();
67 dentalPaymentFormJLabel.setBounds( 19, 19, 235, 28 );
68 dentalPaymentFormJLabel.setText( "Dental Payment Form" );
69 dentalPaymentFormJLabel.setFont(
70 new Font( "Default", Font.PLAIN, 22 ) );
71 dentalPaymentFormJLabel.setHorizontalAlignment(
72 JLabel.CENTER );
73 contentPane.add( dentalPaymentFormJLabel );
74
75 // set up patientNameJLabel
76 patientNameJLabel = new JLabel();
77 patientNameJLabel.setBounds( 19, 65, 91, 21 );
78 patientNameJLabel.setText( "Patient name:" );
79 contentPane.add( patientNameJLabel );
80
81 // set up patientNameJTextField
82 patientNameJTextField = new JTextField();
83 patientNameJTextField.setBounds( 132, 65, 117, 21 );
84 contentPane.add( patientNameJTextField );
85
86 // set up cleaningJCheckBox
87 cleaningJCheckBox = new JCheckBox();
88 cleaningJCheckBox.setBounds( 16, 112, 140, 24 );
89 cleaningJCheckBox.setText( "Cleaning" );
90 contentPane.add( cleaningJCheckBox );
91
92 // set up cleaningPriceJLabel
93 cleaningPriceJLabel = new JLabel();
94 cleaningPriceJLabel.setBounds( 211, 112, 38, 24 );
95 cleaningPriceJLabel.setText( "$35" );
96 cleaningPriceJLabel.setHorizontalAlignment( JLabel.RIGHT );
97 contentPane.add( cleaningPriceJLabel );
98
99 // set up cavityFillingJCheckBox
100 cavityFillingJCheckBox = new JCheckBox();
101 cavityFillingJCheckBox.setBounds( 16, 144, 140, 24 );
102 cavityFillingJCheckBox.setText( "Cavity Filling" );
103 contentPane.add( cavityFillingJCheckBox );
104
105 // set up cavityFillingPriceJLabel
106 cavityFillingPriceJLabel = new JLabel();
107 cavityFillingPriceJLabel.setBounds( 211, 144, 38, 24 );
108 cavityFillingPriceJLabel.setText( "$150" );
109 cavityFillingPriceJLabel.setHorizontalAlignment(
110 JLabel.RIGHT );
111 contentPane.add( cavityFillingPriceJLabel );
112
113 // set up xRayJCheckBox
114 xRayJCheckBox = new JCheckBox();
115 xRayJCheckBox.setBounds( 16, 178, 140, 24 );
116 xRayJCheckBox.setText( "X-Ray" );
117 contentPane.add( xRayJCheckBox );
118
119 // set up xRayPriceJLabel
120 xRayPriceJLabel = new JLabel();
121 xRayPriceJLabel.setBounds( 211, 178, 38, 24 );
122 xRayPriceJLabel.setText( "$85" );
123 xRayPriceJLabel.setHorizontalAlignment( JLabel.RIGHT );
124 contentPane.add( xRayPriceJLabel );
125
126 // set up fluorideJCheckBox
127 fluorideJCheckBox = new JCheckBox();
128 fluorideJCheckBox.setBounds( 16, 210, 140, 24 );
129 fluorideJCheckBox.setText( "Fluoride" );
130 contentPane.add( fluorideJCheckBox );
131
132 // set up fluoridePriceJLabel
133 fluoridePriceJLabel = new JLabel();
134 fluoridePriceJLabel.setBounds( 211, 210, 38, 24 );
135 fluoridePriceJLabel.setText( "$50" );
136 fluoridePriceJLabel.setHorizontalAlignment( JLabel.RIGHT );
137 contentPane.add( fluoridePriceJLabel );
138
139 // set up rootCanalJCheckBox
140 rootCanalJCheckBox = new JCheckBox();
141 rootCanalJCheckBox.setBounds( 16, 242, 140, 24 );
142 rootCanalJCheckBox.setText( "Root Canal" );
143 contentPane.add( rootCanalJCheckBox );
144
145 // set up rootCanalPriceJLabel
146 rootCanalPriceJLabel = new JLabel();
147 rootCanalPriceJLabel.setBounds( 211, 242, 38, 24 );
148 rootCanalPriceJLabel.setText( "$225" );
149 rootCanalPriceJLabel.setHorizontalAlignment( JLabel.RIGHT );
150 contentPane.add( rootCanalPriceJLabel );
151
152 // set up otherJCheckBox
153 otherJCheckBox = new JCheckBox();
154 otherJCheckBox.setBounds( 16, 274, 140, 24 );
155 otherJCheckBox.setText( "Other" );
156 contentPane.add( otherJCheckBox );
157
158 // set up otherPriceJLabel
159 otherPriceJLabel = new JLabel();
160 otherPriceJLabel.setBounds( 206, 274, 32, 24 );
161 otherPriceJLabel.setText( "$" );
162 contentPane.add( otherPriceJLabel );
163
164 // set up otherPriceJTextField
165 otherPriceJTextField = new JTextField();
166 otherPriceJTextField.setBounds( 214, 274, 34, 24 );
167 otherPriceJTextField.setHorizontalAlignment(
168 JTextField.RIGHT );
169 contentPane.add( otherPriceJTextField );
170
171 // set up totalJLabel
172 totalJLabel = new JLabel();
173 totalJLabel.setBounds( 144, 314, 41, 21 );
174 totalJLabel.setText( "Total:" );
175 contentPane.add( totalJLabel );
176
177 // set up totalJTextField
178 totalJTextField = new JTextField();
179 totalJTextField.setBounds( 192, 314, 56, 21 );
180 totalJTextField.setEditable( false );
181 totalJTextField.setHorizontalAlignment( JTextField.CENTER );
182 contentPane.add( totalJTextField );
183
184 // set up calculateJButton
185 calculateJButton = new JButton();
186 calculateJButton.setBounds( 155, 346, 94, 24 );
187 calculateJButton.setText( "Calculate" );
188 contentPane.add( calculateJButton );
189 calculateJButton.addActionListener(
190
191 new ActionListener() // anonymous inner class
192 {
193 // event handler called when user clicks calculateJButton
194 public void actionPerformed( ActionEvent event )
195 {
196 calculateJButtonActionPerformed( event );
197 }
198
199 } // end anonymous inner class
200
201 ); // end call to addActionListener
202
203 // set properties of application’s window
204 setTitle( "Dental Payment" ); // set title bar string
205 setSize( 272, 409 ); // set window size
206 setVisible( true ); // display window
207
208 } // end method createUserInterface
209
210 // calculate cost of patient's visit
211 private void calculateJButtonActionPerformed( ActionEvent event )
212 {
213 // get patient's name
214 String patient = patientNameJTextField.getText();
215
216 // display error message if no name entered or no box selected
217 if ( ( patient.equals( "" ) ) ||
218 ( !cleaningJCheckBox.isSelected() &&
219 !cavityFillingJCheckBox.isSelected() &&
220 !xRayJCheckBox.isSelected() &&
221
222
223
224 {

!fluorideJCheckBox.isSelected() &&
!rootCanalJCheckBox.isSelected() &&
!otherJCheckBox.isSelected() ) )
225 // display error message
226 JOptionPane.showMessageDialog( null,
227 "Please enter a name and check at least one item.",
228 "Missing information", JOptionPane.ERROR_MESSAGE );
229 }
230 else // otherwise, do calculations
231 {
232 double total = 0.0; // sum of all services provided
233
234 // if patient had a cleaning
235 if ( cleaningJCheckBox.isSelected() )
236 {
237 total += 35; // add 35 to total
238 }
239
240 // if patient had cavity filled
241 if ( cavityFillingJCheckBox.isSelected() )
242 {
243 total += 150; // add 150 to total
244 }
245
246 // if patient had x-ray taken
24

Computer Science & Information Technology

You might also like to view...

Which of the following layers is the presentation layer?

a) Layer 3 b) Layer 5 c) Layer 6 d) Layer 7

Computer Science & Information Technology

Which of the following reveals a system vulnerability and is often documented, either by the manufacturer or by an attacker?

A. hole B. exploit C. break-in D. attack

Computer Science & Information Technology