package pack01;

import java.beans.XMLEncoder;
import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.io.Serializable;
import java.sql.Date;


public class Personne implements Serializable
{
	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;
	private String nom;
	private String prenom;
	private String adresse;
	private Date naissance;
	
	private final String sauvegarde = "sauvegarde.txt";
	
	public Personne()
	{
		this.nom = "Dupont";
		this.prenom="Albert";
		this.adresse="Rue des mésanges, 123";
		this.naissance = new Date(System.currentTimeMillis());
		
	}
	
	public Personne (String pNom, String pPrenom, String pAdresse, Date pNaiss)
	{
		this.setNom(pNom);
		this.setPrenom(pPrenom);
		this.setAdresse(pAdresse);
		this.setNaissance(pNaiss);
	}
	
	// Setters
	public void setNom(String pChaine)
	{
		this.nom = pChaine;
	}
	
	public void setPrenom(String pChaine)
	{
		this.prenom = pChaine;
	}
	
	public void setAdresse (String pChaine)
	{
		this.adresse = pChaine;
	}
	
	public void setNaissance (Date pDate)
	{
		this.naissance = pDate;
	}
	
	// Getters
	
	public String getNom()
	{
		return (this.nom);
	}
	
	public String getPrenom()
	{
		return (this.prenom);
	}
	
	public String getAdresse ()
	{
		return (this.adresse);
	}
	
	
	public Date getNaissance ()
	{
		return (this.naissance);
	}
	
	public void sauver()
	{
		ObjectOutputStream oos;
		BufferedOutputStream bof;
		FileOutputStream fos;
		XMLEncoder monXml; // = new XMLEncoder(new FileOutputStream(fileName));
		File f = new File (this.sauvegarde);
		try
		{
			fos = new FileOutputStream (f);
			bof = new BufferedOutputStream(fos);
			oos = new ObjectOutputStream(bof);
			oos.writeObject(this);
			oos.close();
			
			monXml = new XMLEncoder(new FileOutputStream(new File("dmsXml.xml")));
			monXml.writeObject(this);
			monXml.flush();
		}
		catch (FileNotFoundException e)
		{
			e.printStackTrace();
		}
		catch (IOException e)
		{
			e.printStackTrace();
		}
	}
	
	public void lire()
	{
		ObjectInputStream ois;
		BufferedInputStream bif;
		FileInputStream fis;
		File monFichier = new File (sauvegarde);
		Personne maPersonne = new Personne();
		if (monFichier.exists())
		{
			try
			{
				fis = new FileInputStream (monFichier);
				bif = new BufferedInputStream(fis);
				ois = new ObjectInputStream(bif);
				//System.out.println("Avant");
				maPersonne = (Personne)ois.readObject();
				//System.out.println("Après");
				ois.close();
			}
			catch (ClassNotFoundException e)
			{
				System.err.println("Classe non trouvée");
				e.printStackTrace();
			}
			catch (FileNotFoundException e)
			{
				System.err.println("Fichier non trouvé");
				e.printStackTrace();
			}
			catch (IOException e)
			{
				e.printStackTrace();
			}
			
			this.setNom(maPersonne.getNom());
			this.setPrenom(maPersonne.getPrenom());
			this.setAdresse(maPersonne.getAdresse());
			this.setNaissance(maPersonne.getNaissance());
		}
	}
}
