From 2c530f2a58014e8e9479c41dabff9a639e9297a5 Mon Sep 17 00:00:00 2001 From: Anhgelus Morhtuuzh Date: Mon, 16 Feb 2026 10:16:31 +0100 Subject: =?UTF-8?q?Cours=20du=2002=20au=2013=20f=C3=A9vrier?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- semestre 4/java/tme/tp3/Personne.java | 52 +++++++++++++++++++++++++++++++++++ 1 file changed, 52 insertions(+) create mode 100644 semestre 4/java/tme/tp3/Personne.java (limited to 'semestre 4/java/tme/tp3/Personne.java') diff --git a/semestre 4/java/tme/tp3/Personne.java b/semestre 4/java/tme/tp3/Personne.java new file mode 100644 index 0000000..664bf60 --- /dev/null +++ b/semestre 4/java/tme/tp3/Personne.java @@ -0,0 +1,52 @@ +public class Personne { + public final String name; + private Personne conjoint; + + public Personne(String n) { + name = n; + } + + public Personne() { + this("Pers" + (char) ((int) (Math.random() * 26) + 'A')); + } + + public String toString() { + return String.format("%s, %s", name, conjoint == null ? "célibataire" : "marié(e)"); + } + + public void epouser(Personne p) { + if (p.conjoint != null || conjoint != null || p == this) { + System.out.println(String.format( + "Le mariage de %s avec %s est impossible", this, p + )); + return; + } + System.out.println(String.format( + "%s se marie avec %s", this, p + )); + conjoint = p; + p.conjoint = this; + } + + public void divorcer() { + if (conjoint == null) return; + System.out.println(String.format( + "%s divorce avec %s", this, conjoint + )); + conjoint.conjoint = null; + conjoint = null; + } + + public static void main(String[] args) { + final var p1 = new Personne(); + final var p2 = new Personne(); + final var p3 = new Personne(); + + p1.epouser(p2); + p1.epouser(p3); + p3.epouser(p1); + p3.epouser(p3); + p1.divorcer(); + p3.divorcer(); + } +} -- cgit v1.2.3