Java 11: GUI Kontoprogramm mit verschiedenen Konten

Im letzten Tutorial haben ich euch ja gezeigt wie ihr eine Grafische Oberfläche mit Java erzeugt. Das Kontoprogramm welches wir hier geschrieben haben erweitern wir jetzt etwas. Jetzt soll es möglich sein das wir auch mehre Konten haben welche verwaltet werden können.

Dazu legt ihr ein weiteres Label an, in dem Label gebt ihr den Text Kontonummer ein. Außerdem erstellt ihr ein Textfeld in welches man die Entsprechende Kontonummer eintragen kann.
Eure Grafische Oberfläche sollte dann etwa so aussehen:
Kono_GUI
Ich gebe euch noch einen kleinen Tipp. Bei den Kontonummern solltet ihr mit einem Array arbeiten. Sonst ist bei dieser kleinen Programmänderung aber nichts schweres dabei. Es werden nur dinge verwendet welche ich euch in den alten Java Tutorials schon gezeigt habe.

Euer Fertiger Quellcode sollte etwa so aussehen:

 
/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/*
 * StartKonto.java
 *
 * Created on 01.08.2009, 16:26:57
 */

import java.io.BufferedReader;
import java.io.InputStreamReader;
import javax.swing.JOptionPane;

/**
 *
 * @author Andreas
 */
public class StartKonto extends javax.swing.JFrame {

    double kontostand = 0.0;
    double letzteBuchung;
    double betrag;
    String eingabe;
    String ausgabe;
    String Kontonummerstr;
    int Kontonummerint;
    double [] Kontonumer = new double [100] ;

    BufferedReader input = new BufferedReader(new InputStreamReader(System.in));

    /** Creates new form StartKonto */
    public StartKonto() {
        initComponents();
    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // 
    private void initComponents() {

        jLabBuchung = new javax.swing.JLabel();
        jLabKontostand = new javax.swing.JLabel();
        jLabEingabe = new javax.swing.JLabel();
        jLabAusgabe = new javax.swing.JLabel();
        jButton1 = new javax.swing.JButton();
        jTextEingabe = new javax.swing.JTextField();
        jLabText = new javax.swing.JLabel();
        jLabKontonummer = new javax.swing.JLabel();
        jTextKontonummer = new javax.swing.JTextField();

        setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);

        jLabBuchung.setFont(new java.awt.Font("Tahoma", 1, 11)); // NOI18N
        jLabBuchung.setText("Letzte Buchung:");

        jLabKontostand.setFont(new java.awt.Font("Tahoma", 1, 11));
        jLabKontostand.setText("Kontostand:");

        jButton1.setText("Buchen");
        jButton1.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseClicked(java.awt.event.MouseEvent evt) {
                jButton1SayHelloClicked(evt);
            }
        });
        jButton1.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton1ActionPerformed(evt);
            }
        });

        jTextEingabe.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jTextEingabeActionPerformed(evt);
            }
        });

        jLabText.setFont(new java.awt.Font("Tahoma", 1, 11)); // NOI18N
        jLabText.setText("Buchung:");

        jLabKontonummer.setFont(new java.awt.Font("Tahoma", 1, 11)); // NOI18N
        jLabKontonummer.setText("Kontonummer");

        jTextKontonummer.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jTextKontonummerActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGap(0, 400, Short.MAX_VALUE)
            .addGroup(layout.createSequentialGroup()
                .addGap(50, 50, 50)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(jLabText)
                    .addGroup(layout.createSequentialGroup()
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addGroup(layout.createSequentialGroup()
                                .addGap(30, 30, 30)
                                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                                    .addComponent(jLabBuchung)
                                    .addComponent(jLabKontostand)))
                            .addComponent(jTextEingabe, javax.swing.GroupLayout.PREFERRED_SIZE, 80, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING, false)
                                .addComponent(jTextKontonummer, javax.swing.GroupLayout.Alignment.LEADING)
                                .addComponent(jLabKontonummer, javax.swing.GroupLayout.Alignment.LEADING, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)))
                        .addGap(42, 42, 42)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING, false)
                                .addComponent(jLabEingabe, javax.swing.GroupLayout.Alignment.LEADING, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                                .addComponent(jLabAusgabe, javax.swing.GroupLayout.Alignment.LEADING, javax.swing.GroupLayout.DEFAULT_SIZE, 49, Short.MAX_VALUE))
                            .addComponent(jButton1, javax.swing.GroupLayout.PREFERRED_SIZE, 114, javax.swing.GroupLayout.PREFERRED_SIZE))))
                .addContainerGap(74, Short.MAX_VALUE))
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGap(0, 300, Short.MAX_VALUE)
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                .addGap(40, 40, 40)
                .addComponent(jLabText)
                .addGap(18, 18, 18)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(jButton1, javax.swing.GroupLayout.PREFERRED_SIZE, 56, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addGroup(layout.createSequentialGroup()
                        .addComponent(jTextEingabe, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                        .addComponent(jLabKontonummer)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(jTextKontonummer, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 13, Short.MAX_VALUE)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                    .addComponent(jLabAusgabe, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                    .addComponent(jLabKontostand, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
                .addGap(18, 18, 18)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                    .addComponent(jLabEingabe, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                    .addComponent(jLabBuchung, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
                .addGap(98, 98, 98))
        );

        pack();
    }// 

    private void jButton1SayHelloClicked(java.awt.event.MouseEvent evt) {

        eingabe = jTextEingabe.getText();
        betrag=Double.parseDouble (eingabe);
        Kontonummerstr = jTextKontonummer.getText();
        Kontonummerint = Integer.parseInt(Kontonummerstr);

        if (Kontonumer[Kontonummerint]+betrag >=0.0)
        {
        Kontonumer[Kontonummerint] = Kontonumer[Kontonummerint] + betrag;
        ausgabe = Double.toString (Kontonumer[Kontonummerint]);
        jLabEingabe.setText (eingabe);
        jLabAusgabe.setText (ausgabe);
        // TODO add your handling code here:
        }
        else
        {
            JOptionPane.showMessageDialog(null, "Sie würden bei ihrem Konto ins Minus gehen", "Guthaben zu gering", JOptionPane.OK_CANCEL_OPTION);
        }
 

    }

    private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {
        // TODO add your handling code here:
}

    private void jTextEingabeActionPerformed(java.awt.event.ActionEvent evt) {


        // TODO add your handling code here:
}

    private void jTextKontonummerActionPerformed(java.awt.event.ActionEvent evt) {
        // TODO add your handling code here:
}

    /**
    * @param args the command line arguments
    */
    public static void main(String args[]) {
        java.awt.EventQueue.invokeLater(new Runnable() {
            public void run() {
                new StartKonto().setVisible(true);
            }
        });
    }

    // Variables declaration - do not modify
    private javax.swing.JButton jButton1;
    private javax.swing.JLabel jLabAusgabe;
    private javax.swing.JLabel jLabBuchung;
    private javax.swing.JLabel jLabEingabe;
    private javax.swing.JLabel jLabKontonummer;
    private javax.swing.JLabel jLabKontostand;
    private javax.swing.JLabel jLabText;
    private javax.swing.JTextField jTextEingabe;
    private javax.swing.JTextField jTextKontonummer;
    // End of variables declaration

}

Ich erkläre jetzt nur noch die dinge welche sich seit dem letzen Kontoprogramm geändert haben oder neu dazu kamen.

  • In der Zeile 27 und 28 erstellt ihr eine int und String Variable für die Kontonummer.
  • In der Zeile 29 erstellt ihr ein double Array welches 100 Variablen groß ist.
  • In der Zeile 156 lest ihr die Zahl aus dem Kontonummern Textfeld ein und speichert sie in der Variablen Kontonummerstr.
  • In der Zeile 157 wandelt ihr die Zahl aus der Variablen Kontonummerstr in ein int um und speichert sie dann in der Variablen Kontonummerint.
  • Sonst hat sich noch geändert das die Variable kontostand weggefallen ist. Überall wo diese stand ist jetzt das Array wie folgt eingetragen Kontonumer[Kontonummerint]. Denn so wird das Gebuchte Ergebnis immer auf das Konto mit der eingeben Kontonummer gebucht und angezeigt.

Hier geht es zu den bisherigen Teilen der Artikelserie Java:

Java 1: Netbeans die ersten Schritte und Hello World

Java 2: Rechnen mit Variablen

Java 3: Werte einlesen, zwischenspeichern und wieder ausgeben

Java 4: Kassen Programm mit if Abfrage

Java 5: Kassen Programm while, do-while Schleife

Java 6: Kassen Programm For Schleife

Java 7: Array – Lotto Programm erstellen

Java 8: Inhalt einer Datei auslesen und filtern

Java 9: Inhalt einer Textdatei in eine andere Datei kopieren

Java 10: Einführung in die Grafische Oberfläche – kleines Kontoprogramm

Hinterlasse jetzt einen Kommentar

Kommentar hinterlassen

E-Mail Adresse wird nicht veröffentlicht.


*