(Credit Checker Application) Develop an application (as shown in Fig. 6.33) that a credit manager can use to determine whether a department store customer has exceeded the credit limit on a charge account. For each customer, the credit manager enters an account number (an int), a balance at the beginning of the month (a double), the total of all items charged this month (a double), the total of all credits applied to the customer’s account this month (a double) and the customer’s allowed credit limit (a double). The application should input each of these facts, calculate the new balance (= beginning balance + charges – credits), display the new balance and determine whether the new balance exceeds the customer’s credit limit. If the customer’s credit limi



a) Copying the template to your working directory. Copy the C:Examples Tutorial06ExercisesCreditChecker directory to your C:SimplyJava directory.

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

c) Coding the Calculate Balance JButton’s ActionPerformed event handler. Add the code for Steps d through f to event handler calculateJButtonActionPerformed (lines 195–199).

d) Declaring variables. Starting in line 198, insert statements that declare four double variables—startBalance, totalCharges, totalCredits and creditLimit. Assign to each of these variables the value from the corresponding JTextField (start- BalanceJTextField, totalChargesJTextField, totalCreditsJTextField and creditLimitJTextField, respectively) converted to type double.

e) Calculating and displaying the new balance. Declare a fifth double variable called newBalance to store the new balance in the account after the charges and credits have been applied. Calculate

```
1 // CreditChecker.java
2 // This application calculates a credit balance and
3 // determines whether the credit limit has been exceeded.
4 import java.awt.*;
5 import java.awt.event.*;
6 import java.text.DecimalFormat;
7 import javax.swing.*;
8
9 public class CreditChecker extends JFrame
10 {
11 // JLabel and JTextField for account number
12 private JLabel accountNumberJLabel;
13 private JTextField accountNumberJTextField;
14
15 // JLabel and JTextField for starting balance
16 private JLabel startBalanceJLabel;
17 private JTextField startBalanceJTextField;
18
19 // JLabel and JTextField for total charges
20 private JLabel totalChargesJLabel;
21 private JTextField totalChargesJTextField;
22
23 // JLabel and JTextField for total credits
24 private JLabel totalCreditsJLabel;
25 private JTextField totalCreditsJTextField;
26
27 // JLabel and JTextField for credit limit
28 private JLabel creditLimitJLabel;
29 private JTextField creditLimitJTextField;
30
31 // JLabel and JTextField for displaying new balance
32 private JLabel newBalanceJLabel;
33 private JTextField newBalanceJTextField;
34
35 // JTextField for displaying error message
36 private JTextField errorJTextField;
37
38 // JButton to initiate new balance calculation
39 private JButton calculateJButton;
40
41 // no-argument constructor
42 public CreditChecker()
43 {
44 createUserInterface();
45 }
46
47 // create and position GUI components; register event handlers
48 private void createUserInterface()
49 {
50 // get content pane for attaching GUI components
51 Container contentPane = getContentPane();
52
53 // enable explicit positioning of GUI components
54 contentPane.setLayout( null );
55
56 // set up accountNumberJLabel
57 accountNumberJLabel = new JLabel();
58 accountNumberJLabel.setBounds( 16, 16, 106, 21 );
59 accountNumberJLabel.setText( "Account number:" );
60 contentPane.add( accountNumberJLabel );
61
62 // set up accountNumberJTextField
63 accountNumberJTextField = new JTextField();
64 accountNumberJTextField.setBounds( 130, 16, 96, 21 );
65 accountNumberJTextField.setHorizontalAlignment(
66 JTextField.RIGHT );
67 contentPane.add( accountNumberJTextField );
68 accountNumberJTextField.addKeyListener(
69
70 new KeyAdapter() // anonymous inner class
71 {
72 // event handler called when key pressed in
73 // accountNumberJTextField
74 public void keyPressed( KeyEvent event )
75 {
76 accountNumberJTextFieldKeyPressed( event );
77 }
78
79 } // end anonymous inner class
80
81 ); // end call to addKeyListener
82
83 // set up startBalanceJLabel
84 startBalanceJLabel = new JLabel();
85 startBalanceJLabel.setBounds( 16, 56, 106, 21 );
86 startBalanceJLabel.setText( "Starting balance:" );
87 contentPane.add( startBalanceJLabel );
88
89 // set up startBalanceJTextField
90 startBalanceJTextField = new JTextField();
91 startBalanceJTextField.setBounds( 130, 56, 96, 21 );
92 startBalanceJTextField.setHorizontalAlignment(
93 JTextField.RIGHT );
94 contentPane.add( startBalanceJTextField );
95
96 // set up totalChargesJLabel
97 totalChargesJLabel = new JLabel();
98 totalChargesJLabel.setBounds( 16, 96, 106, 21 );
99 totalChargesJLabel.setText( "Total charges:" );
100 contentPane.add( totalChargesJLabel );
101
102 // set up totalChargesJTextField
103 totalChargesJTextField = new JTextField();
104 totalChargesJTextField.setBounds( 130, 96, 96, 21 );
105 totalChargesJTextField.setHorizontalAlignment(
106 JTextField.RIGHT );
107 contentPane.add( totalChargesJTextField );
108
109 // set up totalCreditsJLabel
110 totalCreditsJLabel = new JLabel();
111 totalCreditsJLabel.setBounds( 16, 136, 106, 21 );
112 totalCreditsJLabel.setText( "Total credits:" );
113 contentPane.add( totalCreditsJLabel );
114
115 // set up totalCreditsJTextField
116 totalCreditsJTextField = new JTextField();
117 totalCreditsJTextField.setBounds( 130, 136, 96, 21 );
118 totalCreditsJTextField.setHorizontalAlignment(
119 JTextField.RIGHT );
120 contentPane.add( totalCreditsJTextField );
121
122 // set up creditLimitJLabel
123 creditLimitJLabel = new JLabel();
124 creditLimitJLabel.setBounds( 16, 176, 106, 21 );
125 creditLimitJLabel.setText( "Credit limit:" );
126 contentPane.add( creditLimitJLabel );
127
128 // set up creditLimitJTextField
129 creditLimitJTextField = new JTextField();
130 creditLimitJTextField.setBounds( 130, 176, 96, 21 );
131 creditLimitJTextField.setHorizontalAlignment(
132 JTextField.RIGHT );
133 contentPane.add( creditLimitJTextField );
134
135 // set up newBalanceJTextField
136 newBalanceJTextField = new JTextField();
137 newBalanceJTextField.setBounds( 130, 216, 96, 21 );
138 newBalanceJTextField.setHorizontalAlignment(
139 JTextField.RIGHT );
140 newBalanceJTextField.setEditable( false );
141 contentPane.add( newBalanceJTextField );
142
143 // set up newBalanceJLabel
144 newBalanceJLabel = new JLabel();
145 newBalanceJLabel.setBounds( 16, 216, 156, 21 );
146 newBalanceJLabel.setText( "New balance:" );
147 contentPane.add( newBalanceJLabel );
148
149 // set up errorJTextField
150 errorJTextField = new JTextField();
151 errorJTextField.setBounds( 16, 256, 210, 21 );
152 errorJTextField.setHorizontalAlignment( JTextField.CENTER );
153 errorJTextField.setEditable( false );
154 contentPane.add( errorJTextField );
155
156 // set up calculateJButton and register its event handler
157 calculateJButton = new JButton();
158 calculateJButton.setBounds( 90, 296, 136, 23 );
159 calculateJButton.setText( "Calculate Balance" );
160 contentPane.add( calculateJButton );
161 calculateJButton.addActionListener(
162
163 new ActionListener() // anonymous inner class
164 {
165 // event handler called when calculateJButton is pressed
166 public void actionPerformed( ActionEvent event )
167 {
168 calculateJButtonActionPerformed( event );
169 }
170
171 } // end anonymous inner class
172
173 ); // end call to addActionListener
174
175 // set properties of application’s window
176 setTitle( "Credit Checker" ); // set title bar text
177 setSize( 250, 360 ); // set window size
178 setVisible( true ); // display window
179
180 } // end method createUserInterface
181
182 // method called when key pressed in accountNumberJTextField
183 private void accountNumberJTextFieldKeyPressed( KeyEvent event )
184 {
185 // clear all JTextFields when account number is changed
186 startBalanceJTextField.setText( "" );
187 totalChargesJTextField.setText( "" );
188 totalCreditsJTextField.setText( "" );
189
190
191
192
193 } // end method accountNumberJTextFieldKeyPressed
194
195 // method called when user clicks calculateJButton
196 private void calculateJButtonActionPerformed( ActionEvent event )
197 {
198 // obtain user input
199 double startBalance = Double.parseDouble(
200 startBalanceJTextField.getText() );
201 double totalCharges = Double.parseDouble(
202 totalChargesJTextField.getText() );
203 double totalCredits = Double.parseDouble(
204 totalCreditsJTextField.getText() );
205 double creditLimit = Double.parseDouble(
206 creditLimitJTextField.getText() );
207
208 // calculate balance after charges and credits
209 double newBalance = startBalance + totalCharges - totalCredits;
210
211 // specify floating-point number format
212 DecimalFormat dollarFormat = new DecimalFormat( "$0.00" );
213
214 // display new balance in newBalanceJTextField
215 newBalanceJTextField.setText( dollarFormat.format(
216 newBalance ) );
217
218
219
220
221
222
223
224
225 // determine whether credit limit exceeded
if ( newBalance > creditLimit )
{
// display credit limit exceeded message errorJTextField.setText( "Credit Limit Exceeded!" );
}
else
errorJTextField.setText( "" ); // clear errorJTextField
226
227 } // end method calculateJButtonActionPerformed
228
229 //
230 public static void main( String args[] )
231 {
232 CreditChecker application = new CreditChecker();
233 application.setDefaultCloseOperation( JFrame.EXIT_ON_CLOSE );
234
235 } // end method main
236
237 } // end class CreditChecker
```

Computer Science & Information Technology

You might also like to view...

Which of the following can be described as striping with parity?

A. RAID 0 B. RAID 1 C. RAID 5 D. RAID 0+1

Computer Science & Information Technology

You can move a cell in a layout by dragging it to its new position.

Answer the following statement true (T) or false (F)

Computer Science & Information Technology