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

import java.io.*;

import java.util.*;

// schreiben und lesen von CAuto
//   Format binaer, mittel Serializer

// schreiben und lesen von CAutos
//   Format binaer, mittel Serializer

public class klassen_read_write2 extends JFrame {

  JButton Bn1 = new JButton("write Auto s");
  JButton Bn2 = new JButton("lesen Auto s");
  JButton Bn3 = new JButton("write Garage s");
  JButton Bn4 = new JButton("lesen Garage s");
  JButton BnOk = new JButton("Ok");
  JButton BnEsc = new JButton("Abbruch");


  private JTextArea editor;


  public klassen_read_write2() {
    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)  );
    Bn3.setFont( new Font("Arial", Font.BOLD,16)  );
    Bn4.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(Bn3);
	pnButton.add(Bn4);
	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();
            }
          });

          Bn3.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(ActionEvent e) {
              Bn3_Click();
            }
          });
          Bn4.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(ActionEvent e) {
              Bn4_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 in jeweils einer Datei
   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);
    a1.write2FileSerialize("auto1.ser");
    editor.setText("schreiben\n");
    editor.append( a1.toString() );
    a2.write2FileSerialize("auto2.ser");
    editor.append( a2.toString() );
   }

    // laedt zwei Autos aus jeweils einer Datei
   private void Bn2_Click() {
    CAuto a1;
    CAuto a2;
     // static Methode
    a1 = CAuto.readFromFileSerialize("auto1.ser");
    a2 = CAuto.readFromFileSerialize("auto2.ser");
    editor.setText("lesen\n");
    editor.append( a1.toString() );
    editor.append( a2.toString() );
   }


    // speichert zwei Autos aus einer Liste in einer Datei
   private void Bn3_Click() {
    CAutos garage = new CAutos();
    CAuto a1 = new CAuto("VW Golf", "WR HS 1234", 4, 120, 4);
    CAuto a2 = new CAuto("Renault Megane", "MD RN 4321", 2, 120, 4);
    garage.add(a1);
    garage.add(a2);
    editor.setText("schreiben aus liste\n");
    garage.write2FileSerialize("autos.ser");
    editor.append( garage.toString() );
   }

    // laedt zwei Autos aus einer Liste aus einer Datei
   private void Bn4_Click() {
    CAutos garage;
    garage = CAutos.readFromFileSerialize("autos.ser");
    editor.setText("lesen in liste\n");
    editor.append( garage.toString() );
   }


    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_write2 frame = new klassen_read_write2();
    frame.setVisible(true);
 }

}  // klassen_read_write1



// Liste ist typisiert
// in read_write_3 gibt es mehrere Klassen zu speichern
class CAutos implements Serializable {
   final int C_VERSION =1;
   private ArrayList<CAuto> liste;

   public CAutos () {
     liste = new ArrayList<CAuto>();
   }

   public void add(CAuto a) {
     liste.add(a);
   }



    // static, da man mit Serialize ein Objekt erstellt
   public static CAutos readFromFileSerialize(String sFilename) {
    FileInputStream fis=null;
    ObjectInputStream ois = null;
    CAutos a=null;
    try {
        fis = new FileInputStream(sFilename);
        ois = new ObjectInputStream( fis );
        Object o = ois.readObject();
        System.out.println("Gelesenes Objekt: " + o.getClass() );
        System.out.println("          Inhalt: " + o );
        a = (CAutos) o;
    }
    catch (Exception e) {
        e.printStackTrace();
    }
    finally {
        if (ois != null) {
            try {
              ois.close();
            } catch (IOException ioe) {}
        }
    }
    return a;
   } // readFromFileSerialize

   public void write2FileSerialize(String sFilename) {
    FileOutputStream Fout=null;
    ObjectOutputStream Oos=null;
    try {
      Fout = new FileOutputStream(sFilename);
      Oos = new ObjectOutputStream(Fout);
      Oos.writeObject(this); // Garage
      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) {}
       }
     }
   } // write2File

   public String toString() {
     String sValue="";
     for (CAuto a: liste) {
       sValue+= a.toString()+"\n";
      }
     return sValue;
   }

} // CAutos


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 void write2FileSerialize(String sFilename) {
    FileOutputStream Fout=null;
    ObjectOutputStream Oos=null;
    try {
      Fout = new FileOutputStream(sFilename);
      Oos = new ObjectOutputStream(Fout);
      Oos.writeObject(this);
      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) {}
       }
      }
   } // write2File

    // static, da man mit Serialize ein Objekt erstellt
   public static CAuto readFromFileSerialize(String sFilename) {
    FileInputStream fis=null;
    ObjectInputStream ois = null;
    CAuto a=null;
    try {
        fis = new FileInputStream(sFilename);
        ois = new ObjectInputStream( fis );
        Object o = ois.readObject();
        System.out.println("Gelesenes Objekt: " + o.getClass() );
        System.out.println("          Inhalt: " + o );
        a = (CAuto) o;
    }
    catch (Exception e) {
        e.printStackTrace();
    }
    finally {
        if (ois != null) {
            try {
              ois.close();
            } catch (IOException ioe) {}
        }
    }
    return a;
   } // readFromFileSerialize

   public String toString() {
    return "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


