01: /**
02: A class to monitor the growth of an investment that
03: accumulates interest at a fixed annual rate.
04: */
05: public class Investment
06: {
07: /**
08: Constructs an Investment object from a starting balance and
09: interest rate.
10: @param aBalance the starting balance
11: @param aRate the interest rate in percent
12: */
13: public Investment(double aBalance, double aRate)
14: {
15: balance = aBalance;
16: rate = aRate;
17: years = 0;
18: }
19:
20: /**
21: Keeps accumulating interest until a target balance has
22: been reached.
23: @param targetBalance the desired balance
24: */
25: public void waitForBalance(double targetBalance)
26: {
27: while (balance < targetBalance)
28: {
29: years++;
30: double interest = balance * rate / 100;
31: balance = balance + interest;
32: }
33: }
34:
35: /**
36: Gets the current investment balance.
37: @return the current balance
38: */
39: public double getBalance()
40: {
41: return balance;
42: }
43:
44: /**
45: Gets the number of years this investment has accumulated
46: interest.
47: @return the number of years since the start of the investment
48: */
49: public int getYears()
50: {
51: return years;
52: }
53:
54: private double balance;
55: private double rate;
56: private int years;
57: }