diff --git a/src/lab16_composite/ex1/Entity.java b/src/lab16_composite/ex1/Entity.java new file mode 100644 index 0000000..8768638 --- /dev/null +++ b/src/lab16_composite/ex1/Entity.java @@ -0,0 +1,8 @@ +package lab16_composite.ex1; + +public interface Entity { + void cry(); + void simulateInjury(); + void enterField(); + void shoot(); +} diff --git a/src/lab16_composite/ex1/Game.java b/src/lab16_composite/ex1/Game.java new file mode 100644 index 0000000..75ba22c --- /dev/null +++ b/src/lab16_composite/ex1/Game.java @@ -0,0 +1,33 @@ +package lab16_composite.ex1; + +public class Game { + public static void main(String[] args) { + Player jo = new Player(1); + Player jean = new Player(2); + Player paul = new Player(3); + jo.cry(); + jean.cry(); + jo.enterField(); + Team team1 = new Team(); + team1.add(jo); + team1.add(jean); + team1.add(paul); + team1.enterField(); + team1.cry(); + team1.simulateInjury(); + Player martine = new Player(3); + Player isabelle = new Player(4); + Player mariePaule = new Player(5); + + Team team2 = new Team(); + team2.add(martine); + team2.add(isabelle); + team2.add(mariePaule); + team2.add(team1); + team2.enterField(); + team2.cry(); + team2.simulateInjury(); + team2.remove(team1); + team2.simulateInjury(); + } +} \ No newline at end of file diff --git a/src/lab16_composite/ex1/Player.java b/src/lab16_composite/ex1/Player.java new file mode 100644 index 0000000..bce3ab2 --- /dev/null +++ b/src/lab16_composite/ex1/Player.java @@ -0,0 +1,30 @@ +package lab16_composite.ex1; + +public class Player implements Entity { + private int number; + + public Player(int number) { + this.number = number; + } + + @Override + public String toString() { + return "[Player " + number + "]"; + } + + public void cry() { + System.out.println(this + " ouin ouin"); + } + + public void enterField() { + System.out.println(this + " let's go !"); + } + + public void simulateInjury() { + System.out.println(this + " ouch !"); + } + + public void shoot() { + System.out.println(this + " encara Messi..."); + } +} diff --git a/src/lab16_composite/ex1/Team.java b/src/lab16_composite/ex1/Team.java new file mode 100644 index 0000000..b0fb75c --- /dev/null +++ b/src/lab16_composite/ex1/Team.java @@ -0,0 +1,44 @@ +package lab16_composite.ex1; + +import java.util.ArrayList; +import java.util.List; + +public class Team implements Entity { + private List entities = new ArrayList<>(); + + public void add(Entity entity) { + entities.add(entity); + } + + public void remove(Entity entity) { + entities.remove(entity); + } + + @Override + public void cry() { + for (Entity entity : entities) { + entity.cry(); + } + } + + @Override + public void simulateInjury() { + for (Entity entity : entities) { + entity.simulateInjury(); + } + } + + @Override + public void enterField() { + for (Entity entity : entities) { + entity.enterField(); + } + } + + @Override + public void shoot() { + for (Entity entity : entities) { + entity.shoot(); + } + } +}