Java Reference
In-Depth Information
21
final
BankAccount account =
new
BankAccount(INITIAL_BALANCE);
22
23
class
AddInterestListener
implements
ActionListener
24 {
25
public void
actionPerformed(ActionEvent event)
26 {
27
//
The listener method accesses the account variable
28
//
from the surrounding block
29
double
interest =
account.getBalance()
30 * INTEREST_RATE /
100
;
31 account.deposit(interest);
32 System.out.println(
"balance: "
+
account.getBalance());
33 }
34 }
35
36 ActionListener listener =
new
AddInterestListener();
37 button.addActionListener(listener);
38
39 frame.setSize(FRAME_WIDTH, FRAME_HEIGHT);
40 frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE
41 frame.setVisible(
true
);
42 }
43
44
private static final double
INTEREST_RATE =
10
;
45
private static final double
INITIAL_BALANCE
=
1000
;
46
47
private static final int
FRAME_WIDTH =
120
;
48
private static final int
FRAME_HEIGHT =
60
;
49 }
413
414
Output
balance: 1100.0
balance: 1210.0
balance: 1331.0
balance: 1464.1