import java.awt.*;
import javax.swing.*;
import java.awt.event.*;

import java.io.*;

import java.util.*;

// schreiben und lesen von CAuto, CSchiffe, CFlugzeuge
//   Format binaer, mittel Serializer

public class klassen_read_write3 extends JFrame {

  JButton Bn1 = new JButton("write auto, schiffe, flugzeuge");
  JButton Bn2 = new JButton("lesen auto, schiffe, flugzeuge");
  JButton BnOk = new JButton("Ok");
  JButton BnEsc = new JButton("Abbruch");


  private JTextArea editor;


  public klassen_read_write3() {
    setDefaultCloseOperation( JFrame.EXIT_ON_CLOSE );
    setGUI();
  }

  private void setGUI() {
    setSize(800, 430);
    setTitle("Serialize load and save");

    Bn1.setFont( new Font("Arial", Font.BOLD,16)  );
    Bn2.setFont( new Font("Arial", Font.BOLD,16)  );
    BnOk.setFont( new Font("Arial", Font.BOLD,16)  );
    BnEsc.setFont( new Font("Arial", Font.BOLD,16)  );
    BnOk.setPreferredSize( BnEsc.getPreferredSize() );

    this.getContentPane().setLayout( new BorderLayout() );
    editor = new JTextArea("editor");
    editor.setFont( new Font("Arial", Font.BOLD,18)  );
    this.getContentPane().add( new JScrollPane(editor), BorderLayout.CENTER);

	JPanel pnButton = new JPanel();
	//pnButton.setBackground(Color.red);
	pnButton.setLayout( new FlowLayout() );
	pnButton.add(Bn1);
	pnButton.add(Bn2);
	pnButton.add(BnOk);
	pnButton.add(BnEsc);

    this.getContentPane().add( pnButton, BorderLayout.SOUTH );

          Bn1.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(ActionEvent e) {
              Bn1_Click();
            }
          });
          Bn2.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(ActionEvent e) {
              Bn2_Click();
            }
          });

          BnOk.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(ActionEvent e) {
              BnOk_Click();
            }
          });


          BnEsc.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(ActionEvent e) {
              BnEsc_Click();
            }
          });


  } // setGUI

    // speichert zwei Autos, ein Schiff, ein Flugzeug in einer Datei
    // in Datei read_write3  ist die Liste hier deklariert
    // in Datei read_write4  ist es eine eigene Klasse
   private void Bn1_Click() {
    CAuto a1 = new CAuto("VW Golf", "WR HS 1234", 4, 120, 4);
    CAuto a2 = new CAuto("Renault Megane", "MD RN 4321", 2, 120, 4);
    CSchiff s1 = new CSchiff("Titanic", 570);
    CFlugzeug f1 = new CFlugzeug("Airbus 380", 300, 200);
    ArrayList liste = new ArrayList();
    liste.add(a1);
    liste.add(a2);
    liste.add(s1);
    liste.add(f1);

    FileOutputStream Fout=null;
    ObjectOutputStream Oos=null;
    try {
      Fout = new FileOutputStream("kaufen.ser");
      Oos = new ObjectOutputStream(Fout);
      Oos.writeObject(liste);
      long uid = ObjectStreamClass.lookup( this.getClass() ).getSerialVersionUID();
      System.out.println("UID: : " + uid + "\n\n" );
      Oos.flush(); // Speicher leeren, auf Festplatte schreiben
    }
    catch (IOException e) {
        System.err.println("IOException: " + e);
    }
    finally {
       if (Oos != null) {
         try {
           Oos.flush();
         }
         catch (IOException ioe) {}
         try {
           Oos.close();
         } catch (IOException ioe) {}
       }
     }

    editor.setText("schreiben Autos, Schiff, Flugzeug\n");
    editor.append( a1.toString() );
    editor.append( a2.toString() );
    editor.append( s1.toString() );
    editor.append( f1.toString() );
   }


    // laedt zwei Autos, ein Schiff, ein Flugzeug aus einer Datei
    // in Datei read_write3  ist die Liste hier deklariert
    // in Datei read_write4  ist es eine eigene Klasse
   private void Bn2_Click() {
    CAuto a1;
    CAuto a2;
    CSchiff s1;
    CFlugzeug f1;
    ArrayList liste;

    FileInputStream fis=null;
    ObjectInputStream ois = null;    
    try {
       fis = new FileInputStream("kaufen.ser");
       ois = new ObjectInputStream( fis );
       Object o = ois.readObject();
       System.out.println("Gelesenes Objekt: " + o.getClass() );
       System.out.println("          Inhalt: " + o );
       liste = (ArrayList) o;
       editor.append( "Lesen der Liste\r\n" );
       for(Object obj : liste){
         editor.append( obj.toString()+"\r\n" );
       }
    }
    catch (Exception e) {
        e.printStackTrace();
    }
    finally {
        if (ois != null) {
            try {
              ois.close();
            } catch (IOException ioe) {}
        }
    }
   }  // read Liste


    private void BnOk_Click() {
        System.out.println("Hier in Ok");
        System.exit(0);
    }

    private void BnEsc_Click() {
        setVisible(false);
        System.exit(0);
    }


  public static void main(String[] args) {
    klassen_read_write3 frame = new klassen_read_write3();
    frame.setVisible(true);
 }

}  // klassen_read_write3


class CAuto implements Serializable {
   final int C_VERSION =1;
   private String sname;
   private String skfz;
   private int anzTueren;
   CMotor motor;

   transient int nichtgespeichert=123;

   public CAuto () {
     this("","",0,0,0);
   }
   public CAuto (String sname, String skfz, int anzTueren, int ps, int zylinder) {
     this.sname = sname;
     this.skfz = skfz;
     this.anzTueren = anzTueren;
     motor = new CMotor(ps,zylinder);
     nichtgespeichert=42;
   }


   public String toString() {
    return "Auto: Name: "+sname+"  KFZ: "+skfz+"  anzTüren: "+anzTueren+"\n";
   }
} // CAuto



class CMotor implements Serializable {
   private int ps;
   private int zylinder;

   public CMotor (int ps, int zylinder) {
     this.ps = ps;
     this.zylinder = zylinder;
   }
} // CMotor

class CSchiff implements Serializable {
   final int C_VERSION =1;
   private String sname;
   private int laenge;

   transient int nichtgespeichert=123;

   public CSchiff () {
     this("",0);
   }
   public CSchiff (String sname, int laenge) {
     this.sname = sname;
     this.laenge = laenge;
     nichtgespeichert=42;
   }


   public String toString() {
    return "Schiff: Name: "+sname+"  Laenge: "+laenge+"\n";
   }
} // CSchiff


class CFlugzeug implements Serializable {
   final int C_VERSION =1;
   private String sname;
   private int laenge;
   private int spannweite;

   transient int nichtgespeichert=123;

   public CFlugzeug () {
     this("",0,0);
   }
   public CFlugzeug (String sname, int laenge,int spannweite) {
     this.sname = sname;
     this.laenge = laenge;
     this.spannweite = spannweite;
     nichtgespeichert=42;
   }


   public String toString() {
    return "Flgzg: Name: "+sname+"  Laenge: "+laenge+"  Spannweite: "+spannweite+"\n";
   }
} // CFlugzeug