1 /**
2 A class to monitor the growth of an investment that
3 accumulates interest at a fixed annual rate
4 */
5 public class Investment
6 {
7 private double balance;
8 private double rate;
9 private int year;
10
11 /**
12 Constructs an Investment object from a starting balance and
13 interest rate.
14 @param aBalance the starting balance
15 @param aRate the interest rate in percent
16 */
17 public Investment(double aBalance, double aRate)
18 {
19 balance = aBalance;
20 rate = aRate;
21 year = 0;
22 }
23
24 /**
25 Keeps accumulating interest until a target balance has
26 been reached.
27 @param targetBalance the desired balance
28 */
29 public void waitForBalance(double targetBalance)
30 {
31 while (balance < targetBalance)
32 {
33 year++;
34 double interest = balance * rate / 100;
35 balance = balance + interest;
36 }
37 }
38
39 /**
40 Keeps accumulating interest for a given number of years.
41 @param numberOfYears the number of years to wait
42 */
43 public void waitYears(int numberOfYears)
44 {
45 for (int i = 1; i <= numberOfYears; i++)
46 {
47 double interest = balance * rate / 100;
48 balance = balance + interest;
49 }
50 year = year + n;
51 }
52
53 /**
54 Gets the current investment balance.
55 @return the current balance
56 */
57 public double getBalance()
58 {
59 return balance;
60 }
61
62 /**
63 Gets the number of years this investment has accumulated
64 interest.
65 @return the number of years since the start of the investment
66 */
67 public int getYears()
68 {
69 return year;
70 }
71 }