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
| public class Garage2 {
protected String nomGarage;
protected LinkedList<Voiture> listeChainee;
---------------------------------------------------------------------------
public Garage2(String nomGarage, Voiture... voitures) {
this.nomGarage = nomGarage;
listeChainee = new LinkedList();
for (Voiture v : voitures) {
this.listeChainee.add(v);
}
}
---------------------------------------------------------------------------
public void affiche() {
Voiture [] tmpTab = null;
//met la liste dans un tableau. Pas de boucle, pas de source d'erreur
tmpTab = listeChainee.toArray(tmpTab);
if (this.listeChainee.isEmpty())
System.out.print("Ce garage NE contient PAS de voiture(s) :\n");
else {
System.out.print("Le garage "+this.nomGarage+" contient "+tmpTab.length+" voitures :\n");
---------------------------------------------------------------------------
for (Voiture v : tmpTab) {
System.out.print("Voiture "+v+"\n\n");
}
---------------------------------------------------------------------------
}
}
public static void main(String[] args) {
Voiture car_1 = new Voiture("Renault", "Mégane", 13900.85, 2004);
car_1.Affiche();
Voiture car_2 = new Voiture("Peugeot", "407", 16900.85, 2005);
car_2.Affiche();
Voiture car_3 = new Voiture("Citroën", "C6", 28900.85, 2006);
car_3.Affiche();
Garage2 concess_1 = new Garage2("Dabireau", car_1, car_2, car_3);
concess_1.affiche();
Voiture [] tabTempVoiture= {car_1, car_2, car_3};
Garage2 g1=new Garage2("Dabireau_bis", tabTempVoiture);
g1.affiche();
Garage2 concess_2 = new Garage2("Saroteau", car_1, car_2, car_3);
concess_2.affiche();
}
} |
Partager