next up previous
suivant: Et ensuite... monter: Exemple de programme JAVA précédent: Tableau extensible

Structure de donnée au choix

Cette nouvelle version du programme est plus abstraite et utilise une interface. On peut utiliser n'importe quelle structure de donnée pour contenir les produits du stock: un tableau, mais aussi une liste chainée, une table de hachage, etc. On peut décrire la gestion de stock indépendament de la structure de donnée utilisée.

Dans cet exemple, nous avons utilisé les tableaux d'un côté, les listes chaînées de l'autre, encapsulées dans deux classes qui implémentent la même interface (SDD). Ce programme illustre l'utilisation d'interface. La méthode main gère deux stocks différents avec deux structures de données sous-jacentes différentes. Mais la classe Stock est unique.

class Bizarre extends Exception{
    String msg;
    Bizarre(String s){
	msg = s;
    }
    void print(){
	System.out.println(msg);
    }
}
    

class Product{
    String nom;
    int ref;
    int qte;
    void print(){
	System.out.print(nom+" "+ref+" "+qte);
    }
    Product(String n, int r, int q){
	nom = n;
	ref = r;
	qte = q;
    }
    Product stocker(int delta)throws Bizarre{
	if (delta < 0)
	    throw new Bizarre("stocker: parametre negatif");
	qte = qte + delta;
	return this;
    }
    Product destocker(int delta)throws Bizarre{
	if (delta < 0)
	    throw new Bizarre("destocker: parametre negatif");
	if (delta > qte)
	    throw new Bizarre("destocker: stock insuffisant");
	qte = qte - delta;
	return this;
    }
}

interface SDD{
    SDD ajouter(Product p);
    SDD retirer(int ref) throws Bizarre;
    Product retrouver(int ref);
    void initialiserParcours();
    Product suivant();
}
    

class Stock{
    SDD sdd;
    Stock(SDD s){
	sdd = s;
    }
    Stock referencer(String n, int r) throws Bizarre{
	if (sdd.retrouver(r) != null)
	    throw new Bizarre("referencer: cette reference existe: " + r);
	sdd.ajouter(new Product(n,r,0));
	return this;
    }
    Stock dereferencer(int r) throws Bizarre{
	try{
	    sdd.retirer(r);
	}catch(Bizarre biz){
	    throw new Bizarre("dereferencer: cette reference n'existe: pas " + r);
	}
	return this;
    }
    Stock ajouter(int r, int delta) throws Bizarre{
	Product p = sdd.retrouver(r);
	if (p == null)
	    throw new Bizarre("ajouter: cette reference n'existe: pas " + r);
	p.stocker(delta);
	return this;
    }
    Stock retirer(int r, int delta) throws Bizarre{
	Product p = sdd.retrouver(r);
	if (p == null)
	    throw new Bizarre("ajouter: cette reference n'existe: pas " + r);
	p.destocker(delta);
	return this;
    }
    void etat(){
	Product p;
	sdd.initialiserParcours();
	while (true){
	    p=sdd.suivant();
	    if (p == null)
		break;
	    p.print();
	    System.out.println();
	}
    }
}

class Tableau implements SDD {
    Product[] t = new Product[10];
    int nb = 0;
    int i = 0;
    public SDD ajouter(Product p){
	if (nb == t.length){
	    Product[] gt = new Product[nb+10];   
	    for (int j = 0; j<nb; j++){
		gt[j]=t[j];
	    }
	    t=gt;
	}
	t[nb]=p;
	nb++;
	return this;
    }    
    public Product retrouver(int ref){
	for(int j = 0; j<nb; j++){
	    if (t[j].ref == ref)
		return t[j];
	}
	return null;
    }	
    public SDD retirer(int ref) throws Bizarre{
	for(int j = 0; j<nb; j++){
	    if (t[j].ref == ref){
		t[j]=t[nb-1];
		nb--;
		return this;
	    }
	}
	throw new Bizarre("Tableau: retirer: non trouvee, ref " + ref);
    }
    public void initialiserParcours(){
	i = 0;
    }
    public Product suivant(){
	if (i<nb){
	    i++;
	    return t[i-1];
	}else
	    return null;
    }    
}


class EnteteListe implements SDD{
    Maillon debut;
    Maillon parcours;
    public SDD ajouter(Product p){
	debut = new Maillon(p, debut);
	return this;
    }
    public SDD retirer(int ref) throws Bizarre{
	if (debut == null)
	    throw new Bizarre("EnteteListe: retirer: reference inconnue " + ref);
	debut = debut.retirer(ref);
	return this;
    }
    public Product retrouver(int ref){
	if (debut == null)
	    return null;
	return debut.retrouver(ref);
    }
    public void initialiserParcours(){
	parcours = debut;
    }
    public Product suivant(){
	if (parcours == null)
	    return null;
	else{
	    Product res = parcours.tete;
	    parcours = parcours.reste;
	    return res;
	}
    }
}

class Maillon{
    Product tete;
    Maillon reste;
    Maillon(Product p, Maillon s){
	tete=p;
	reste=s;
    }
    Maillon retirer(int ref) throws Bizarre{
	if (tete.ref == ref)
	    return reste;
	else{
	    if (reste == null)
		throw new Bizarre("EnteteListe: retirer: reference inconnue " + ref);
	    Maillon m = reste.retirer(ref);
	    reste = m;
	    return this;
	}
    }
    Product retrouver(int ref){
	if (tete.ref == ref)
	    return tete;
	else
	    if(reste == null)
		return null;
	    else
		return reste.retrouver(ref);
    }
}


abstract class ListeChainee implements SDD{
    ListeChainee parcours;
    public SDD ajouter(Product p){
	return new ListeChaineeNonVide(p,this);
    }
    abstract public SDD retirer(int ref) throws Bizarre;
    abstract public Product retrouver(int ref);
    public void initialiserParcours(){
	parcours = this;
    }
    abstract public Product suivant();
    abstract Product premier() throws Bizarre;
    abstract ListeChainee reste() throws Bizarre;
    abstract void inspection();
}
    
   

class Produit4{
    public static void main(String[] arg){
	Stock st = new Stock(new Tableau());
	Stock stlc = new Stock(new EnteteListe());
	try{
	    st.referencer("peigne",1001);
	    st.referencer("brosse",999);
	    st.ajouter(1001,100);
	    st.referencer("lime",1003);
	    st.ajouter(1003,50);
	    st.retirer(1001,3);
	    st.etat();
	    System.out.println("---------------------");
	    stlc.referencer("moule a cake",1501);
	    stlc.referencer("rouleau",989);
	    stlc.etat();
	}catch (Bizarre biz){
	    System.out.println("Une erreur en survenue: ");
	    biz.print();
	    System.out.println();
	}
    }
}



Barthelemy Francois 2002-03-07