-
Notifications
You must be signed in to change notification settings - Fork 79
Expand file tree
/
Copy pathInvestmentTable.java
More file actions
92 lines (80 loc) · 2.07 KB
/
Copy pathInvestmentTable.java
File metadata and controls
92 lines (80 loc) · 2.07 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
package tableModel;
import java.awt.*;
import javax.swing.*;
import javax.swing.table.*;
/**
* This program shows how to build a table from a table model.
* @version 1.02 2007-08-01
* @author Cay Horstmann
*/
public class InvestmentTable
{
public static void main(String[] args)
{
EventQueue.invokeLater(new Runnable()
{
public void run()
{
JFrame frame = new InvestmentTableFrame();
frame.setTitle("InvestmentTable");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setVisible(true);
}
});
}
}
/**
* This frame contains the investment table.
*/
class InvestmentTableFrame extends JFrame
{
public InvestmentTableFrame()
{
TableModel model = new InvestmentTableModel(30, 5, 10);
JTable table = new JTable(model);
add(new JScrollPane(table));
pack();
}
}
/**
* This table model computes the cell entries each time they are requested. The table contents shows
* the growth of an investment for a number of years under different interest rates.
*/
class InvestmentTableModel extends AbstractTableModel
{
private static double INITIAL_BALANCE = 100000.0;
private int years;
private int minRate;
private int maxRate;
/**
* Constructs an investment table model.
* @param y the number of years
* @param r1 the lowest interest rate to tabulate
* @param r2 the highest interest rate to tabulate
*/
public InvestmentTableModel(int y, int r1, int r2)
{
years = y;
minRate = r1;
maxRate = r2;
}
public int getRowCount()
{
return years;
}
public int getColumnCount()
{
return maxRate - minRate + 1;
}
public Object getValueAt(int r, int c)
{
double rate = (c + minRate) / 100.0;
int nperiods = r;
double futureBalance = INITIAL_BALANCE * Math.pow(1 + rate, nperiods);
return String.format("%.2f", futureBalance);
}
public String getColumnName(int c)
{
return (c + minRate) + "%";
}
}