Skip to content
Snippets Groups Projects
Commit 4a2877a3 authored by ACHARD Romain's avatar ACHARD Romain
Browse files

Premier invariant

parent 3bf8c022
Branches
No related tags found
No related merge requests found
...@@ -4,11 +4,8 @@ package train; ...@@ -4,11 +4,8 @@ package train;
* Cette classe abstraite est la représentation générique d'un élément de base d'un * 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 : * 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/> * 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 Fabien Dagnat <fabien.dagnat@imt-atlantique.fr>
* @author Philippe Tanguy <philippe.tanguy@imt-atlantique.fr> * @author Philippe Tanguy <philippe.tanguy@imt-atlantique.fr>
......
...@@ -8,7 +8,25 @@ package train; ...@@ -8,7 +8,25 @@ package train;
* @author Philippe Tanguy <philippe.tanguy@imt-atlantique.fr> * @author Philippe Tanguy <philippe.tanguy@imt-atlantique.fr>
*/ */
public class Section extends Element { public class Section extends Element {
private Train currentTrain = null;
public Section(String name) { public Section(String name) {
super(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
}
}
} }
...@@ -10,11 +10,26 @@ package train; ...@@ -10,11 +10,26 @@ package train;
*/ */
public class Station extends Element { public class Station extends Element {
private final int size; private final int size;
private int currentTrain = 0;
public Station(String name, int size) { public Station(String name, int size) {
super(name); super(name);
if(name == null || size <=0) if(name == null || size <=0)
throw new NullPointerException(); throw new NullPointerException();
this.size = size; 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
}
} }
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment