1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96
| public class Stade {
public static void main(String[] args) {
int a;
do{
System.out.println("1. créer un ballon\n2. détruire un ballon\n3. afficher la liste des ballons\n0. quitter\nQuel est votre choix?");
a = Keyboard.getInt();
System.out.println("choix"+" "+a);
if(a==1){
Ballon.creer_instance();
}
else if(a==3){
Ballon.aff();
}
else if(a==2){
System.out.println(Ballon.getInstance("paulo"));
}
}
while(a!=0);
}
}
==========================================
import java.util.Iterator;
import java.util.Vector;
public class Ballon {
public String identificateur;
public int taille;
public static Vector mesInstances = new Vector() ;
public Ballon(String s, int t){
this.identificateur=s;
this.taille=t;
}
public String toString() {
return (this.identificateur +" "+this.taille );
}
public static Ballon creer_instance(){
System.out.println("Tapez un identificateur :");
String s = Keyboard.getString();
System.out.println("Tapez une taille :");
int t = Keyboard.getInt();
Ballon b = new Ballon(s, t);
System.out.println("Ballon b cree." + b);
mesInstances.addElement(b);
//Keyboard.pause();
return b;
}
public static void aff(){
System.out.println(mesInstances);
}
public void detruire(){
}
/*fonction qui retourne linstance de la classe
Ballon possédant un identificateur égal à la String passée en paramètre. Elle est
appelée à chaque fois que lon cherche une instance connaissant son identificateur. Elle
retourne null si aucune instance ne correspond à la String passée en paramètre.*/
public static Ballon getInstance(String s){
Iterator it= mesInstances.iterator();
Object b = it.next();
do{
if(((Ballon)b).identificateur==s){
return (Ballon) b;
}
}
while (it.hasNext()) ;
return null;
}
} |