/* Autor:           Dipl.-Inf., Dipl.-Ing. (FH) Michael Wilhelm
   Firma:           HS Harz, FB AI
   Version:         1.1
   Ersterstellung:  05.09.2007
   Letzte Änderung: 12.11.2010
*/

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

// Serialize2 mit zwei Klassen (Person Parson  und Gehalt)
// eine Person HAT eine Gehalt
//  BnRead_Clicked
//  BnWrite_Clicked
// Ausgabe in einem Editor


public class Serialize2 extends JFrame {

  JTextArea _editor;

  //Frame konstruieren
  public Serialize2() {
    setSize(600, 500);
    this.setTitle("Serialize2");
    setDefaultCloseOperation( JFrame.EXIT_ON_CLOSE );
    setGUI();
  }

  void setGUI() {

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

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

     JButton BnRead = new JButton("Read");
     JButton BnWrite = new JButton("Write");


    JPanel panel = new JPanel();
    panel.setLayout ( new FlowLayout() );
    panel.add(BnRead);
    panel.add(BnWrite);
    this.getContentPane().add(panel, BorderLayout.SOUTH);
    BnRead.setFont( new Font("Arial", Font.BOLD,18)  );
    BnWrite.setFont( new Font("Arial", Font.BOLD,18)  );

    BnRead.addActionListener(new java.awt.event.ActionListener() {

      public void actionPerformed(ActionEvent e) {
        BnRead_Clicked();
      }
    });
    BnWrite.addActionListener(new java.awt.event.ActionListener() {

      public void actionPerformed(ActionEvent e) {
        BnWrite_Clicked();
      }
    });

  } // setGUI

  private void BnWrite_Clicked() {
      Person p = new Person("Fred", "Weasley", "secret", 1000);
      FileOutputStream fout;
      ObjectOutputStream oos = null;
      try {
          fout = new FileOutputStream("Person2.ser");
          oos = new ObjectOutputStream(fout);
          oos.writeObject(p);
          _editor.append("Geschriebenes Objekt: " + p.getClass()+"\n" );
          _editor.append("Inhalt: \n" );
          _editor.append(p  + "\n\n" );
          System.out.println("Geschriebenes Objekt: " + p.getClass() );
          System.out.println("              Inhalt: " + p );
          System.out.println();
          oos.flush(); // Speicher leeren, auf Festplatte schreiben
      }
      catch (Exception e) {
          e.printStackTrace();
      }
      finally {
          if (oos != null) {
              try {
                oos.flush();
              }
              catch (IOException ioe) {}
              try {
                oos.close();
              } catch (IOException ioe) {}
          }
      }
  }  // BnWrite_Clicked

  private void BnRead_Clicked() {
    FileInputStream fis;
    ObjectInputStream ois = null;
    try {
        fis = new FileInputStream("Person2.ser");
        ois = new ObjectInputStream( fis );
        Object o = ois.readObject();
        _editor.append("Gelesenes Objekt: " + o.getClass() +"\n" );
        _editor.append("Inhalt: \n" );
        _editor.append(o  + "\n\n" );
        System.out.println("Gelesenes Objekt: " + o.getClass() );
        System.out.println("          Inhalt: " + o );
    }
    catch (Exception e) {
        e.printStackTrace();
    }
    finally {
        if (ois != null) {
            try {
              ois.close();
            } catch (IOException ioe) {}
        }
    }
  } // BnRead_Clicked



  public static void main(String[] args) {
    Serialize2 frame = new Serialize2();
    frame.setVisible(true);
  }


}


class Person implements Serializable {

    // private static final long serialVersionUID = 20080330L;

    private String firstName;
    private String lastName;
    private String password;

    private Gehalt gehalt;  // ist Klasse

    public Person(String firstName, String lastName, String password, double betrag) {
        this.firstName = firstName;
        this.lastName = lastName;
        this.password = password;
        gehalt = new Gehalt(betrag);
    }
    public String toString() {
        return new String(" "+lastName + ", " + firstName
          + "\n Passwort: " + password+ "\n"+gehalt);
    }

    public void setFirstname(String firstName) {
      this.firstName = firstName;
    }
    public void setLastname(String lastName) {
      this.lastName = lastName;
    }
    public void setPassword(String password) {
      this.password = password;
    }


    public String getFirstname() {
      return  firstName;
    }
    public String getLastname() {
      return  lastName;
    }
    public String getPassword() {
      return  password;
    }


}

class Gehalt implements Serializable {

    private double betrag;
    private transient double jahresbetrag=111;

    public Gehalt(double betrag) {
        this.betrag = betrag;
        jahresbetrag = betrag*12;
    }
    public String toString() {
        return new String(" Betrag: "+betrag+" Jahresbetrag: "+jahresbetrag );
    }

    public void init() {
        jahresbetrag = betrag*12;
    }

    public void setBetrag(double betrag) {
        this.betrag = betrag;
        jahresbetrag = betrag*12;
    }
    public double getBetrag() {
      return betrag;
    }

}


