package com.ahuntsic.outils;

/**
 * Adresse.java
 * permet de configurer le numéro, la rue, la ville et le code postal
 * d'un Employe.
 * Date: 07/10/2005
 * Pour: Mr. Mohammed Salah Bendelloul
 * Projet: Travail pratique #2
 *
 * @author Hassen Ben Tanfous
 */

//imports
import java.io.Serializable;

public class Adresse implements Serializable {

    //critères de validation
    /** critères de validation de la rue */
    public static final int RUE_TAILLE = 20;

    /** critères de validation de la ville */
    public static final int VILLE_TAILLE = 12;

    /** critères de validation du code postal */
    public static final int CODE_POSTAL_TAILLE = 6;

    private int numero;
    private String rue,
            ville,
            codePostal;
    private MonStringTokenizer chaineJetons;

    /**
     * configure l'adresse d'un Employe
     * @param numero int
     * @param rue String
     * @param ville String
     * @param codePostal String
     */
    public Adresse(int numero, String rue, String ville, String codePostal) {
        valider(rue, ville, codePostal);
        this.numero = numero;
    }

    /**
     * permet d'initialiser une adresse sous le format
     * numéro, rue, ville, code postal
     * @param adresse String
     */
    public Adresse (String adresse) {
        chaineJetons = new MonStringTokenizer(adresse, ",");
        numero = Integer.parseInt (chaineJetons.nextJeton());
        valider (chaineJetons.nextJeton(), chaineJetons.nextJeton(), chaineJetons.nextJeton());
    }

    /**
     * valide les attributs d'après les critères de validation
     * @param rue String
     * @param ville String
     * @param codePostal String
     */
    private void valider(String rue, String ville, String codePostal) {
        if (rue.length() > RUE_TAILLE) {
            rue = rue.substring(0, RUE_TAILLE);
        }
        if (ville.length() > VILLE_TAILLE) {
            ville = ville.substring(0, VILLE_TAILLE);
        }
        if (codePostal.length() > CODE_POSTAL_TAILLE) {
            codePostal = codePostal.substring(0, CODE_POSTAL_TAILLE);
        }
        this.rue = rue;
        this.ville = ville;
        this.codePostal = codePostal;
    }

    public int getNumero() {
        return numero;
    }

    public String getRue() {
        return rue;
    }

    public String getVille() {
        return ville;
    }

    public String getCodePostal() {
        return codePostal;
    }
} //Fin de la classe Adresse
