summaryrefslogtreecommitdiff
path: root/graphs/java/creatureInterface/src/main
diff options
context:
space:
mode:
authorMartial Simon <msimon_fr@hotmail.com>2025-09-15 01:08:27 +0200
committerMartial Simon <msimon_fr@hotmail.com>2025-09-15 01:08:27 +0200
commitc9b6b9a5ca082fe7c1b6f58d7713f785a9eb6a5c (patch)
tree3e4f42f93c7ae89a364e4d51fff6e5cec4e55fa9 /graphs/java/creatureInterface/src/main
add: graphs et rushs
Diffstat (limited to 'graphs/java/creatureInterface/src/main')
-rw-r--r--graphs/java/creatureInterface/src/main/java/fr/epita/assistants/creatureInterface/BaseHuman.java51
-rw-r--r--graphs/java/creatureInterface/src/main/java/fr/epita/assistants/creatureInterface/Creature.java28
-rw-r--r--graphs/java/creatureInterface/src/main/java/fr/epita/assistants/creatureInterface/Dragon.java74
-rw-r--r--graphs/java/creatureInterface/src/main/java/fr/epita/assistants/creatureInterface/Fish.java27
-rw-r--r--graphs/java/creatureInterface/src/main/java/fr/epita/assistants/creatureInterface/FlyingInterface.java12
-rw-r--r--graphs/java/creatureInterface/src/main/java/fr/epita/assistants/creatureInterface/Human.java8
-rw-r--r--graphs/java/creatureInterface/src/main/java/fr/epita/assistants/creatureInterface/Mage.java59
-rw-r--r--graphs/java/creatureInterface/src/main/java/fr/epita/assistants/creatureInterface/MagicalInterface.java56
-rw-r--r--graphs/java/creatureInterface/src/main/java/fr/epita/assistants/creatureInterface/Mermaid.java110
-rw-r--r--graphs/java/creatureInterface/src/main/java/fr/epita/assistants/creatureInterface/SpeakableInterface.java47
-rw-r--r--graphs/java/creatureInterface/src/main/java/fr/epita/assistants/creatureInterface/Spell.java48
-rw-r--r--graphs/java/creatureInterface/src/main/java/fr/epita/assistants/creatureInterface/SpellType.java11
-rw-r--r--graphs/java/creatureInterface/src/main/java/fr/epita/assistants/creatureInterface/SwimmingInterface.java25
13 files changed, 556 insertions, 0 deletions
diff --git a/graphs/java/creatureInterface/src/main/java/fr/epita/assistants/creatureInterface/BaseHuman.java b/graphs/java/creatureInterface/src/main/java/fr/epita/assistants/creatureInterface/BaseHuman.java
new file mode 100644
index 0000000..308fde9
--- /dev/null
+++ b/graphs/java/creatureInterface/src/main/java/fr/epita/assistants/creatureInterface/BaseHuman.java
@@ -0,0 +1,51 @@
+package fr.epita.assistants.creatureInterface;
+
+public abstract class BaseHuman extends Creature implements SpeakableInterface, SwimmingInterface{
+ /**
+ * Constructor for the Creature class.
+ *
+ * @param name The name of the creature
+ */
+ boolean inWater;
+ BaseHuman(String name) {
+ super(name);
+ inWater = false;
+ }
+
+ @Override
+ public String getName() {
+ return name;
+ }
+
+ @Override
+ public void hello() {
+ System.out.println("Hello, my name is " + this.name + " and I'm a " + this.getClass().getSimpleName() + ".");
+ }
+
+ @Override
+ public void greet(SpeakableInterface contact) {
+ if (contact instanceof Human)
+ System.out.println("Hello " + contact.getName() + ", how are you?");
+ else
+ SpeakableInterface.super.greet(contact);
+ }
+
+ @Override
+ public void swim()
+ {
+ inWater = true;
+ System.out.println("I'm a " + this.getClass().getSimpleName() + " and I'm swimming.");
+ }
+
+ @Override
+ public boolean getSwimmingState()
+ {
+ return inWater;
+ }
+
+ @Override
+ public void emerge()
+ {
+ inWater = false;
+ }
+}
diff --git a/graphs/java/creatureInterface/src/main/java/fr/epita/assistants/creatureInterface/Creature.java b/graphs/java/creatureInterface/src/main/java/fr/epita/assistants/creatureInterface/Creature.java
new file mode 100644
index 0000000..1f63459
--- /dev/null
+++ b/graphs/java/creatureInterface/src/main/java/fr/epita/assistants/creatureInterface/Creature.java
@@ -0,0 +1,28 @@
+package fr.epita.assistants.creatureInterface;
+
+/**
+ * This abstract class provides a blueprint for creating creatures.
+ * All creatures extend this class.
+ */
+public abstract class Creature {
+ // The name of the creature
+ protected String name;
+
+ /**
+ * Constructor for the Creature class.
+ *
+ * @param name The name of the creature
+ */
+ Creature(String name) {
+ this.name = name;
+ }
+
+ /**
+ * Get the name of the creature.
+ *
+ * @return The name of the creature
+ */
+ public String getName() {
+ return name;
+ }
+}
diff --git a/graphs/java/creatureInterface/src/main/java/fr/epita/assistants/creatureInterface/Dragon.java b/graphs/java/creatureInterface/src/main/java/fr/epita/assistants/creatureInterface/Dragon.java
new file mode 100644
index 0000000..49ee99d
--- /dev/null
+++ b/graphs/java/creatureInterface/src/main/java/fr/epita/assistants/creatureInterface/Dragon.java
@@ -0,0 +1,74 @@
+package fr.epita.assistants.creatureInterface;
+
+import java.util.ArrayList;
+import java.util.Collection;
+import java.util.List;
+
+public class Dragon extends Creature implements MagicalInterface, SpeakableInterface, FlyingInterface {
+ int mana;
+ Collection<Spell> spells = new ArrayList<Spell>();
+
+ public Dragon(String name, int mana) {
+ super(name);
+ this.mana = mana;
+ }
+
+ @Override
+ public void hello() {
+ System.out.println("Hello, my name is " + this.name + " and I'm a Dragon.");
+ }
+
+ @Override
+ public void greet(SpeakableInterface contact) {
+ if (contact instanceof Dragon) {
+ System.out.println("Greetings Lord " + contact.getName() + ".");
+ MagicalInterface.super.doSomeSparkles();
+ }
+ else
+ SpeakableInterface.super.greet(contact);
+ }
+
+ @Override
+ public void fly() {
+ System.out.println("I'm a Dragon and I'm flying.");
+ }
+
+ @Override
+ public int getMana() {
+ return this.mana;
+ }
+
+ @Override
+ public Collection<Spell> getSpells() {
+ return spells;
+ }
+
+ @Override
+ public void addSpell(Spell spell) {
+ if (spell.getSpellType() == SpellType.WATER)
+ System.out.println("Dragon cannot learn WATER spells.");
+ else if (!spells.contains(spell))
+ {
+ spells.add(spell);
+ }
+ }
+
+ @Override
+ public void castSpell(Spell spell) {
+ if (spell.getSpellType() == SpellType.WATER)
+ System.out.println("Dragon cannot learn WATER spells.");
+ else if (!spells.contains(spell))
+ System.out.println(this.name + " does not know " + spell.name() + ".");
+ else if (spell.getManaCost() > this.mana)
+ System.out.println(this.name + " does not have enough mana.");
+ else {
+ System.out.println(this.name + " casted " + spell.name() + ".");
+ this.mana -= spell.getManaCost();
+ }
+ }
+
+ @Override
+ public void regenMana(int mana) {
+ this.mana += mana;
+ }
+}
diff --git a/graphs/java/creatureInterface/src/main/java/fr/epita/assistants/creatureInterface/Fish.java b/graphs/java/creatureInterface/src/main/java/fr/epita/assistants/creatureInterface/Fish.java
new file mode 100644
index 0000000..b109493
--- /dev/null
+++ b/graphs/java/creatureInterface/src/main/java/fr/epita/assistants/creatureInterface/Fish.java
@@ -0,0 +1,27 @@
+package fr.epita.assistants.creatureInterface;
+
+public class Fish extends Creature implements SwimmingInterface{
+ /**
+ * Constructor for the Creature class.
+ *
+ * @param name The name of the creature
+ */
+ Fish(String name) {
+ super(name);
+ }
+
+ @Override
+ public void swim() {
+ System.out.println("I'm a " + this.getClass().getSimpleName() + " and I'm swimming.");
+ }
+
+ @Override
+ public boolean getSwimmingState() {
+ return true;
+ }
+
+ @Override
+ public void emerge() {
+ throw new RuntimeException(name + " died.");
+ }
+}
diff --git a/graphs/java/creatureInterface/src/main/java/fr/epita/assistants/creatureInterface/FlyingInterface.java b/graphs/java/creatureInterface/src/main/java/fr/epita/assistants/creatureInterface/FlyingInterface.java
new file mode 100644
index 0000000..49ab154
--- /dev/null
+++ b/graphs/java/creatureInterface/src/main/java/fr/epita/assistants/creatureInterface/FlyingInterface.java
@@ -0,0 +1,12 @@
+package fr.epita.assistants.creatureInterface;
+
+/**
+ * This interface provides methods for flying.
+ * Creatures that fly implement this interface.
+ */
+public interface FlyingInterface {
+ /**
+ * Prints "I'm a {CreatureClassName} and I'm flying."
+ */
+ void fly();
+}
diff --git a/graphs/java/creatureInterface/src/main/java/fr/epita/assistants/creatureInterface/Human.java b/graphs/java/creatureInterface/src/main/java/fr/epita/assistants/creatureInterface/Human.java
new file mode 100644
index 0000000..b29b49f
--- /dev/null
+++ b/graphs/java/creatureInterface/src/main/java/fr/epita/assistants/creatureInterface/Human.java
@@ -0,0 +1,8 @@
+package fr.epita.assistants.creatureInterface;
+
+public class Human extends BaseHuman {
+ public Human(String name)
+ {
+ super(name);
+ }
+}
diff --git a/graphs/java/creatureInterface/src/main/java/fr/epita/assistants/creatureInterface/Mage.java b/graphs/java/creatureInterface/src/main/java/fr/epita/assistants/creatureInterface/Mage.java
new file mode 100644
index 0000000..dab7630
--- /dev/null
+++ b/graphs/java/creatureInterface/src/main/java/fr/epita/assistants/creatureInterface/Mage.java
@@ -0,0 +1,59 @@
+package fr.epita.assistants.creatureInterface;
+
+import java.util.ArrayList;
+import java.util.Collection;
+
+public class Mage extends BaseHuman implements MagicalInterface{
+ int mana;
+ Collection<Spell> spells = new ArrayList<Spell>();
+ public Mage(String name, int mana) {
+ super(name);
+ this.mana = mana;
+ }
+
+ @Override
+ public void greet(SpeakableInterface contact)
+ {
+ if (contact instanceof Mage)
+ System.out.println("I welcome you, " + contact.getName() + ".");
+ else
+ super.greet(contact);
+ }
+
+ @Override
+ public int getMana() {
+ return this.mana;
+ }
+
+ @Override
+ public Collection<Spell> getSpells() {
+ return spells;
+ }
+
+ @Override
+ public void addSpell(Spell spell) {
+ if (!spells.contains(spell))
+ {
+ spells.add(spell);
+ }
+ }
+
+ @Override
+ public void castSpell(Spell spell) {
+ if (!spells.contains(spell))
+ System.out.println(this.name + " does not know " + spell.name() + ".");
+ else if (spell.getManaCost() > this.mana)
+ System.out.println(this.name + " does not have enough mana.");
+ else {
+ System.out.println(this.name + " casted " + spell.name() + ".");
+ this.mana -= spell.getManaCost();
+ }
+ }
+
+ @Override
+ public void regenMana(int mana) {
+ if (inWater)
+ mana = (int)(mana * 0.9);
+ this.mana += mana;
+ }
+}
diff --git a/graphs/java/creatureInterface/src/main/java/fr/epita/assistants/creatureInterface/MagicalInterface.java b/graphs/java/creatureInterface/src/main/java/fr/epita/assistants/creatureInterface/MagicalInterface.java
new file mode 100644
index 0000000..d7530ab
--- /dev/null
+++ b/graphs/java/creatureInterface/src/main/java/fr/epita/assistants/creatureInterface/MagicalInterface.java
@@ -0,0 +1,56 @@
+package fr.epita.assistants.creatureInterface;
+
+import java.util.Collection;
+
+/**
+ * This interface provides methods for creatures that are Magical!
+ * Creatures that implement this interface have magical mana and spells.
+ */
+public interface MagicalInterface {
+ /**
+ * Get the mana of the creature.
+ *
+ * @return The mana of the creature
+ */
+ int getMana();
+
+ /**
+ * Get the spells of the creature.
+ *
+ * @return The spells of the creature
+ */
+ Collection<Spell> getSpells();
+
+ /**
+ * Add a spell to the creature.
+ * If the spell is already present, it will not be added.
+ * Be sure to check that the creature <strong>can</strong> learn the spell before adding it.
+ * Prints a message if the creature cannot learn the spell.
+ *
+ * @param spell The spell to add
+ */
+ void addSpell(Spell spell);
+
+ /**
+ * Cast a spell.
+ * If the creature knows the spell and has enough mana, the spell is cast.
+ * Otherwise, prints a message indicating the reason.
+ *
+ * @param spell The spell to cast
+ */
+ void castSpell(Spell spell);
+
+ /**
+ * Regenerate an amount of mana.
+ *
+ * @param mana The amount of mana to regenerate
+ */
+ void regenMana(int mana);
+
+ /**
+ * Do some sparkles ✨✨✨
+ */
+ default void doSomeSparkles() {
+ System.out.println("*sparkling effects*");
+ }
+}
diff --git a/graphs/java/creatureInterface/src/main/java/fr/epita/assistants/creatureInterface/Mermaid.java b/graphs/java/creatureInterface/src/main/java/fr/epita/assistants/creatureInterface/Mermaid.java
new file mode 100644
index 0000000..d086e8d
--- /dev/null
+++ b/graphs/java/creatureInterface/src/main/java/fr/epita/assistants/creatureInterface/Mermaid.java
@@ -0,0 +1,110 @@
+package fr.epita.assistants.creatureInterface;
+
+import java.util.ArrayList;
+import java.util.Collection;
+import java.util.List;
+
+public class Mermaid extends Creature implements SpeakableInterface, MagicalInterface, SwimmingInterface {
+ int mana;
+ Collection<Spell> spells = new ArrayList<Spell>();
+ boolean swimmingState;
+
+ private String make_name(BaseHuman baseHuman, Fish fish) {
+ StringBuilder name = new StringBuilder();
+ name.append(baseHuman.getName().substring(0, 1).toUpperCase()).append(baseHuman.getName().substring(1).toLowerCase());
+ name.append(fish.getName().toLowerCase());
+ return new String(name);
+ }
+
+ public Mermaid(BaseHuman baseHuman, Fish fish)
+ {
+ super("");
+ this.name = make_name(baseHuman, fish);
+ if (baseHuman instanceof Mage)
+ {
+ for (Spell s : ((Mage) baseHuman).getSpells())
+ {
+ if (s.getSpellType() == SpellType.FIRE)
+ {
+ System.out.println(this.name + " forgot the spell " + s.name() + ".");
+ }
+ else
+ {
+ addSpell(s);
+ }
+ }
+ }
+ this.swimmingState = baseHuman.getSwimmingState();
+ this.mana = 0;
+ }
+
+ @Override
+ public void hello() {
+ System.out.println("Hello, my name is " + this.name + " and I'm a Mermaid.");
+ }
+
+ @Override
+ public void greet(SpeakableInterface contact) {
+ if (contact instanceof Mermaid)
+ System.out.println("Dear " + contact.getName() + ", welcome.");
+ else
+ SpeakableInterface.super.greet(contact);
+ }
+
+ @Override
+ public int getMana() {
+ return this.mana;
+ }
+
+ @Override
+ public Collection<Spell> getSpells() {
+ return spells;
+ }
+
+ @Override
+ public void addSpell(Spell spell) {
+ if (spell.getSpellType() == SpellType.FIRE)
+ System.out.println("Mermaid cannot learn FIRE spells.");
+ else if (!spells.contains(spell)) {
+ spells.add(spell);
+ }
+ }
+
+ @Override
+ public void castSpell(Spell spell) {
+ if (spell.getSpellType() == SpellType.FIRE)
+ System.out.println("Mermaid cannot learn FIRE spells.");
+ else if (!spells.contains(spell))
+ System.out.println(this.name + " does not know " + spell.name() + ".");
+ else if (spell.getSpellType() == SpellType.WATER && swimmingState && spell.getManaCost() * 0.6 <= this.mana) {
+ this.mana -= (int) (spell.getManaCost() * 0.6);
+ System.out.println(this.name + " casted " + spell.name() + ".");
+ } else if (spell.getManaCost() > this.mana)
+ System.out.println(this.name + " does not have enough mana.");
+ else {
+ System.out.println(this.name + " casted " + spell.name() + ".");
+ this.mana -= spell.getManaCost();
+ }
+ }
+
+ @Override
+ public void regenMana(int mana) {
+ this.mana += mana;
+ }
+
+ @Override
+ public void swim() {
+ swimmingState = true;
+ System.out.println("I'm a Mermaid and I'm swimming.");
+ }
+
+ @Override
+ public boolean getSwimmingState() {
+ return swimmingState;
+ }
+
+ @Override
+ public void emerge() {
+ swimmingState = false;
+ }
+}
diff --git a/graphs/java/creatureInterface/src/main/java/fr/epita/assistants/creatureInterface/SpeakableInterface.java b/graphs/java/creatureInterface/src/main/java/fr/epita/assistants/creatureInterface/SpeakableInterface.java
new file mode 100644
index 0000000..cc10988
--- /dev/null
+++ b/graphs/java/creatureInterface/src/main/java/fr/epita/assistants/creatureInterface/SpeakableInterface.java
@@ -0,0 +1,47 @@
+package fr.epita.assistants.creatureInterface;
+
+import java.util.List;
+
+/**
+ * This interface provides communication methods for the objects of the class
+ * that implements it. Classes adopting this interface instantiate objects
+ * capable of communication.
+ */
+public interface SpeakableInterface {
+ /**
+ * Returns the name of the object that can speak
+ */
+ String getName();
+
+ /**
+ * Prints "Hello, my name is {creatureName} and I'm a {creatureClassName}."
+ */
+ void hello();
+
+ /**
+ * Greets the contact
+ * The default implementation greets the contact based on its type
+ * @param contact the creature to greet
+ */
+ default void greet(SpeakableInterface contact) {
+ if (contact instanceof Mage)
+ System.out.println("Salutations " + contact.getName() + ", keeper of Arcane secrets.");
+ else if (contact instanceof Human)
+ System.out.println("Salutations " + contact.getName() + " the human.");
+ else if (contact instanceof Dragon)
+ System.out.println("Salutations " + contact.getName() + ", keeper of Ancient treasures.");
+ else if (contact instanceof Mermaid)
+ System.out.println("Salutations " + contact.getName() + ", keeper of the Seas.");
+ }
+
+
+ /**
+ * Allows all speakers in the collection to say hello as explained in the hello() method
+ * @param speakers the list of creatures that can speak
+ */
+ static void helloAll(List<SpeakableInterface> speakers) {
+ for (SpeakableInterface speaker : speakers) {
+ speaker.hello();
+ }
+ }
+}
diff --git a/graphs/java/creatureInterface/src/main/java/fr/epita/assistants/creatureInterface/Spell.java b/graphs/java/creatureInterface/src/main/java/fr/epita/assistants/creatureInterface/Spell.java
new file mode 100644
index 0000000..87fe68a
--- /dev/null
+++ b/graphs/java/creatureInterface/src/main/java/fr/epita/assistants/creatureInterface/Spell.java
@@ -0,0 +1,48 @@
+package fr.epita.assistants.creatureInterface;
+
+/**
+ * This enum represents a spell that some creature can cast.
+ * Refer to the subject for more information about enum with behavior.
+ */
+public enum Spell {
+ FIREBALL(40, SpellType.FIRE, "Launches a sphere of fire that explodes upon impact."),
+ HEAL(30, SpellType.NEUTRAL, "Channels divine energy to restore health."),
+ MAGIC_SHIELD(15, SpellType.NEUTRAL, "Summons an arcane shield that reflects attacks."),
+ TIDAL_WAVE(30, SpellType.WATER, "Summons a huge wave of water that crashes down dealing damage.");
+
+ /**
+ * The cost of the spell in mana.
+ */
+ private final int manaCost;
+
+ /**
+ * The type of the spell.
+ */
+ private final SpellType spellType;
+
+ /**
+ * The description of the spell.
+ */
+ private final String description;
+
+ Spell(int manaCost, SpellType spellType, String description) {
+ this.manaCost = manaCost;
+ this.spellType = spellType;
+ this.description = description;
+ }
+
+ public int getManaCost() {
+ return manaCost;
+ }
+
+ public SpellType getSpellType() {
+ return spellType;
+ }
+
+ /**
+ * This method prints a description of the spell and its mana cost.
+ */
+ public void what() {
+ System.out.println(this.description + "\tUses " + getManaCost() + " mana.");
+ }
+}
diff --git a/graphs/java/creatureInterface/src/main/java/fr/epita/assistants/creatureInterface/SpellType.java b/graphs/java/creatureInterface/src/main/java/fr/epita/assistants/creatureInterface/SpellType.java
new file mode 100644
index 0000000..53cca2a
--- /dev/null
+++ b/graphs/java/creatureInterface/src/main/java/fr/epita/assistants/creatureInterface/SpellType.java
@@ -0,0 +1,11 @@
+package fr.epita.assistants.creatureInterface;
+
+/**
+ * This enumeration provides the types of spells.
+ * Do not forget that creatures have restrictions on the types of spells they can learn.
+ */
+public enum SpellType {
+ FIRE,
+ WATER,
+ NEUTRAL,
+}
diff --git a/graphs/java/creatureInterface/src/main/java/fr/epita/assistants/creatureInterface/SwimmingInterface.java b/graphs/java/creatureInterface/src/main/java/fr/epita/assistants/creatureInterface/SwimmingInterface.java
new file mode 100644
index 0000000..ede4d31
--- /dev/null
+++ b/graphs/java/creatureInterface/src/main/java/fr/epita/assistants/creatureInterface/SwimmingInterface.java
@@ -0,0 +1,25 @@
+package fr.epita.assistants.creatureInterface;
+
+/**
+ * This interface provides methods for swimming.
+ * Creatures that swim implement this interface.
+ */
+public interface SwimmingInterface {
+ /**
+ * Prints "I'm a {CreatureClassName} and I'm swimming."
+ */
+ void swim();
+
+ /**
+ * Returns true if the creature is swimming.
+ * It is up to the implementing class to determine the conditions for swimming.
+ *
+ * @return True if the creature is swimming
+ */
+ boolean getSwimmingState();
+
+ /**
+ * Emerges from the water.
+ */
+ void emerge();
+}