import java.io.IOException;
import java.util.*;
import java.io.*;

public class TestSerial {
  // Test des classes créées dans ce TD :
  // Créer une commande, ajouter et enlever des lignes,
  // calculer le prix total de la commande.
  // On commence par créer les articles et les lots.
  // Version sans interface graphique
  public static void main(String argv[])
    throws IOException, CloneNotSupportedException {
    // Création des articles
    /*Stylo s1, s2;
    Ramette r1;
    Lot l1, l2, l3;*/
    // Initialisations obligatoires en dehors du "try"
    Article s1 = null;
    Article s2 = null;
    Article r1 = null;
    Article l1 = null;
    Article l2 = null;
    Article l3 = null;
    try {
      s1 = new Stylo("s1", "Stylo", "Parker", 520, "Noir");
      s2 = new Stylo("s2", "Stylo", "Waterman", 800, "Bleu");
      r1 = new Ramette("r1", "Ramette", "Clairefontaine", 50, 80);
    // Création des lots
      try {
        l1 = new Lot("l1", "s1", 100);
        l2 = new Lot("l2", "r1", 50);
        l3 = new Lot("l3", "s1", 75);
      } catch (RefExistePasException e) {
          System.err.println("Lot référence " + e.getReference()
                         + " qui n'existe pas");
        System.exit(1);
      }
    } catch (RefDoubleException e) {
      System.err.println("Référence " + e.getReference()
                         + " en double");
      System.exit(1);
    }

    // Affichage pour vérification
    System.out.println(s1);
    System.out.println(s2);
    System.out.println(r1);
    System.out.println(l1);
    System.out.println(l2);
    System.out.println(l3);

    Article.enregistreArticles(new File("articles.data"));

    // Création d'une facture
    Commande f1 = new Commande("Patrick");

    // Ajout des lignes de la commande
    f1.ajouterLigneCommande(s1,3);
    f1.ajouterLigneCommande(l1,1);
    f1.ajouterLigneCommande(r1,2);

    // Affiche la facture
    // Est-ce qu'il y a un toString implicite ?
    System.out.println("*** FACTURE ***");
    System.out.println(f1);
    // Création d'une autre facture
    Commande f2 = new Commande("Richard");

    // Ajout des lignes de la commande
    f2.ajouterLigneCommande(s1,3);
    f2.ajouterLigneCommande(l1,1);
    f2.ajouterLigneCommande(r1,2);

    // Affiche la facture
    // Est-ce qu'il y a un toString implicite ?
    System.out.println("*** FACTURE ***");
    System.out.println(f2);

    // Test de clone
    Commande f3 = (Commande)f2.clone();
    System.out.println("*** FACTURE CLONE***");
    System.out.println(f3);

    // Test de l'énumeration
    System.out.println("*** FACTURE ENUMEREE ***");
    Enumeration enum = f3.lignesCommande();
    while (enum.hasMoreElements()) {
      System.out.println(enum.nextElement());
    }


    System.out.println("(press Enter to exit)");
    try {
        System.in.read();
    } catch (IOException e) {
        return;
    }

  }
}
