DTO pour Personnage, Inventaire Objet
Conditions d’achèvement
PersonnageDTO
package com.example.rest.DTO;
import java.util.Set;
import java.util.stream.Collectors;
import com.example.rest.Model.Personnage.Personnage;
import java.util.Objects;
public class PersonnageDTO {
private Long id; // ID unique du personnage.
private String nom; // Nom du personnage.
// Autres champs que vous souhaitez inclure dans la réponse
private String classe; // Classe du personnage (ex: guerrier, mage, etc.)
private Integer niveau; // Niveau du personnage
private Integer pointsExperience; // Points d'expérience du personnage
private Integer pointsVie; // Points de vie du personnage
private Integer pointsMana; // Points de mana du personnage
private Integer pointForce; // Points de force du personnage
private Integer pointScore; // Score du personnage dans le jeu
private Set<InventaireDTO> inventaire; // Ensemble d'objets que le personnage possède, représenté par un ensemble de DTO d'inventaire.
private ProfilDTO profil; // Profil associé au personnage, représenté par un DTO de profil.
// Getters et Setters
public PersonnageDTO() {
}
public PersonnageDTO(Long id, String nom, String classe, Integer niveau, Integer pointsExperience, Integer pointsVie, Integer pointsMana, Integer pointForce, Integer pointScore, Set<InventaireDTO> inventaire, ProfilDTO profil) {
this.id = id;
this.nom = nom;
this.classe = classe;
this.niveau = niveau;
this.pointsExperience = pointsExperience;
this.pointsVie = pointsVie;
this.pointsMana = pointsMana;
this.pointForce = pointForce;
this.pointScore = pointScore;
this.inventaire = inventaire;
this.profil = profil;
}
// Constructeur qui accepte un objet Personnage
public PersonnageDTO(Personnage personnage) {
this.id = personnage.getId();
this.nom = personnage.getNom();
this.classe = personnage.getClasse();
this.niveau = personnage.getNiveau();
this.pointsExperience = personnage.getPointsExperience();
this.pointsVie = personnage.getPointsVie();
this.pointsMana = personnage.getPointsMana();
this.pointForce = personnage.getPointsForce();
this.pointScore = personnage.getPointsScore();
this.inventaire = personnage.getInventaire() != null ? personnage.getInventaire().stream().map(InventaireDTO::new).collect(Collectors.toSet()) : null;
this.profil = personnage.getProfil() != null ? new ProfilDTO(personnage.getProfil()) : null;
}
public Long getId() {
return this.id;
}
public void setId(Long id) {
this.id = id;
}
public String getNom() {
return this.nom;
}
public void setNom(String nom) {
this.nom = nom;
}
public String getClasse() {
return this.classe;
}
public void setClasse(String classe) {
this.classe = classe;
}
public Integer getNiveau() {
return this.niveau;
}
public void setNiveau(Integer niveau) {
this.niveau = niveau;
}
public Integer getPointsExperience() {
return this.pointsExperience;
}
public void setPointsExperience(Integer pointsExperience) {
this.pointsExperience = pointsExperience;
}
public Integer getPointsVie() {
return this.pointsVie;
}
public void setPointsVie(Integer pointsVie) {
this.pointsVie = pointsVie;
}
public Integer getPointsMana() {
return this.pointsMana;
}
public void setPointsMana(Integer pointsMana) {
this.pointsMana = pointsMana;
}
public Integer getpointForce() {
return this.pointForce;
}
public void setpointForce(Integer pointForce) {
this.pointForce = pointForce;
}
public Integer getpointScore() {
return this.pointScore;
}
public void setpointScore(Integer pointScore) {
this.pointScore = pointScore;
}
public Set<InventaireDTO> getInventaire() {
return this.inventaire;
}
public void setInventaire(Set<InventaireDTO> inventaire) {
this.inventaire = inventaire;
}
public ProfilDTO getProfil() {
return this.profil;
}
public void setProfil(ProfilDTO profil) {
this.profil = profil;
}
public PersonnageDTO id(Long id) {
setId(id);
return this;
}
public PersonnageDTO nom(String nom) {
setNom(nom);
return this;
}
public PersonnageDTO classe(String classe) {
setClasse(classe);
return this;
}
public PersonnageDTO niveau(Integer niveau) {
setNiveau(niveau);
return this;
}
public PersonnageDTO pointsExperience(Integer pointsExperience) {
setPointsExperience(pointsExperience);
return this;
}
public PersonnageDTO pointsVie(Integer pointsVie) {
setPointsVie(pointsVie);
return this;
}
public PersonnageDTO pointsMana(Integer pointsMana) {
setPointsMana(pointsMana);
return this;
}
public PersonnageDTO pointForce(Integer pointForce) {
setpointForce(pointForce);
return this;
}
public PersonnageDTO pointScore(Integer pointScore) {
setpointScore(pointScore);
return this;
}
public PersonnageDTO inventaire(Set<InventaireDTO> inventaire) {
setInventaire(inventaire);
return this;
}
public PersonnageDTO profil(ProfilDTO profil) {
setProfil(profil);
return this;
}
@Override
public boolean equals(Object o) {
if (o == this)
return true;
if (!(o instanceof PersonnageDTO)) {
return false;
}
PersonnageDTO personnageDTO = (PersonnageDTO) o;
return Objects.equals(id, personnageDTO.id) && Objects.equals(nom, personnageDTO.nom) && Objects.equals(classe, personnageDTO.classe) && Objects.equals(niveau, personnageDTO.niveau) && Objects.equals(pointsExperience, personnageDTO.pointsExperience) && Objects.equals(pointsVie, personnageDTO.pointsVie) && Objects.equals(pointsMana, personnageDTO.pointsMana) && Objects.equals(pointForce, personnageDTO.pointForce) && Objects.equals(pointScore, personnageDTO.pointScore) && Objects.equals(inventaire, personnageDTO.inventaire) && Objects.equals(profil, personnageDTO.profil);
}
@Override
public int hashCode() {
return Objects.hash(id, nom, classe, niveau, pointsExperience, pointsVie, pointsMana, pointForce, pointScore, inventaire, profil);
}
@Override
public String toString() {
return "{" +
" id='" + getId() + "'" +
", nom='" + getNom() + "'" +
", classe='" + getClasse() + "'" +
", niveau='" + getNiveau() + "'" +
", pointsExperience='" + getPointsExperience() + "'" +
", pointsVie='" + getPointsVie() + "'" +
", pointsMana='" + getPointsMana() + "'" +
", pointForce='" + getpointForce() + "'" +
", pointScore='" + getpointScore() + "'" +
", inventaire='" + getInventaire() + "'" +
", profil='" + getProfil() + "'" +
"}";
}
}
ObjetDTO
package com.example.rest.DTO;
import java.util.Set;
import java.util.stream.Collectors;
import com.example.rest.Model.Personnage.Objet;
public class ObjetDTO {
private Long id;
private String nom;
private String description;
private String type;
private Integer valeur;
private Double poids;
private String effet;
private Integer niveauRequis;
private Set<InventaireDTO> inventaire; // Supposant que vous avez également un DTO pour Inventaire
// Constructeur sans arguments
public ObjetDTO() {
}
// Constructeur avec arguments
public ObjetDTO(Long id, String nom, String description, String type, Integer valeur, Double poids, String effet, Integer niveauRequis, Set<InventaireDTO> inventaire) {
this.id = id;
this.nom = nom;
this.description = description;
this.type = type;
this.valeur = valeur;
this.poids = poids;
this.effet = effet;
this.niveauRequis = niveauRequis;
this.inventaire = inventaire;
}
public ObjetDTO(Objet objet) {
this.id = objet.getId();
this.nom = objet.getNom();
this.description = objet.getDescription();
this.type = objet.getType();
this.valeur = objet.getValeur();
this.poids = objet.getPoids();
this.effet = objet.getEffet();
this.niveauRequis = objet.getNiveauRequis();
if (objet.getInventaire() != null) {
this.inventaire = objet.getInventaire().stream()
.map(InventaireDTO::new) // Supposant que vous avez un constructeur dans InventaireDTO qui accepte un objet Inventaire
.collect(Collectors.toSet());
}
}
// Getters et Setters
public Long getId() {
return id;
}
public void setId(Long id) {
this.id = id;
}
public String getNom() {
return nom;
}
public void setNom(String nom) {
this.nom = nom;
}
public String getDescription() {
return description;
}
public void setDescription(String description) {
this.description = description;
}
public String getType() {
return type;
}
public void setType(String type) {
this.type = type;
}
public Integer getValeur() {
return valeur;
}
public void setValeur(Integer valeur) {
this.valeur = valeur;
}
public Double getPoids() {
return poids;
}
public void setPoids(Double poids) {
this.poids = poids;
}
public String getEffet() {
return effet;
}
public void setEffet(String effet) {
this.effet = effet;
}
public Integer getNiveauRequis() {
return niveauRequis;
}
public void setNiveauRequis(Integer niveauRequis) {
this.niveauRequis = niveauRequis;
}
public Set<InventaireDTO> getInventaire() {
return inventaire;
}
public void setInventaire(Set<InventaireDTO> inventaire) {
this.inventaire = inventaire;
}
}
InventaireDTO
package com.example.rest.DTO;
import com.example.rest.Model.Personnage.Inventaire;
public class InventaireDTO {
private Long id;
private PersonnageDTO personnage;
private ObjetDTO objet;
private Integer quantite;
// Constructeur sans arguments
public InventaireDTO() {
}
// Constructeur qui accepte un objet Inventaire
public InventaireDTO(Inventaire inventaire) {
this.id = inventaire.getId();
this.personnage = new PersonnageDTO(inventaire.getPersonnage()); // Supposant que vous avez un constructeur dans PersonnageDTO qui accepte un objet Personnage
this.objet = new ObjetDTO(inventaire.getObjet()); // Vous avez déjà ce constructeur dans ObjetDTO
this.quantite = inventaire.getQuantite();
}
// Getters et Setters
public Long getId() {
return id;
}
public void setId(Long id) {
this.id = id;
}
public PersonnageDTO getPersonnage() {
return personnage;
}
public void setPersonnage(PersonnageDTO personnage) {
this.personnage = personnage;
}
public ObjetDTO getObjet() {
return objet;
}
public void setObjet(ObjetDTO objet) {
this.objet = objet;
}
public Integer getQuantite() {
return quantite;
}
public void setQuantite(Integer quantite) {
this.quantite = quantite;
}
@Override
public String toString() {
return "InventaireDTO{" +
"id=" + id +
", personnage=" + personnage +
", objet=" + objet +
", quantite=" + quantite +
'}';
}
}
ProfilDTO
package com.example.rest.DTO;
import java.util.HashSet;
import java.util.Set;
import com.example.rest.Model.Profil;
public class ProfilDTO {
// L'ID unique du profil
private Integer id;
// Le nom du profil
private String name;
// Un ensemble de jeux associés au profil.
// HashSet est utilisé pour éviter les doublons et pour une recherche plus rapide.
private Set<GameDTO> games = new HashSet<>();
// Un ensemble de personnages associés au profil
private Set<PersonnageDTO> personnages;
// Constructeur sans arguments
public ProfilDTO() {
}
// Constructeur avec arguments
public ProfilDTO(Integer id, String name, Set<GameDTO> games, Set<PersonnageDTO> personnages) {
this.id = id;
this.name = name;
this.games = games;
this.personnages = personnages;
}
public ProfilDTO(Profil profil) {
if(profil != null) {
this.id = profil.getId();
this.name = profil.getName();
}
}
// Getters et Setters
public Integer getId() {
return id;
}
public void setId(Integer id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public Set<GameDTO> getGames() {
return games;
}
public void setGames(Set<GameDTO> games) {
this.games = games;
}
public Set<PersonnageDTO> getPersonnages() {
return personnages;
}
public void setPersonnages(Set<PersonnageDTO> personnages) {
this.personnages = personnages;
}
@Override
public String toString() {
return "ProfilDTO{" +
"id=" + id +
", name='" + name + '\'' +
", games=" + games +
", personnages=" + personnages +
'}';
}
}
Pourquoi utiliser un HashSet pour games ?
Un HashSet est une implémentation de l'interface Set qui utilise une table de hachage pour stocker les éléments. Voici quelques raisons pour lesquelles un HashSet est utilisé pour games :
-
Unicité :
HashSetne permet pas les doublons. Chaque élément dans unHashSetdoit être unique. Si vous essayez d'ajouter un élément déjà présent dans leHashSet, l'opération échoue silencieusement, et l'élément original n'est pas remplacé par le nouveau.
-
Performance :
HashSetoffre une performance constante pour les opérations de base commeadd,remove, etcontains, ce qui le rend approprié pour les grandes collections.
-
Nullité :
HashSetpermet un élémentnull.
-
Pas d'ordre garanti :
- Les éléments stockés dans un
HashSetn'ont pas d'ordre spécifique. Si l'ordre des éléments est important, une autre implémentation deSetcommeLinkedHashSet(qui maintient l'ordre d'insertion) ouTreeSet(qui ordonne les éléments selon leur ordre naturel ou par un comparateur) serait plus appropriée.
- Les éléments stockés dans un
Dans le contexte de ProfilDTO, utiliser un HashSet pour games permet de s'assurer que chaque GameDTO associé au profil est unique et permet des opérations efficaces sur les jeux, comme vérifier si un jeu spécifique est associé au profil.
AccountDTO
package com.example.rest.DTO;
public class AccountDTO {
private Long id;
private String email;
private String password;
private ProfilDTO profil;
// Constructeur sans arguments
public AccountDTO() {
}
// Constructeur avec arguments
public AccountDTO(Long id, String email, String password, ProfilDTO profil) {
this.id = id;
this.email = email;
this.password = password;
this.profil = profil;
}
// Getters et Setters
public Long getId() {
return id;
}
public void setId(Long id) {
this.id = id;
}
public String getEmail() {
return email;
}
public void setEmail(String email) {
this.email = email;
}
public String getPassword() {
return password;
}
public void setPassword(String password) {
this.password = password;
}
public ProfilDTO getProfil() {
return profil;
}
public void setProfil(ProfilDTO profil) {
this.profil = profil;
}
@Override
public String toString() {
return "AccountDTO{" +
"id=" + id +
", email='" + email + '\'' +
", password='" + password + '\'' +
", profil=" + profil +
'}';
}
}
GameDTO
package com.example.rest.DTO;
import java.util.HashSet;
import java.util.Set;
public class GameDTO {
private Integer id;
private String name;
private Set<ProfilDTO> profils = new HashSet<>();
// Constructeur sans arguments
public GameDTO() {
}
// Constructeur avec arguments
public GameDTO(Integer id, String name, Set<ProfilDTO> profils) {
this.id = id;
this.name = name;
this.profils = profils;
}
// Getters et Setters
public Integer getId() {
return id;
}
public void setId(Integer id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public Set<ProfilDTO> getProfils() {
return profils;
}
public void setProfils(Set<ProfilDTO> profils) {
this.profils = profils;
}
@Override
public String toString() {
return "GameDTO{" +
"id=" + id +
", name='" + name + '\'' +
", profils=" + profils +
'}';
}
}
LoginDTO
Utiliser dans AccountController.java
Il faut finaliser l'implémentation !
package com.example.rest.DTO;
public class LoginDTO {
private String username;
private String password;
public LoginDTO() {}
public LoginDTO(String username, String password) {
this.username = username;
this.password = password;
}
public String getUsername() {
return username;
}
public void setUsername(String username) {
this.username = username;
}
public String getPassword() {
return password;
}
public void setPassword(String password) {
this.password = password;
}
}
Modifié le: mercredi 27 septembre 2023, 05:59