Corrigé Examen Java SMI juin 2016
Classe WagonMarchandise
public WagonMarchandise(String code, String description,float volume) {
0 super(code, description); [Link] = volume; nombreWagonsMarchandise++;
}
1 public static float getVolumeMax() { return volumeMax; }
2 public static int getNombreWagonsMarchandise() { return nombreWagonsMarchandise; }
@Override
3 public String toString() { return "WagonMarchandise{" +[Link]()+ "volumeMax=" +
volumeMax + " volume=" + volume+'}'; }
Classe Train
public Train(String code, Personnel conducteur, TypeTrain type) {
[Link] = code; [Link] = new Personnel(conducteur);
4 [Link] = type; [Link] = 0;
[Link] = new ArrayList< >();
}
public Train(String code, Personnel conducteur, TypeTrain type, ArrayList<Wagon> wagons) {
[Link] = code; [Link] = new Personnel(conducteur);
5 [Link] = type; [Link] = [Link]();
[Link] = new ArrayList<>(wagons);
}
6 public int getLongueur() { return longueur; }
public void ajouterWagonMinerai(String code, String description, float tonnage) throws
TrainWagonIncompatiblesException {
if ([Link] != [Link]) { throw new
7 TrainWagonIncompatiblesException("Type wagon non autorisé pour ce type de train"); }
[Link](new WagonMinerai(code, description, tonnage));
[Link]++;
}
public void ajouterWagon(Wagon wagon) throws TrainWagonIncompatiblesException {
if ((([Link] == [Link]) && (wagon instanceof WagonMinerai)) || (([Link]
== [Link]) && (wagon instanceof WagonMarchandise)) || (([Link] ==
[Link]) && ((wagon instanceof WagonMarchandise) || (wagon
8
instanceof WagonPassagers)))) { [Link](wagon); [Link]++; }
else { throw new TrainWagonIncompatiblesException("Type wagon non autorisé pour ce
type de train"); }
}
public boolean supprimerWagon(String code) {
boolean supprimé = false;
for (Wagon w : wagons) {
9 if ([Link]().equals(code)) {
[Link](w); supprimé = true; [Link]--; break; }
}
return supprimé; }
Page 1 sur 2
public void AfficherCharge() {
float volumeTotal = 0;
float tonnageTotal = 0;
int nombreTotal = 0;
switch (type) {
case Minerai:
for (Wagon w : wagons) {
tonnageTotal += ((WagonMinerai) w).getTonnage();
}
[Link]("le tonnage total est " + tonnageTotal); break;
case marchandise:
for (Wagon w : wagons) {
volumeTotal += ((WagonMarchandise) w).getVolume();
}
[Link]("le volume total est " + volumeTotal); break;
case passagers:
10 for (Wagon w : wagons) {
nombreTotal += ((WagonPassagers) w).getNombrePassagers();
}
[Link]("le nombre total est " + nombreTotal); break;
case PassagersMarchandise:
for (Wagon w : wagons) {
if (w instanceof WagonPassagers) {
nombreTotal += ((WagonPassagers) w).getNombrePassagers();
} else {
volumeTotal += ((WagonMarchandise) w).getVolume();
}
}
[Link]("le nombre total est " + nombreTotal + " le volume total est" +
volumeTotal); break;
}
Page 2 sur 2