InvestmentTable.java Source code

Java tutorial

Introduction

Here is the source code for InvestmentTable.java

Source

/*
   This program is a part of the companion code for Core Java 8th ed.
   (http://horstmann.com/corejava)
    
   This program is free software: you can redistribute it and/or modify
   it under the terms of the GNU General Public License as published by
   the Free Software Foundation, either version 3 of the License, or
   (at your option) any later version.
    
   This program is distributed in the hope that it will be useful,
   but WITHOUT ANY WARRANTY; without even the implied warranty of
   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
   GNU General Public License for more details.
    
   You should have received a copy of the GNU General Public License
   along with this program.  If not, see <http://www.gnu.org/licenses/>.
*/

import java.awt.EventQueue;

import javax.swing.JFrame;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.table.AbstractTableModel;
import javax.swing.table.TableModel;

/**
 * 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.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
                frame.setVisible(true);
            }
        });
    }
}

/**
 * This frame contains the investment table.
 */
class InvestmentTableFrame extends JFrame {
    public InvestmentTableFrame() {
        setTitle("InvestmentTable");
        setSize(DEFAULT_WIDTH, DEFAULT_HEIGHT);

        TableModel model = new InvestmentTableModel(30, 5, 10);
        JTable table = new JTable(model);
        add(new JScrollPane(table));
    }

    private static final int DEFAULT_WIDTH = 600;
    private static final int DEFAULT_HEIGHT = 300;
}

/**
 * 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 {
    /**
     * 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) + "%";
    }

    private int years;
    private int minRate;
    private int maxRate;

    private static double INITIAL_BALANCE = 100000.0;
}