added lab16 ex1
This commit is contained in:
parent
927a2232dd
commit
5220b27146
8
src/lab16_composite/ex1/Entity.java
Normal file
8
src/lab16_composite/ex1/Entity.java
Normal file
@ -0,0 +1,8 @@
|
||||
package lab16_composite.ex1;
|
||||
|
||||
public interface Entity {
|
||||
void cry();
|
||||
void simulateInjury();
|
||||
void enterField();
|
||||
void shoot();
|
||||
}
|
33
src/lab16_composite/ex1/Game.java
Normal file
33
src/lab16_composite/ex1/Game.java
Normal 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();
|
||||
}
|
||||
}
|
30
src/lab16_composite/ex1/Player.java
Normal file
30
src/lab16_composite/ex1/Player.java
Normal 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...");
|
||||
}
|
||||
}
|
44
src/lab16_composite/ex1/Team.java
Normal file
44
src/lab16_composite/ex1/Team.java
Normal 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();
|
||||
}
|
||||
}
|
||||
}
|
Loading…
Reference in New Issue
Block a user