diff --git a/src/train/Element.java b/src/train/Element.java index 50fb8d9529d02e3e4c906fd946b742e7f29ff218..cfbe8c8a59f3219ac23c896706ea8dfd81fdbef6 100644 --- a/src/train/Element.java +++ b/src/train/Element.java @@ -4,11 +4,8 @@ package train; * Cette classe abstraite est la représentation générique d'un élément de base d'un * circuit, elle factorise les fonctionnalités communes des deux sous-classes : * l'entrée d'un train, sa sortie et l'appartenance au circuit.<br/> - * Les deux sous-classes sont : - * <ol> - * <li>La représentation d'une gare : classe {@link Station}</li> - * <li>La représentation d'une section de voie ferrée : classe {@link Section}</li> - * </ol> + * + * * * @author Fabien Dagnat <fabien.dagnat@imt-atlantique.fr> * @author Philippe Tanguy <philippe.tanguy@imt-atlantique.fr> diff --git a/src/train/Section.java b/src/train/Section.java index ed26a960dacb2a33e4ba297c1d62c75359800bde..25d2f03c01efc2608aba91e13577b310209bdfd2 100644 --- a/src/train/Section.java +++ b/src/train/Section.java @@ -8,7 +8,25 @@ package train; * @author Philippe Tanguy <philippe.tanguy@imt-atlantique.fr> */ public class Section extends Element { + private Train currentTrain = null; + public Section(String name) { super(name); + this.currentTrain = null; } + + public synchronized void enterSection(Train t) throws InterruptedException { + while (currentTrain != null) { // Attendre que la section soit libre + wait(); + } + currentTrain = t; + } + + public synchronized void leaveSection(Train t) { + if (currentTrain == t) { + currentTrain = null; + notifyAll(); // Réveille les trains en attente + } + } + } diff --git a/src/train/Station.java b/src/train/Station.java index 68107b8a5cbf22059f0e6afecb994e8b9a3265b9..53eeb4a5b4499f7993b9d148128d6f87e6f96b12 100644 --- a/src/train/Station.java +++ b/src/train/Station.java @@ -10,11 +10,26 @@ package train; */ public class Station extends Element { private final int size; + private int currentTrain = 0; public Station(String name, int size) { super(name); if(name == null || size <=0) throw new NullPointerException(); this.size = size; + this.currentTrain = 0; } + + public synchronized void enterStation(Train t) throws InterruptedException { + while (currentTrain >= size) { // Si la gare est pleine, attendre + wait(); + } + currentTrain++; + } + + public synchronized void leaveStation(Train t) { + currentTrain--; + notifyAll(); // Réveille les trains en attente + } + }