added lab16 ex1

This commit is contained in:
Louis Heredero 2024-11-18 10:52:57 +01:00
parent 927a2232dd
commit 5220b27146
Signed by: HEL
GPG Key ID: 8D83DE470F8544E7
4 changed files with 115 additions and 0 deletions

View File

@ -0,0 +1,8 @@
package lab16_composite.ex1;
public interface Entity {
void cry();
void simulateInjury();
void enterField();
void shoot();
}

View File

@ -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();
}
}

View File

@ -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...");
}
}

View File

@ -0,0 +1,44 @@
package lab16_composite.ex1;
import java.util.ArrayList;
import java.util.List;
public class Team implements Entity {
private List<Entity> 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();
}
}
}