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

// Komplexe Klasse wird in ein Vector eingefügt und ausgegeben 
// verwendet Singleton Pattern 
// CPrintStream p = CPrintStream.getInstance(); 

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

import java.util.Vector;

public class Vector02 {

   private static void pause() {
    try {
      System.in.read();
    }
    catch (IOException e) {
    }
  }  // pause


  public static void main(String[] args) {
    Vector<CKlasse> myVector = new Vector<CKlasse>();
    CKlasse kl;  // def
    kl = new CKlasse(12,123,1);  // create  a=12  b=123  tab=1
      myVector.add(kl);  // eingefügt
    kl = new CKlasse(123,55,2);  // create  a=123  b=55  tab=2
      myVector.add(kl);  // eingefügt
    kl = new CKlasse(33,-444,3);  // create  a=33  b=-444  tab=3
      myVector.add(kl);  // eingefügt
    kl = new CKlasse(12333,-555,2);  // create  a=12333  b=-555  tab=2
      myVector.add(kl);  // eingefügt

      // writeXML   open printstream
    CPrintStream p = CPrintStream.getInstance();
     // schreiben der Daten
    p.openFile2Write("Vector2.xml");

     System.out.println("Auslesen und schreiben");
    System.out.println("\nAlter Vector");
    for (int i=0; i<myVector.size(); i++) {
      kl = myVector.elementAt(i);
      kl.print();
    }

     // neuer Iterator
    System.out.println("\nNeuer Vector");
    for (CKlasse kl2:myVector) {
      kl2.print();
    }
    pause();
  }

}


class CKlasse {
  int a;
  int b;
  int tab;


  CKlasse(int a, int b, int tab) {
    this.a = a;
    this.b = b;
    this.tab = tab;
  }

  public void print() {
    // CPrintStream p = new CPrintStream();  ist fehlerhaft
    CPrintStream p = CPrintStream.getInstance();
    p.setTab(tab);
    p.write(a);
    p.write(b);
  }


}



class CPrintStream {
	private static CPrintStream instance = new CPrintStream();
  private int tab=0;
  FileOutputStream Fout;
  PrintStream pout;


	private CPrintStream(){
	}

    public static CPrintStream getInstance(){
            return instance;
    }

    public void openFile2Write(String sFilename) {
      try {
        Fout = new FileOutputStream(sFilename);
     		// Druckausgabe
        pout = new PrintStream(Fout);
      }
      catch (IOException ee) {
        System.err.println("IOException: " + ee);
      }
  }

    public void setTab(int tab) {
      this.tab = tab;
    }

    public void incTab() {
      tab++;
    }
    public void decTab() {
      if (tab>0) tab--;
    }

    public void write(int a){
        for (int i=0; i<tab; i++) {
          System.out.print("  ");
          //pout.print("  ");
          pout.print('\t');
        }
        System.out.println(a);
        pout.println(a);
    }



}  // CPrintStream

