import java.awt.*;
import javax.swing.*;
import java.awt.Container;
import java.awt.event.*;
import bankkonto.*;

public class BankkontoGUI extends JFrame implements ActionListener{
    private Bankkonto konto;
    private JTextField kontostandFeld;
    private JTextField betragFeld;
    private JButton einzahlen;
    private JButton abheben;
    private JButton jahresEnde;

    public BankkontoGUI(Bankkonto konto) {
	this.konto = konto;
	Container contentPane = this.getContentPane();
	this.setSize(400,200);
	this.setTitle("Bankkonto");
	
	kontostandFeld = new JTextField(20);
	kontostandFeld.setEditable(false);
	kontostandFeld.setText("" + konto.getKontostand());
	JLabel kontostandLabel = new JLabel("Kontostand:");
	JPanel kontostandPanel = new JPanel();
	kontostandPanel.add(kontostandLabel);
	kontostandPanel.add(kontostandFeld);
	
	betragFeld = new JTextField(20);
	betragFeld.setEditable(true);
	JLabel betragLabel = new JLabel("Betrag:");
	JPanel betragPanel = new JPanel();
	betragPanel.add(betragLabel);
	betragPanel.add(betragFeld);
	
	einzahlen = new JButton("einzahlen");
	abheben = new JButton("abheben");
	jahresEnde = new JButton("Jahresende");

	if (! (konto instanceof Sparkonto))
	    jahresEnde.setEnabled(false);


	JPanel buttonPanel = new JPanel();
	buttonPanel.add(einzahlen);
	buttonPanel.add(abheben);
	buttonPanel.add(jahresEnde);
	einzahlen.addActionListener(this);
	einzahlen.addActionListener(new MyListener());
	abheben.addActionListener(this);
	jahresEnde.addActionListener(this);
	abheben.addActionListener(new MyListener());
	contentPane.setLayout(new GridLayout(3,1));
	contentPane.add(kontostandPanel);

	contentPane.add(betragPanel);
	contentPane.add(buttonPanel);
    }
    public void actionPerformed(ActionEvent event) {
	Object source = event.getSource();
	double betrag = Double.parseDouble(betragFeld.getText());
	if (source == einzahlen) 
	    konto.einzahlen(betrag);
	else if (source == abheben)
	    konto.abheben(betrag);
	else if (source == jahresEnde)
	    ((Sparkonto)konto).zinsenAnrechnen();
	else
	    ;
	kontostandFeld.setText("" + konto.getKontostand());
    }
		
}

