Commit 3da9edeb authored by Alan's avatar Alan

ajout du projet CV4

parent 8d7e0677
Pipeline #2555 failed with stages
public class Main {
public static void main(String[] args) {
VehiculePassagers bus = new VehiculePassagers(20, "Bus");
VehiculePassagers voiture = new VehiculePassagers(4, "Voiture");
VehiculePassagers pieton = new VehiculePassagers(1, "moto");
VehiculeComposite bateau = new VehiculeComposite();
bateau.ajouterVehicule(bus);
bateau.ajouterVehicule(voiture);
bateau.ajouterVehicule(pieton);
bateau.afficher();
}
}
\ No newline at end of file
interface Vehicule {
int getNombrePassagers();
void afficher();
}
\ No newline at end of file
import java.util.ArrayList;
import java.util.List;
class VehiculeComposite implements Vehicule {
private List<Vehicule> vehicules = new ArrayList<>();
@Override
public int getNombrePassagers() {
int totalPassagers = 0;
for (Vehicule vehicule : vehicules) {
totalPassagers += vehicule.getNombrePassagers();
}
return totalPassagers;
}
@Override
public void afficher() {
System.out.println("Le bateau contient au total : " + getNombrePassagers() + " passagers");
for (Vehicule vehicule : vehicules) {
vehicule.afficher();
}
}
public void ajouterVehicule(Vehicule vehicule) {
vehicules.add(vehicule);
}
public void retirerVehicule(Vehicule vehicule) {
vehicules.remove(vehicule);
}
}
\ No newline at end of file
class VehiculePassagers implements Vehicule {
private String type;
private int nombrePassagers;
public VehiculePassagers(int nombrePassagers, String type) {
this.nombrePassagers = nombrePassagers;
this.type = type;
}
@Override
public int getNombrePassagers() {
return nombrePassagers;
}
@Override
public void afficher() {
System.out.println(type + " contient : " + nombrePassagers + " passagers");
}
}
\ No newline at end of file
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment