/*
* Created on 09.01.2005@15:28:11
*
* TODO Licence info
*/
package de.tutorials;
import java.awt.BorderLayout;
import java.awt.Container;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.text.DecimalFormat;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTextArea;
import javax.swing.JTextField;
/**
* @author Administrator
*
* TODO Explain me...
*/
public class TilgungsRechnung extends JFrame {
private JButton btnCalc;
private JTextArea txtResults;
private JTextField txtInterestRate, txtSeedCapital, txtMaturity,
txtAnnuity;
private JLabel lblInterestRate, lblSeedCaptial, lblMaturity, lblAnnuity;
public TilgungsRechnung() {
super("TilgungsRechnung");
setDefaultCloseOperation(EXIT_ON_CLOSE);
JPanel controlPanel = new JPanel(new GridLayout(4, 2));
lblSeedCaptial = new JLabel("Startkaptial: ");
txtSeedCapital = new JTextField(10);
controlPanel.add(lblSeedCaptial);
controlPanel.add(txtSeedCapital);
lblInterestRate = new JLabel("Zinssatz: (%) ");
txtInterestRate = new JTextField(6);
controlPanel.add(lblInterestRate);
controlPanel.add(txtInterestRate);
lblMaturity = new JLabel("Laufzeit: (Jahre)");
txtMaturity = new JTextField(4);
controlPanel.add(lblMaturity);
controlPanel.add(txtMaturity);
lblAnnuity = new JLabel("Annuität");
txtAnnuity = new JTextField(10);
controlPanel.add(lblAnnuity);
controlPanel.add(txtAnnuity);
txtResults = new JTextArea(10, 50);
btnCalc = new JButton("Berechne Tilgungsplan");
btnCalc.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
double interestRate = Double.valueOf(txtInterestRate.getText())
.doubleValue();
double seedCapital = Double.valueOf(txtSeedCapital.getText())
.doubleValue();
int maturity = Integer.valueOf(txtMaturity.getText())
.intValue();
double balance = seedCapital;
double p = interestRate / 100.0D;
double q = 1 + p;
double n = maturity;
double annuity = seedCapital * (Math.pow(q, n) * (q - 1))
/ (Math.pow(q, n) - 1);
DecimalFormat df = new DecimalFormat("#.00");
txtAnnuity.setText(df.format(annuity));
StringBuffer buffer = new StringBuffer();
String header = "Jahr Restschuld(Jahresanfang) Zinsen Tilgungsrate Annuität\n\n";
n = 0;
while (n++ < maturity) {
double interest = balance * p;
buffer.append((int) n);
buffer.append(" ");
buffer.append(balance);
buffer.append(" ");
buffer.append(interest);
buffer.append(" ");
buffer.append(annuity - interest);
buffer.append(" ");
buffer.append(annuity);
balance -= annuity;
buffer.append('\n');
}
buffer.insert(0, header);
txtResults.setText(buffer.toString());
}
});
Container c = getContentPane();
c.add(controlPanel, BorderLayout.CENTER);
c.add(new JScrollPane(txtResults), BorderLayout.EAST);
c.add(btnCalc, BorderLayout.SOUTH);
pack();
setVisible(true);
}
public static void main(String[] args) {
new TilgungsRechnung();
}
}