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

import java.io.*;

import java.util.*;

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

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

public class klassen_read_write1 extends JFrame {

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


  private JTextArea editor;


  public klassen_read_write1() {
    setDefaultCloseOperation( JFrame.EXIT_ON_CLOSE );
    setGUI();
  }

  private void setGUI() {
    setSize(800, 430);
    setTitle("JFrame als Dialog");

    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 unterschiedlichen Dateien
   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.write2File("auto1.bin");
    editor.setText("schreiben\n");
    editor.append( a1.toString() );
    a2.write2File("auto2.bin");
    editor.append( a2.toString() );
   }

    // laedt zwei Autos aus unterschiedlichen Dateien
   private void Bn2_Click() {
    CAuto a1 = new CAuto();
    CAuto a2 = new CAuto();
    a1.readFromFile("auto1.bin");
    a2.readFromFile("auto2.bin");
    editor.setText("lesen\n");
    editor.append( a1.toString() );
    editor.append( a2.toString() );
   }

   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.write2File("autos.bin");
    editor.append( garage.toString() );
   }

   private void Bn4_Click() {
    CAutos garage = new CAutos();
    garage.readFromFile("autos.bin");
    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_write1 frame = new klassen_read_write1();
    frame.setVisible(true);
 }

}  // klassen_read_write1

class CAutos {
   final int C_VERSION =1;
   private ArrayList<CAuto> liste;

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

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


   public void write2File(String sFilename) {
    try {
      FileOutputStream Fout = new FileOutputStream(sFilename);
      DataOutputStream Dout = new DataOutputStream(Fout);
      Dout.writeInt(C_VERSION);
      int n = liste.size();
      Dout.writeInt(n);
      for (CAuto a: liste) {
        a.write2File(Dout);
      }
      Dout.close();
      Fout.close();
    }
    catch (IOException e) {
        System.err.println("IOException: " + e);
    }
   } // write2File

   public void readFromFile(String sFilename) {
    CAuto a;
    int i, n, version;
    try {
      FileInputStream Fin = new FileInputStream(sFilename);
      DataInputStream Din = new DataInputStream(Fin);
      version = Din.readInt();
      if (version != C_VERSION) {
        throw new IOException("Modul readFromFile, falsche Version"); 
      }
      liste.clear();
      n = Din.readInt();
      for (i=0; i<n; i++) {
        a = new CAuto();
        a.readFromFile(Din);
        liste.add(a);
      }
      Din.close();
      Fin.close();
    }
    catch (IOException e) {
        System.err.println("IOException: " + e);
    }
   } // readFromFile

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

} // CAutos


class CAuto {
   final int C_VERSION =1;
   final byte C_MagicNumber1 = 87 ;  // 87=W    77=M
   final byte C_MagicNumber2 = 73 ;  // 73=I
   private String sname;
   private String skfz;
   private int anzTueren;
   CMotor motor;

   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);
   }

   public void write2File(String sFilename) {
    try {
      FileOutputStream Fout = new FileOutputStream(sFilename);
      DataOutputStream Dout = new DataOutputStream(Fout);
      Dout.writeByte(C_MagicNumber1);
      Dout.writeByte(C_MagicNumber2);
      Dout.writeInt(C_VERSION);
      Dout.writeUTF(sname);
      Dout.writeUTF(skfz);
      Dout.writeInt(anzTueren);
      motor.write2File(Dout);
      Dout.close();
      Fout.close();
    }
    catch (IOException e) {
        System.err.println("IOException: " + e);
    }
   } // write2File

   public void readFromFile(String sFilename) {
    try {
      FileInputStream Fin = new FileInputStream(sFilename);
      DataInputStream Din = new DataInputStream(Fin);
      char ch1 = (char) Din.readByte();
      char ch2 = (char) Din.readByte();
      if ( (ch1 !=C_MagicNumber1) || (ch2 !=C_MagicNumber2) ) {
        System.out.println("error");
        throw new IOException("Modul readFromFile, falsche MagicNumber"); 
      }
      int version = Din.readInt();
        System.out.println("v: "+version);
      if (version != C_VERSION) {
        System.out.println("error");
        throw new IOException("Modul readFromFile, falsche Version"); 
      }
      sname = Din.readUTF();
      skfz = Din.readUTF();
      anzTueren =Din.readInt();
      motor.readFromFile(Din);
      Din.close();
      Fin.close();
    }
    catch (IOException e) {
        System.err.println("IOException: " + e);
    }
   } // readFromFile


   public void write2File(DataOutputStream Dout) {
    try {
      Dout.writeInt(C_VERSION);
      Dout.writeUTF(sname);
      Dout.writeUTF(skfz);
      Dout.writeInt(anzTueren);
      motor.write2File(Dout);
    }
    catch (IOException e) {
        System.err.println("IOException: " + e);
    }
   } // write2File

   public void readFromFile(DataInputStream Din) {
    try {
      int version = Din.readInt();
      if (version != C_VERSION) {
        throw new IOException("Modul readFromFile, falsche Version"); 
      }
      sname = Din.readUTF();
      skfz = Din.readUTF();
      anzTueren =Din.readInt();
      motor.readFromFile(Din);
    }
    catch (IOException e) {
        System.err.println("IOException: " + e);
    }
   } // readFromFile

   public String toString() {
    return "Name: "+sname+"  KFZ: "+skfz+"  anzTüren: "+anzTueren+"\n"+
        motor.toString()+"\n\n";
   }
} // CAuto


class CMotor {
   final int C_VERSION=1;
   private int ps;
   private int zylinder;


   public CMotor (int ps, int zylinder) {
     this.ps = ps;
     this.zylinder = zylinder;
   }

   public void write2File(DataOutputStream Dout) {
    try {
      Dout.writeInt(C_VERSION);
      Dout.writeInt(ps);
      Dout.writeInt(zylinder);
    }
    catch (IOException e) {
        System.err.println("IOException: " + e);
    }
   } // write2File

   public void readFromFile(DataInputStream Din) {
    try {
      int version = Din.readInt();
      if (version != C_VERSION) {
        throw new IOException("Modul readFromFile, falsche Version"); 
      }
      ps = Din.readInt();
      zylinder = Din.readInt();
    }
    catch (IOException e) {
        System.err.println("IOException: " + e);
    }
   } // readFromFile

   public String toString() {
    return "  Motor:   ps "+ps+"  Zylinder: "+zylinder+"\n";
   }

} // CMotor


