A bank wants you to create an application that will allow them to view their clients’ information. The interface is created for you (Fig. 18.41); you need to implement the Client class which stores the data. Once your appli- cation is completed, the bank manager should be able to click the Next or Previous JBut- tons to run through each client’s information. The GUI is implemented such that clicking the Next JButton at the last account returns the user to the first account and clicking the Previ- ous JButton at the first account returns the user to the last account. The information is stored in four arrays containing first names, last names, account numbers and account bal- ances.



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

b) Opening the template files. Open the AccountInformation.java and Client.java

files in your text editor.

c) Determining variables for the class. Examine the code from AccountInforma- tion.java, including all the get method calls that the Client object uses to retrieve information. These method calls can be found in the displayInformation method, beginning on line 197.

d) Creating the Client class. Switch to your Client.java file. Declare four private instance variables beginning on line 6 to represent an account number, a balance amount, a first name and a last name. Use get and set methods (which you will declare in the next step) for those variables to declare a constructor on lines 16–26.

e) Declaring the get and set methods. Each instance variable should have a correspond- ing get and set method. Use this

```
1 // AccountInformation.java
2 // This application displays bank account information.
3 import java.awt.*;
4 import java.awt.event.*;
5 import java.text.DecimalFormat;
6 import javax.swing.*;
7
8 public class AccountInformation extends JFrame
9 {
10 // JLabel and JTextField for first name
11 private JLabel firstJLabel;
12 private JTextField firstJTextField;
13
14 // JLabel and JTextField for last name
15 private JLabel lastJLabel;
16 private JTextField lastJTextField;
17
18 // JLabel and JTextField for account number
19 private JLabel numberJLabel;
20 private JTextField numberJTextField;
21
22 // JLabel and JTextField for balance
23 private JLabel balanceJLabel;
24 private JTextField balanceJTextField;
25
26 // JButton to display previous or next record
27 private JButton previousJButton;
28 private JButton nextJButton;
29
30 // integer for storing the index of the current record
31 private int position = 0;
32
33 // Client instance for storing an array of account records
34 private Client accountRecords[];
35
36 // no-argument constructor
37 public AccountInformation()
38 {
39 createUserInterface();
40
41 // variables hold data to be stored in accountRecords
42 String firstName[] = new String[] { "John", "Sarah", "Jack",
43 "Adam", "Diane", "David", "Kristin", "Jennifer", "George" };
44 String lastName[] = new String[] { "Blue", "White", "Red",
45 "Brown", "Yellow", "Black", "Green", "Orange", "Pink" };
46 int accountNumber[] = new int[] { 1234652, 1234666, 1234678,
47 1234681, 1234690, 1234770, 1234787, 1234887, 1239797, };
48 double balance[] = new double[] { 1000.78, 2056.24, 978.65,
49 990.0, 432.75, 780.78, 4590.63, 7910.11, 23.99 };
50
51 // initialize accountRecords and make it the same
52 // length as the variables to be stored in it
53 accountRecords = new Client[ firstName.length ];
54
55 // store account information variables in accountRecords
56 for ( int i = 0; i < firstName.length; i++)
57 {
58 accountRecords[ i ] = new Client( firstName[ i ],
59 lastName[ i ], accountNumber[ i ], balance[ i ] );
60 }
61
62 } // end constructor
63
64 // create and position GUI components; register event handlers
65 private void createUserInterface()
66 {
67 // get content pane for attaching GUI components
68 Container contentPane = getContentPane();
69
70 // enable explicit positioning of GUI components
71 contentPane.setLayout( null );
72
73 // set up firstJLabel
74 firstJLabel = new JLabel();
75 firstJLabel.setBounds( 16, 16, 100, 24 );
76 firstJLabel.setText( "First name:" );
77 contentPane.add( firstJLabel );
78
79 // set up firstJTextField
80 firstJTextField = new JTextField();
81 firstJTextField.setBounds( 134, 16, 120, 24 );
82 firstJTextField.setEditable( false );
83 contentPane.add( firstJTextField );
84
85 // set up lastJLabel
86 lastJLabel = new JLabel();
87 lastJLabel.setBounds( 16, 56, 120, 24 );
88 lastJLabel.setText( "Last name:" );
89 contentPane.add( lastJLabel );
90
91 // set up lastJTextField
92 lastJTextField = new JTextField();
93 lastJTextField.setBounds( 134, 56, 120, 24 );
94 lastJTextField.setEditable( false );
95 contentPane.add( lastJTextField );
96
97 // set up numberJLabel
98 numberJLabel = new JLabel();
99 numberJLabel.setBounds( 16, 96, 120, 24 );
100 numberJLabel.setText( "Account number:" );
101 contentPane.add( numberJLabel );
102
103 // set up numberJTextField
104 numberJTextField = new JTextField();
105 numberJTextField.setBounds( 134, 96, 120, 24 );
106 numberJTextField.setEditable( false );
107 contentPane.add( numberJTextField );
108
109 // set up balanceJLabel
110 balanceJLabel = new JLabel();
111 balanceJLabel.setBounds( 16, 136, 100, 24 );
112 balanceJLabel.setText( "Balance:" );
113 contentPane.add( balanceJLabel );
114
115 // set up balanceJTextField
116 balanceJTextField = new JTextField();
117 balanceJTextField.setBounds( 134, 136, 120, 24 );
118 balanceJTextField.setEditable( false );
119 contentPane.add( balanceJTextField );
120
121 // set up previousJButton
122 previousJButton = new JButton();
123 previousJButton.setBounds( 16, 190, 90, 24 );
124 previousJButton.setText( "Previous" );
125 contentPane.add( previousJButton );
126 previousJButton.addActionListener(
127
128 new ActionListener() // anonymous inner class
129 {
130 // event handler called when previousJButton is pressed
131 public void actionPerformed( ActionEvent event )
132 {
133 previousJButtonActionPerformed( event );
134 }
135
136 } // end anonymous inner class
137
138 ); // end call to addActionListener
139
140 // set up nextJButton
141 nextJButton = new JButton();
142 nextJButton.setBounds( 164, 190, 90, 24 );
143 nextJButton.setText( "Next" );
144 contentPane.add( nextJButton );
145 nextJButton.addActionListener(
146
147 new ActionListener() // anonymous inner class
148 {
149 // event handler called when nextJButton is pressed
150 public void actionPerformed( ActionEvent event )
151 {
152 nextJButtonActionPerformed( event );
153 }
154
155 } // end anonymous inner class
156
157 ); // end call to addActionListener
158
159 // set properties of application’s window
160 setTitle( "Account Information" ); // set title bar string
161 setSize( 280, 255 ); // set window size
162 setVisible( true ); // display window
163
164 } // end method createUserInterface
165
166 // display next account
167 private void nextJButtonActionPerformed( ActionEvent event )
168 {
169 position++; // increment position
170
171 // prevent position from becoming invalid
172 if ( position >= accountRecords.length )
173 {
174 position = 0;
175 }
176
177 displayInformation();
178
179 } // end method nextJButtonActionPerformed
180
181 // display previous account
182 private void previousJButtonActionPerformed( ActionEvent event )
183 {
184 position--; // decrement position
185
186 // prevent position from becoming invalid
187 if ( position < 0 )
188 {
189 position = accountRecords.length - 1;
190 }
191
192 displayInformation();
193
194 } // end method previousJButtonActionPerformed
195
196 // display correct information in the correct JTextFields
197 private void displayInformation()
198 {
199 DecimalFormat balanceFormat = new DecimalFormat( ".00" );
200
201 firstJTextField.setText(
202 accountRecords[ position ].getFirstName() );
203 lastJTextField.setText(
204 accountRecords[ position ].getLastName() );
205 numberJTextField.setText( String.valueOf(
206 accountRecords[ position ].getAccount() ) );
207 balanceJTextField.setText( balanceFormat.format(
208 accountRecords[ position ].getBalance() ) );
209
210 } // end method displayInformation
211
212 // main method
213 public static void main( String[] args )
214 {
215 AccountInformation application = new AccountInformation();
216 application.setDefaultCloseOperation( JFrame.EXIT_ON_CLOSE );
217
218 } // end method main
219
220 } // end class AccountInformation
```
```
1 // Exercise 18.13: Client.java
2 // Represents account information and contains get and set methods.
3
4 public class Client
5 {
6 // integer for storing account number
7 private int account;
8
9 // double for storing balance
10 private double balance;
11
12 // Strings for storing first and last name
13 private String firstName;
14 private String lastName;
15
16 // Client constructor, first name, last name, account value and
17 // balance supplied
18 public Client( String firstNameValue, String lastNameValue,
19 int accountValue, double balanceValue )
20 {
21 setFirstName( firstNameValue );
22 setLastName( lastNameValue );
23 setAccount( accountValue );
24 setBalance( balanceValue );
25
26 } // end constructor
27
28
29
28 // return firstName value
29 public String getFirstName()
30 {
31 return firstName;
32
33 // end method getFirstName
34
35 // set firstName value
36 public void setFirstName( String value )
37 {
38 firstName = value;
39
40 } // end method setFirstName
41
42 // return lastName
43 value public String getLastName()
44 {
45 return lastName;
46
47 }// end method getLastName
48
49 // set lastName value
50 public void setLastName( String value )
51 {
52 lastName = value;
53
54 // end method setLastName
55
56 // return account value
57 public int getAccount()
58 {
59 return account;
60
61 // end method getAccount
62
63 // set account value
64 public void setAccount( int value )
65 {
66 if ( value >= 0 )
67 {
68 account = value;
69 }
70 else // set invalid input to 0
71 {
72 account = 0;
73 }
74
75 // end method setAccount
76
77 // return balance value
78 public double getBalance()
79 {
80 return balance;
81
82 // end method getBalance
83

Computer Science & Information Technology

You might also like to view...

The WordArt Quick Style feature is located on the ________ tab

Fill in the blank(s) with correct word

Computer Science & Information Technology

Developers can code Visual Basic applications to make decisions based on the input of users or other conditions that occur.

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

Computer Science & Information Technology