From 4a2877a39f8dc5e9ee8be7c019866058bfd91287 Mon Sep 17 00:00:00 2001
From: romain achard <romain.achard@imt-atlantique.net>
Date: Mon, 3 Feb 2025 15:30:18 +0100
Subject: [PATCH] Premier invariant

---
 src/train/Element.java |  7 ++-----
 src/train/Section.java | 18 ++++++++++++++++++
 src/train/Station.java | 15 +++++++++++++++
 3 files changed, 35 insertions(+), 5 deletions(-)

diff --git a/src/train/Element.java b/src/train/Element.java
index 50fb8d9..cfbe8c8 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 ed26a96..25d2f03 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 68107b8..53eeb4a 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
+	    }
+	
 }
-- 
GitLab