blob: 664bf60fbd9996900f1f38e52f5b2ee8ee4c23ee (
plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
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();
}
}
|