1 |
import java.awt.event.ActionEvent; |
2 |
import java.awt.event.ActionListener; |
3 |
import javax.swing.JOptionPane; |
4 |
import javax.swing.Timer; |
5 |
|
6 |
/** |
7 |
This program uses a timer to add interest to a bank |
8 |
account once per second. |
9 |
*/ |
10 |
public class TimerTest |
11 |
{ |
12 |
public static void main(String[] args) |
13 |
{ |
14 |
final BankAccount account = new BankAccount(1000); |
15 |
|
16 |
class InterestAdder implements ActionListener |
17 |
{ |
18 |
public void actionPerformed(ActionEvent event) |
19 |
{ |
20 |
double interest = account.getBalance() * RATE / 100; |
21 |
account.deposit(interest); |
22 |
System.out.println("Balance = " |
23 |
+ account.getBalance()); |
24 |
} |
25 |
} |
26 |
|
27 |
InterestAdder listener = new InterestAdder(); |
28 |
|
29 |
final int DELAY = 1000; // milliseconds between timer ticks |
30 |
Timer t = new Timer(DELAY, listener); |
31 |
t.start(); |
32 |
|
33 |
JOptionPane.showMessageDialog(null, "Quit?"); |
34 |
System.exit(0); |
35 |
} |
36 |
|
37 |
private static final double RATE = 5; |
38 |
} |