// Importieren des Archiv-Files  junit.jar

import java.awt.*;

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

public class junit4 extends JFrame {

  JTextArea _editor;

  JToolBar toolBar = new JToolBar();
  JButton BnTest1 = new JButton();  // linie
  JButton BnTest2 = new JButton(); // kreis
  JButton BnTest3 = new JButton(); // rechteck1

  public junit4() {
    setSize(600, 500);
    setTitle("junit4");
    setGUI();
  }

  private void setGUI()  {
    this.getContentPane().setLayout( new BorderLayout() );

     _editor = new JTextArea("");
    this.getContentPane().add(new JScrollPane(_editor), BorderLayout.CENTER);

    _editor.setFont( new Font("Arial", Font.BOLD,18)  );

    BnTest1.setText("1. Test");
    BnTest1.setFont( new Font("Arial", Font.BOLD,18)  );
    BnTest1.addActionListener(new java.awt.event.ActionListener() {

      public void actionPerformed(ActionEvent e) {
        BnTest1_actionPerformed(e);
      }
    });
    toolBar.add(BnTest1);
    // ----------------------------------------

    BnTest2.setText("2. Test");
    BnTest2.setFont( new Font("Arial", Font.BOLD,18)  );
    BnTest2.addActionListener(new java.awt.event.ActionListener() {

      public void actionPerformed(ActionEvent e) {
        BnTest2_actionPerformed(e);
      }
    });
    toolBar.add(BnTest2);

    // ----------------------------------------
    BnTest3.setText("3. Test");
    BnTest3.setFont( new Font("Arial", Font.BOLD,18)  );
    BnTest3.addActionListener(new java.awt.event.ActionListener() {

      public void actionPerformed(ActionEvent e) {
        BnTest3_actionPerformed(e);
      }
    });
    toolBar.add(BnTest3);  // rechteck
    // ----------------------------------------
    this.getContentPane().add(toolBar, BorderLayout.NORTH);


  } // create


  public double add(double x, double y) {
    return x+y;
  }




  void BnTest1_actionPerformed(ActionEvent e) {
    double a,b,c;
    junit3 myclass = new junit3();  // neue Klasse

    c = myclass.add(5.033, 4.343);
    if (c == 9.376 ) 
      _editor.append("1. Test Add okay: "+c+ " = 9.376 "+"\n" );
    else
      _editor.append("1. Test Add fehlerhaft: "+c+ " != 9.376 "+"\n" );
  }
    // if (c != (5.033+4.343) ) {

  void BnTest2_actionPerformed(ActionEvent e) {
    double a[];
    double d, s;
       // Testvorbereitung
    a = new double[11];
    s = 0;
    for (int i=0; i<11; i++) {
      d = Math.random()*50;
      a[i] = d;
      s += d;
    }
    junit3 myclass = new junit3();  // neue Klasse

    d = 0;
    for (int i=0; i<11; i++) {
      d = myclass.add(a[i], d);
    }
    if (d == s )
      _editor.append("2. Test Add okay: "+d+ "  "+s+"\n" );
    else
      _editor.append("Fehler beim 2. Test Add: "+d+ "  "+s+"\n" );
  } // BnTest2



  void BnTest3_actionPerformed(ActionEvent e) {
    double a[];
    double d, s;
    int k;
       // Testvorbereitung mit Array
    a = new double[11];
    s = 0;
    for (int i=0; i<11; i++) {
      d = Math.random()*100;
      k = (int) Math.random()*2;
      if (k==0) d -= d;
      System.out.println("d: "+d );
      a[i] = d;
      s += d;
    }
    // Benutzung der Methode add
    junit4 myclass = new junit4();  // neue Klasse

    d = 0;
    for (int i=0; i<11; i++) {
      d = myclass.add(a[i], d);
    }
    if (d == s )
      _editor.append("3. Test Add okay: "+d+ "  "+s+"\n" );
    else
      _editor.append("Fehler beim 3. Test Add: "+d+ "  "+s+"\n" );
  } // BnTest3



  public static void main(String[] args) {
    double a,b,c;
    junit4 frame = new junit4();
    c = frame.add(5.033, 4.343);

    // externer Test
    a = 1;
    b = 3;
    c = frame.add(a,b);

    frame.setVisible(true);

  }



  //Überschreiben, damit das Programm bei Herunterfahren des Systems beendet werden kann
  protected void processWindowEvent(WindowEvent e) {
    super.processWindowEvent(e);
    if(e.getID() == WindowEvent.WINDOW_CLOSING) {
      System.exit(0);
    }
  }

}
