Commit c8d65494 authored by Alan BILLY's avatar Alan BILLY

Ajout du projet Monstre

parent 91c4a189
{
"java.project.sourcePaths": ["src"],
"java.project.outputPath": "bin",
"java.project.referencedLibraries": [
"lib/**/*.jar"
]
}
# TP monstre
Diagramme de classe :
![Alt text](uml.png)
\ No newline at end of file
@startuml Monstre
class Monstre {
- type: String
- name: String
- description: String
- santé: int
- puissance: int
- bras: int
- jambes: int
- tete: boolean
- corps: boolean
- queue: boolean
- ailes: int
+ Monstre() -> Monstre
+ getType() -> String
+ setType(String type) -> void
+ getName() -> String
+ setName(String name) -> void
}
interface MonstreBuilder {
+ reset() -> void
+ buildBras(int nombre) -> void
+ buildJambes(int nombre) -> void
+ buildTete(boolean isUnique) -> void
+ buildCorps(boolean isUnique) -> void
+ buildQueue(int nombre) -> void
+ buildAiles(int nombre) -> void
+ type(String type) -> void
+ buildNom(String nom) -> void
+ buildDescription(String description) -> void
+ buildSante(int sante) -> void
+ buildPuissance(int puissance) -> void
Monstre build() -> Monstre
}
class MonstreDirector {
- monstreBuilder: MonstreBuilder
+ MonstreDirector(MonstreBuilder builder) -> MonstreDirector
+ créerMonstre(String type) -> Monstre
}
MonstreBuilder <|... MonstreUniverselBuilder
class MonstreUniverselBuilder {
- monstre: Monstre
}
MonstreUniverselBuilder "0" --- "1" Monstre
MonstreDirector "0" --- "1" MonstreBuilder
class Main {
+ main(String[] args) -> void
}
Main "0" --- "1" MonstreDirector
Main "0" --- "1" MonstreBuilder
Main "0" --- "1" Monstre
@enduml
\ No newline at end of file
File added
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
MonstreBuilder builder = new MonstreUniverselBuilder();
MonstreDirector director = new MonstreDirector(builder);
Monstre m1;
try {
Scanner scanner = new Scanner(System.in);
System.out.println("Vous êtes un necromancien. Vous vous trouvez dans une forêt enchantée. Mais d'un seul coup, un monstre apparaît devant vous !");
Thread.sleep(2000);
System.out.println("Vous avez le choix entre plusieurs créatures pour vous protéger :");
Thread.sleep(2000);
System.out.println("1. Un dragon");
Thread.sleep(2000);
System.out.println("2. Un squelette");
Thread.sleep(2000);
System.out.println("3. Un goblin");
Thread.sleep(2000);
System.out.print("Quel monstre choisissez-vous ? ");
int input = scanner.nextInt();
switch (input) {
case 1:
m1 = director.créerMonstre("dragon");
System.out.println(m1);
break;
case 2:
m1 = director.créerMonstre("squelette");
System.out.println(m1);
break;
case 3:
m1 = director.créerMonstre("gobelin");
System.out.println(m1);
break;
default:
System.out.println("Choix invalide.");
break;
}
scanner.close();
} catch (Exception e) {
System.out.println("An error occurred: " + e.getMessage());
}
}
}
public class Monstre {
private String type;
private String name;
private String description;
private int santé;
private int puissance;
private int bras;
private int jambes;
private boolean tete;
private boolean corps;
private int queue;
private int ailes;
public Monstre() {}
public String getType() {
return type;
}
public void setType(String type) {
this.type = type;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getDescription() {
return description;
}
public void setDescription(String description) {
this.description = description;
}
public int getSanté() {
return santé;
}
public void setSanté(int santé) {
this.santé = santé;
}
public int getPuissance() {
return puissance;
}
public void setPuissance(int puissance) {
this.puissance = puissance;
}
public int getBras() {
return bras;
}
public void setBras(int bras) {
this.bras = bras;
}
public int getJambes() {
return jambes;
}
public void setJambes(int jambes) {
this.jambes = jambes;
}
public boolean isTete() {
return tete;
}
public void setTete(boolean tete) {
this.tete = tete;
}
public boolean isCorps() {
return corps;
}
public void setCorps(boolean corps) {
this.corps = corps;
}
public int getQueue() {
return queue;
}
public void setQueue(int queue) {
this.queue = queue;
}
public int getAiles() {
return ailes;
}
public void setAiles(int ailes) {
this.ailes = ailes;
}
@Override
public String toString() {
return "Le monstre est un " + type + " nommé " + name + ".\n" +
"Description : " + description + "\n" +
"Santé : " + santé + "\n" +
"Puissance : " + puissance + "\n" +
"Bras : " + bras + "\n" +
"Jambes : " + jambes + "\n" +
"Tête : " + tete + "\n" +
"Corps : " + corps + "\n" +
"Queue : " + queue + "\n" +
"Ailes : " + ailes + "\n";
}
}
\ No newline at end of file
public interface MonstreBuilder {
public void reset();
public void buildBras(int nombre);
public void buildJambes(int nombre);
public void buildTete(boolean isUnique);
public void buildCorps(boolean isUnique);
public void buildQueue(int nombre);
public void buildAiles(int nombre);
public void type(String type);
public void buildNom(String nom);
public void buildDescription(String description);
public void buildSante(int sante);
public void buildPuissance(int puissance);
Monstre build();
}
public class MonstreDirector {
private MonstreBuilder b;
public MonstreDirector(MonstreBuilder builder) {
this.b = builder;
}
public Monstre créerMonstre(String type) {
switch (type) {
case "dragon":
b.reset();
b.type(type);
b.buildCorps(true);
b.buildJambes(2);
b.buildTete(true);
b.buildQueue(1);
b.buildAiles(2);
b.buildNom("Draco");
b.buildDescription("Un monstre cracheur de feu.");
b.buildSante(100);
b.buildPuissance(100);
break;
case "squelette":
b.reset();
b.type(type);
b.buildCorps(true);
b.buildBras(1);
b.buildJambes(2);
b.buildTete(false);
b.buildNom("Bones");
b.buildDescription("Un monstre squelettique.");
b.buildSante(50);
b.buildPuissance(50);
break;
case "gobelin":
b.reset();
b.type(type);
b.buildCorps(true);
b.buildBras(2);
b.buildJambes(2);
b.buildTete(true);
b.buildNom("Gollum");
b.buildDescription("Un petit gobelin.");
b.buildSante(15);
b.buildPuissance(15);
break;
default:
throw new IllegalArgumentException("Type de monstre non valide");
}
return b.build();
}
}
public class MonstreUniverselBuilder implements MonstreBuilder {
private Monstre monstre;
@Override
public void reset() {
monstre = new Monstre();
}
@Override
public void buildBras(int nombre) {
monstre.setBras(nombre);
}
@Override
public void buildJambes(int nombre) {
monstre.setJambes(nombre);
}
@Override
public void buildTete(boolean isUnique) {
monstre.setTete(isUnique);
}
@Override
public void buildCorps(boolean isUnique) {
monstre.setCorps(isUnique);
}
@Override
public void buildQueue(int nombre) {
monstre.setQueue(nombre);
}
@Override
public void buildAiles(int nombre) {
monstre.setAiles(nombre);
}
@Override
public void type(String type) {
monstre.setType(type);
}
@Override
public void buildNom(String nom) {
monstre.setName(nom);
}
@Override
public void buildDescription(String description) {
monstre.setDescription(description);
}
@Override
public void buildSante(int sante) {
monstre.setSanté(sante);
}
@Override
public void buildPuissance(int puissance) {
monstre.setPuissance(puissance);
}
@Override
public Monstre build() {
return monstre;
}
}
\ No newline at end of file
uml.png

69.1 KB

Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment