mirror of
https://github.com/kmitresse/Cards-Rush.git
synced 2026-05-14 09:05:23 +00:00
Draft - ProjetDevWeb : tentative de création des POJO avec JPA
This commit is contained in:
@@ -0,0 +1,28 @@
|
||||
package project;
|
||||
|
||||
import jakarta.persistence.EntityManager;
|
||||
import jakarta.persistence.EntityManagerFactory;
|
||||
import jakarta.persistence.Persistence;
|
||||
|
||||
public final class EntityManagerProvider {
|
||||
|
||||
private static EntityManager instance;
|
||||
private static EntityManagerFactory factory;
|
||||
private final static String PERSISTANCE_UNIT_NAME = "default";
|
||||
|
||||
public static EntityManager getInstance() {
|
||||
if (factory == null) {
|
||||
factory = Persistence.createEntityManagerFactory(PERSISTANCE_UNIT_NAME);
|
||||
}
|
||||
|
||||
if (instance == null) {
|
||||
instance = factory.createEntityManager();
|
||||
}
|
||||
return instance;
|
||||
}
|
||||
|
||||
public static void close() {
|
||||
if (instance.isOpen()) instance.close();
|
||||
if (factory.isOpen()) factory.close();
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,51 @@
|
||||
package project;
|
||||
|
||||
import java.util.Calendar;
|
||||
import project.dao.DAO;
|
||||
import project.dao.DAOException;
|
||||
import project.dao.jpa.Game_JPA_DAO_Factory;
|
||||
import project.pojo.Game;
|
||||
import project.pojo.Player;
|
||||
import project.pojo.User;
|
||||
|
||||
public class Main {
|
||||
|
||||
public static void main(String[] args) {
|
||||
try {
|
||||
Game_JPA_DAO_Factory jpaDaoFactory = new Game_JPA_DAO_Factory();
|
||||
DAO<User> daoJpaUser = jpaDaoFactory.getDAOUser();
|
||||
DAO<Game> daoJpaGame = jpaDaoFactory.getDAOGame();
|
||||
DAO<Player> daoJpaPlayer = jpaDaoFactory.getDAOPlayer();
|
||||
|
||||
// Contenu de la BD au début
|
||||
User[] users = daoJpaUser.findAll();
|
||||
for (User u : users) {
|
||||
System.out.println(u.toString());
|
||||
}
|
||||
System.out.println();
|
||||
|
||||
// Ajout d'un joueur :
|
||||
Calendar calendarUser1 = Calendar.getInstance();
|
||||
calendarUser1.set(1996,Calendar.FEBRUARY,20);
|
||||
|
||||
User user1 = new User("Kevin", "Mitresse", calendarUser1.getTime(), User.Sexe.HOMME);
|
||||
|
||||
Calendar calendarUser2 = Calendar.getInstance();
|
||||
calendarUser2.set(2002,Calendar.JUNE,28);
|
||||
|
||||
User user2 = new User("Lucàs", "Vabre", calendarUser1.getTime(), User.Sexe.HOMME);
|
||||
|
||||
users = daoJpaUser.findAll();
|
||||
for (User u : users) {
|
||||
System.out.println(u.toString());
|
||||
}
|
||||
System.out.println();
|
||||
|
||||
EntityManagerProvider.close();
|
||||
} catch (DAOException e) {
|
||||
e.printStackTrace();
|
||||
} catch (Exception e) {
|
||||
System.out.println("Error : : : : ");
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,20 @@
|
||||
/*
|
||||
* AbstractDAOFactory.java, 09/02/2024
|
||||
* UPPA M1 TI 2023-2024
|
||||
* Pas de copyright, aucun droits
|
||||
*/
|
||||
|
||||
package project.dao;
|
||||
|
||||
import project.dao.jpa.Game_JPA_DAO_Factory;
|
||||
|
||||
/**
|
||||
* Factory renvoyant une factory de DAO en fonction du support de persistance choisi
|
||||
*/
|
||||
public class AbstractDAOFactory {
|
||||
|
||||
public static GameDAOFactory getDAOFactory(PersistenceKind type) {
|
||||
if (type.equals(PersistenceKind.JPA)) return new Game_JPA_DAO_Factory();
|
||||
return null;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,55 @@
|
||||
/*
|
||||
* DAO.java, 21/02/2024
|
||||
* UPPA M1 TI 2023-2024
|
||||
* Pas de copyright, aucun droits
|
||||
*/
|
||||
|
||||
package project.dao;
|
||||
|
||||
/**
|
||||
* DAO abstrait et générique pour tout type de données
|
||||
*
|
||||
* @param <D> la classe paramétrant le DAO
|
||||
*/
|
||||
public abstract class DAO<D> {
|
||||
|
||||
public void DAO() throws DAOException {
|
||||
|
||||
}
|
||||
|
||||
/**
|
||||
* Retourne à partir du support de persistance un objet en fonction de son identifiant
|
||||
*
|
||||
* @param id identifiant de l'objet
|
||||
* @return l'instance de l'objet
|
||||
* @throws DAOException en cas de problème
|
||||
*/
|
||||
public abstract D findById(int id) throws DAOException;
|
||||
|
||||
public abstract D[] findAll() throws DAOException;
|
||||
|
||||
/**
|
||||
* Rend persistant un objet qui n'avait pas encore de réprésentation sur le support de persistance
|
||||
*
|
||||
* @param data l'objet à rendre persistant
|
||||
* @throws DAOException en cas de problème
|
||||
*/
|
||||
public abstract void create(D data) throws DAOException;
|
||||
|
||||
/**
|
||||
* Met à jour le contenu correspondant à l'objet sur le support persistant (l'objet
|
||||
* avait déjà une représentation sur le support persistant)
|
||||
*
|
||||
* @param data l'objet modifié dont le contenu est à mettre à jour
|
||||
* @throws DAOException en cas de problème
|
||||
*/
|
||||
public abstract void update(D data) throws DAOException;
|
||||
|
||||
/**
|
||||
* Efface du support persistant le contenu équivalent à l'objet
|
||||
*
|
||||
* @param data l'objet à supprimer
|
||||
* @throws DAOException en cas de problème
|
||||
*/
|
||||
public abstract void delete(D data) throws DAOException;
|
||||
}
|
||||
@@ -0,0 +1,21 @@
|
||||
/*
|
||||
* DAOException.java, 09/02/2024
|
||||
* UPPA M1 TI 2023-2024
|
||||
* Pas de copyright, aucun droits
|
||||
*/
|
||||
|
||||
package project.dao;
|
||||
|
||||
/**
|
||||
* Exception spécifique aux problèmes d'accès aux données via un DAO
|
||||
*/
|
||||
public class DAOException extends Exception {
|
||||
|
||||
public DAOException() {
|
||||
super();
|
||||
}
|
||||
|
||||
public DAOException(String message) {
|
||||
super(message);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,34 @@
|
||||
/*
|
||||
* SportsDAOFactory.java, 09/02/2024
|
||||
* UPPA M1 TI 2023-2024
|
||||
* Pas de copyright, aucun droits
|
||||
*/
|
||||
|
||||
package project.dao;
|
||||
|
||||
import project.pojo.*;
|
||||
|
||||
/**
|
||||
* Fabrique abstraite de DAO pour le schéma sports
|
||||
*/
|
||||
public abstract class GameDAOFactory {
|
||||
|
||||
/**
|
||||
* @return le DAO pour la classe/table User
|
||||
* @throws DAOException en cas de problème
|
||||
*/
|
||||
public abstract DAO<User> getDAOUser() throws DAOException;
|
||||
|
||||
/**
|
||||
* @return le DAO pour la classe/table Game
|
||||
* @throws DAOException en cas de problème
|
||||
*/
|
||||
public abstract DAO<Game> getDAOGame() throws DAOException;
|
||||
|
||||
/**
|
||||
* @return le DAO pour la classe/table Player
|
||||
* @throws DAOException en cas de problème
|
||||
*/
|
||||
public abstract DAO<Player> getDAOPlayer() throws DAOException;
|
||||
|
||||
}
|
||||
@@ -0,0 +1,14 @@
|
||||
/*
|
||||
* PersistenceKind.java, 09/02/2024
|
||||
* UPPA M1 TI 2023-2024
|
||||
* Pas de copyright, aucun droits
|
||||
*/
|
||||
|
||||
package project.dao;
|
||||
|
||||
/**
|
||||
* Type de support de persistance pour les données
|
||||
*/
|
||||
public enum PersistenceKind {
|
||||
JDBC, JPA, XML
|
||||
}
|
||||
@@ -0,0 +1,52 @@
|
||||
package project.dao.jpa;
|
||||
|
||||
import jakarta.persistence.EntityManager;
|
||||
import jakarta.persistence.TypedQuery;
|
||||
import java.math.BigDecimal;
|
||||
import java.util.List;
|
||||
import project.EntityManagerProvider;
|
||||
import project.dao.DAO;
|
||||
import project.dao.DAOException;
|
||||
import project.pojo.Game;
|
||||
|
||||
public class DAO_JPA_Game extends DAO<Game> {
|
||||
|
||||
private final EntityManager entityManager;
|
||||
|
||||
public DAO_JPA_Game() throws DAOException {
|
||||
this.entityManager = EntityManagerProvider.getInstance();
|
||||
}
|
||||
|
||||
@Override
|
||||
public Game findById(int id) throws DAOException {
|
||||
Game result = entityManager.find(Game.class, new BigDecimal(id));
|
||||
entityManager.flush();
|
||||
return result;
|
||||
}
|
||||
|
||||
@Override
|
||||
public Game[] findAll() throws DAOException {
|
||||
TypedQuery<Game> query = entityManager.createQuery("SELECT g FROM Game g", Game.class);
|
||||
List<Game> results = query.getResultList();
|
||||
return results.toArray(new Game[0]);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void create(Game data) throws DAOException {
|
||||
update(data);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void update(Game data) throws DAOException {
|
||||
entityManager.getTransaction().begin();
|
||||
entityManager.merge(data);
|
||||
entityManager.getTransaction().commit();
|
||||
}
|
||||
|
||||
@Override
|
||||
public void delete(Game data) throws DAOException {
|
||||
entityManager.getTransaction().begin();
|
||||
entityManager.remove(data);
|
||||
entityManager.getTransaction().commit();
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,52 @@
|
||||
package project.dao.jpa;
|
||||
|
||||
import jakarta.persistence.EntityManager;
|
||||
import jakarta.persistence.TypedQuery;
|
||||
import java.math.BigDecimal;
|
||||
import java.util.List;
|
||||
import project.EntityManagerProvider;
|
||||
import project.dao.DAO;
|
||||
import project.dao.DAOException;
|
||||
import project.pojo.Player;
|
||||
|
||||
public class DAO_JPA_Player extends DAO<Player> {
|
||||
|
||||
private final EntityManager entityManager;
|
||||
|
||||
public DAO_JPA_Player() throws DAOException {
|
||||
this.entityManager = EntityManagerProvider.getInstance();
|
||||
}
|
||||
|
||||
@Override
|
||||
public Player findById(int id) throws DAOException {
|
||||
Player result = entityManager.find(Player.class, new BigDecimal(id));
|
||||
entityManager.flush();
|
||||
return result;
|
||||
}
|
||||
|
||||
@Override
|
||||
public Player[] findAll() throws DAOException {
|
||||
TypedQuery<Player> query = entityManager.createQuery("SELECT p FROM Player p", Player.class);
|
||||
List<Player> results = query.getResultList();
|
||||
return results.toArray(new Player[0]);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void create(Player data) throws DAOException {
|
||||
update(data);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void update(Player data) throws DAOException {
|
||||
entityManager.getTransaction().begin();
|
||||
entityManager.merge(data);
|
||||
entityManager.getTransaction().commit();
|
||||
}
|
||||
|
||||
@Override
|
||||
public void delete(Player data) throws DAOException {
|
||||
entityManager.getTransaction().begin();
|
||||
entityManager.remove(data);
|
||||
entityManager.getTransaction().commit();
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,52 @@
|
||||
package project.dao.jpa;
|
||||
|
||||
import jakarta.persistence.EntityManager;
|
||||
import jakarta.persistence.TypedQuery;
|
||||
import java.math.BigDecimal;
|
||||
import java.util.List;
|
||||
import project.dao.DAO;
|
||||
import project.dao.DAOException;
|
||||
import project.EntityManagerProvider;
|
||||
import project.pojo.User;
|
||||
|
||||
public class DAO_JPA_User extends DAO<User> {
|
||||
|
||||
private final EntityManager entityManager;
|
||||
|
||||
public DAO_JPA_User() throws DAOException {
|
||||
this.entityManager = EntityManagerProvider.getInstance();
|
||||
}
|
||||
|
||||
@Override
|
||||
public User findById(int id) throws DAOException {
|
||||
User result = entityManager.find(User.class, new BigDecimal(id));
|
||||
entityManager.flush();
|
||||
return result;
|
||||
}
|
||||
|
||||
@Override
|
||||
public User[] findAll() throws DAOException {
|
||||
TypedQuery<User> query = entityManager.createQuery("SELECT u FROM User u", User.class);
|
||||
List<User> results = query.getResultList();
|
||||
return results.toArray(new User[0]);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void create(User data) throws DAOException {
|
||||
update(data);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void update(User data) throws DAOException {
|
||||
entityManager.getTransaction().begin();
|
||||
entityManager.merge(data);
|
||||
entityManager.getTransaction().commit();
|
||||
}
|
||||
|
||||
@Override
|
||||
public void delete(User data) throws DAOException {
|
||||
entityManager.getTransaction().begin();
|
||||
entityManager.remove(data);
|
||||
entityManager.getTransaction().commit();
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,41 @@
|
||||
/*
|
||||
* Sports_JDBC_DAOFactory.java, 09/02/2024
|
||||
* UPPA M1 TI 2023-2024
|
||||
* Pas de copyright, aucun droits
|
||||
*/
|
||||
|
||||
package project.dao.jpa;
|
||||
|
||||
import project.dao.DAO;
|
||||
import project.pojo.*;
|
||||
import project.dao.DAOException;
|
||||
import project.dao.GameDAOFactory;
|
||||
|
||||
/**
|
||||
* Fabrique concrète de DAO pour le schéma relationnel sports avec une implémentation en JDBC.
|
||||
*/
|
||||
public class Game_JPA_DAO_Factory extends GameDAOFactory {
|
||||
|
||||
private DAO_JPA_User daoUser = null;
|
||||
|
||||
private DAO_JPA_Game daoGame = null;
|
||||
|
||||
private DAO_JPA_Player daoPlayer = null;
|
||||
|
||||
@Override
|
||||
public DAO<User> getDAOUser() throws DAOException {
|
||||
if (daoUser == null) daoUser = new DAO_JPA_User();
|
||||
return daoUser;
|
||||
}
|
||||
|
||||
@Override
|
||||
public DAO<Game> getDAOGame() throws DAOException {
|
||||
if (daoGame == null) daoGame = new DAO_JPA_Game();
|
||||
return daoGame;
|
||||
}
|
||||
@Override
|
||||
public DAO<Player> getDAOPlayer() throws DAOException {
|
||||
if (daoPlayer == null) daoPlayer = new DAO_JPA_Player();
|
||||
return daoPlayer;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,24 @@
|
||||
package project.pojo;
|
||||
|
||||
public class Card {
|
||||
|
||||
public enum Color{coeur, carreau, pique, trèfle}
|
||||
|
||||
public enum Value{un, deux, trois, quatre, cinq, six, sept, huit, neuf, dix, valet, dame, roi}
|
||||
private Color color;
|
||||
|
||||
private Value value;
|
||||
|
||||
public Card(Color color, Value value) {
|
||||
this.color = color;
|
||||
this.value = value;
|
||||
}
|
||||
|
||||
public Color getColor() {
|
||||
return color;
|
||||
}
|
||||
|
||||
public Value getValue() {
|
||||
return value;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,31 @@
|
||||
package project.pojo;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.Collections;
|
||||
import java.util.HashSet;
|
||||
import java.util.List;
|
||||
import java.util.Set;
|
||||
|
||||
public class Deck {
|
||||
private Set<Card> cards;
|
||||
|
||||
public Deck(){
|
||||
cards = new HashSet<>();
|
||||
initializeDeck();
|
||||
shuffleDeck();
|
||||
}
|
||||
|
||||
private void initializeDeck() {
|
||||
for (Card.Color color : Card.Color.values()){
|
||||
for (Card.Value value : Card.Value.values()) {
|
||||
cards.add(new Card(color, value));
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private void shuffleDeck() {
|
||||
List<Card> cardList = new ArrayList<>(cards);
|
||||
Collections.shuffle(cardList);
|
||||
cards = new HashSet<>(cardList);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,96 @@
|
||||
package project.pojo;
|
||||
|
||||
import jakarta.persistence.CascadeType;
|
||||
import jakarta.persistence.Column;
|
||||
import jakarta.persistence.Entity;
|
||||
import jakarta.persistence.FetchType;
|
||||
import jakarta.persistence.GeneratedValue;
|
||||
import jakarta.persistence.GenerationType;
|
||||
import jakarta.persistence.Id;
|
||||
import jakarta.persistence.OneToMany;
|
||||
import jakarta.persistence.Table;
|
||||
import jakarta.persistence.Temporal;
|
||||
import jakarta.persistence.TemporalType;
|
||||
import java.io.Serializable;
|
||||
import java.math.BigDecimal;
|
||||
import java.sql.Timestamp;
|
||||
import java.util.Date;
|
||||
import java.util.Objects;
|
||||
import java.util.Set;
|
||||
|
||||
@Entity
|
||||
@Table(name = "game")
|
||||
public class Game implements Serializable {
|
||||
|
||||
@Id
|
||||
@Column(name = "id_game")
|
||||
@GeneratedValue(strategy = GenerationType.IDENTITY)
|
||||
private BigDecimal id;
|
||||
|
||||
@Temporal(TemporalType.DATE)
|
||||
@Column(name="date")
|
||||
private Date date;
|
||||
|
||||
@Temporal(TemporalType.TIMESTAMP)
|
||||
@Column(name="starting_time")
|
||||
private Timestamp startTime;
|
||||
|
||||
@OneToMany(mappedBy = "game", cascade = CascadeType.ALL, fetch = FetchType.LAZY)
|
||||
private Set<Player> players;
|
||||
|
||||
public Game() {
|
||||
|
||||
}
|
||||
|
||||
public Game(Date date, Timestamp startTime) {
|
||||
this.date = date;
|
||||
this.startTime = startTime;
|
||||
}
|
||||
|
||||
@Override
|
||||
public int hashCode() {
|
||||
return Objects.hash(id, date);
|
||||
}
|
||||
public BigDecimal getId() {
|
||||
return id;
|
||||
}
|
||||
|
||||
public Date getDate() {
|
||||
return date;
|
||||
}
|
||||
|
||||
public void setDate(Date date) {
|
||||
this.date = date;
|
||||
}
|
||||
|
||||
public Timestamp getStartTime() {
|
||||
return startTime;
|
||||
}
|
||||
|
||||
public void setStartTime(Timestamp startTime) {
|
||||
this.startTime = startTime;
|
||||
}
|
||||
|
||||
public Set<Player> getPlayers() {
|
||||
return players;
|
||||
}
|
||||
|
||||
public void setPlayers(Set<Player> players) {
|
||||
this.players = players;
|
||||
}
|
||||
|
||||
public void addPlayers(Player player) {
|
||||
this.players.add(player);
|
||||
}
|
||||
|
||||
|
||||
|
||||
@Override
|
||||
public String toString() {
|
||||
return "Game{" +
|
||||
"id=" + id +
|
||||
", date=" + date +
|
||||
", players=" + players +
|
||||
'}';
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,157 @@
|
||||
package project.pojo;
|
||||
|
||||
import jakarta.persistence.Column;
|
||||
import jakarta.persistence.Entity;
|
||||
import jakarta.persistence.GeneratedValue;
|
||||
import jakarta.persistence.GenerationType;
|
||||
import jakarta.persistence.Id;
|
||||
import jakarta.persistence.JoinColumn;
|
||||
import jakarta.persistence.ManyToOne;
|
||||
import jakarta.persistence.Table;
|
||||
import java.io.Serializable;
|
||||
import java.math.BigDecimal;
|
||||
import java.util.Objects;
|
||||
|
||||
@Entity
|
||||
@Table(name = "player")
|
||||
public class Player implements Serializable {
|
||||
|
||||
@Id
|
||||
@Column(name = "id_player")
|
||||
@GeneratedValue(strategy = GenerationType.IDENTITY)
|
||||
private BigDecimal id;
|
||||
|
||||
@ManyToOne
|
||||
@JoinColumn(name = "id_game", nullable = false)
|
||||
private Game game;
|
||||
|
||||
@ManyToOne
|
||||
@JoinColumn(name = "id_user", nullable = false)
|
||||
private User user;
|
||||
|
||||
@Column(name = "score")
|
||||
private int score;
|
||||
|
||||
@Column(name = "winner")
|
||||
private boolean winner;
|
||||
|
||||
@Column(name = "nb_clicks")
|
||||
private int nbClicks;
|
||||
|
||||
@Column(name = "nb_right_clicks")
|
||||
private int nbRightClicks;
|
||||
|
||||
@Column(name = "nb_rapid_clicks")
|
||||
private int nbRapidClick;
|
||||
|
||||
public Player() {
|
||||
|
||||
}
|
||||
|
||||
public Player(Game game, User user) {
|
||||
this.game = game;
|
||||
this.user = user;
|
||||
this.score = 0;
|
||||
this.winner = false;
|
||||
this.nbClicks = 0;
|
||||
this.nbRightClicks = 0;
|
||||
this.nbRapidClick = 0;
|
||||
}
|
||||
|
||||
public Player(Game game, User user, int score, boolean winner, int nbClicks, int nbRightClicks, int nbRapidClick) {
|
||||
this.game = game;
|
||||
this.user = user;
|
||||
this.score = score;
|
||||
this.winner = winner;
|
||||
this.nbClicks = nbClicks;
|
||||
this.nbRightClicks = nbRightClicks;
|
||||
this.nbRapidClick = nbRapidClick;
|
||||
}
|
||||
|
||||
@Override
|
||||
public int hashCode() {
|
||||
return Objects.hash(game, user, score, winner, nbClicks, nbRightClicks, nbRapidClick);
|
||||
}
|
||||
|
||||
public Game getGame() {
|
||||
return game;
|
||||
}
|
||||
|
||||
public void setGame(Game game) {
|
||||
this.game = game;
|
||||
}
|
||||
|
||||
public User getUser() {
|
||||
return user;
|
||||
}
|
||||
|
||||
public void setUser(User user) {
|
||||
this.user = user;
|
||||
}
|
||||
|
||||
public int getScore() {
|
||||
return score;
|
||||
}
|
||||
|
||||
public void setScore(int score) {
|
||||
this.score = score;
|
||||
}
|
||||
|
||||
public void updateScore(int points) { this.score += points; }
|
||||
|
||||
public boolean isWinner() {
|
||||
return winner;
|
||||
}
|
||||
|
||||
public void setWinner() {
|
||||
this.winner = true;
|
||||
}
|
||||
|
||||
public int getNbClicks() {
|
||||
return nbClicks;
|
||||
}
|
||||
|
||||
public void setNbClicks(int nbClicks) {
|
||||
this.nbClicks = nbClicks;
|
||||
}
|
||||
|
||||
public void addClick() {
|
||||
this.nbClicks++;
|
||||
}
|
||||
|
||||
public int getNbRightClicks() {
|
||||
return nbRightClicks;
|
||||
}
|
||||
|
||||
public void setNbRightClicks(int nbRightClicks) {
|
||||
this.nbRightClicks = nbRightClicks;
|
||||
}
|
||||
|
||||
public void addRightClick() {
|
||||
this.nbRapidClick++;
|
||||
}
|
||||
public int getNbRapidClick() {
|
||||
return nbRapidClick;
|
||||
}
|
||||
|
||||
public void setNbRapidClick(int nbRapidClick) {
|
||||
this.nbRapidClick = nbRapidClick;
|
||||
}
|
||||
|
||||
public void addRapidClick() {
|
||||
this.nbRapidClick++;
|
||||
}
|
||||
|
||||
@Override
|
||||
public String toString() {
|
||||
return "Player{" +
|
||||
"game=" + game +
|
||||
", user=" + user +
|
||||
", score=" + score +
|
||||
", winner=" + winner +
|
||||
", nbClicks=" + nbClicks +
|
||||
", nbRightClicks=" + nbRightClicks +
|
||||
", nbRapidClick=" + nbRapidClick +
|
||||
'}';
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,144 @@
|
||||
package project.pojo;
|
||||
|
||||
import jakarta.persistence.CascadeType;
|
||||
import jakarta.persistence.Column;
|
||||
import jakarta.persistence.Entity;
|
||||
import jakarta.persistence.EnumType;
|
||||
import jakarta.persistence.Enumerated;
|
||||
import jakarta.persistence.FetchType;
|
||||
import jakarta.persistence.GeneratedValue;
|
||||
import jakarta.persistence.GenerationType;
|
||||
import jakarta.persistence.Id;
|
||||
import jakarta.persistence.OneToMany;
|
||||
import jakarta.persistence.Table;
|
||||
import jakarta.persistence.Temporal;
|
||||
import jakarta.persistence.TemporalType;
|
||||
import java.io.Serializable;
|
||||
import java.math.BigDecimal;
|
||||
import java.util.Date;
|
||||
import java.util.Objects;
|
||||
import java.util.Set;
|
||||
import java.security.MessageDigest;
|
||||
import java.security.NoSuchAlgorithmException;
|
||||
@Entity
|
||||
@Table(name = "user")
|
||||
public class User implements Serializable {
|
||||
|
||||
public enum Sexe {FEMME, HOMME, NONBINAIRE};
|
||||
|
||||
@Id
|
||||
@Column(name = "id_user")
|
||||
@GeneratedValue(strategy = GenerationType.IDENTITY)
|
||||
private BigDecimal id;
|
||||
|
||||
@Column(name = "username")
|
||||
private String username;
|
||||
|
||||
@Column(name = "password")
|
||||
private String password;
|
||||
|
||||
@Temporal(TemporalType.DATE)
|
||||
@Column(name = "birth_date")
|
||||
private Date bithDate;
|
||||
|
||||
@Column(name = "sexe")
|
||||
@Enumerated( EnumType.STRING)
|
||||
private Sexe sexe;
|
||||
@OneToMany(mappedBy = "user", cascade = CascadeType.ALL, fetch = FetchType.LAZY)
|
||||
private Set<Player> playedGame;
|
||||
public User() {}
|
||||
public User(String username, String password, Date birthDate, Sexe sexe) {
|
||||
this.username = username;
|
||||
String hashedPassword = hashPassword(password);
|
||||
this.password = hashedPassword;
|
||||
this.bithDate = birthDate;
|
||||
this.sexe = sexe;
|
||||
|
||||
}
|
||||
|
||||
public User(BigDecimal id, String username, String password) {
|
||||
this.id = id;
|
||||
this.username = username;
|
||||
this.password = password;
|
||||
}
|
||||
|
||||
@Override
|
||||
public int hashCode() {
|
||||
return Objects.hash(id, username, password, bithDate, sexe);
|
||||
}
|
||||
public BigDecimal getId(){
|
||||
return id;
|
||||
}
|
||||
public String getUsername() {
|
||||
return username;
|
||||
}
|
||||
|
||||
public void setUsername(String username) {
|
||||
this.username = username;
|
||||
}
|
||||
|
||||
public String getPassword() {
|
||||
return password;
|
||||
}
|
||||
|
||||
public void setPassword(String password) {
|
||||
String hashedPassword = hashPassword(password);
|
||||
this.password = hashedPassword;
|
||||
}
|
||||
|
||||
public Date getBithDate() {
|
||||
return bithDate;
|
||||
}
|
||||
|
||||
public void setBithDate(Date bithDate) {
|
||||
this.bithDate = bithDate;
|
||||
}
|
||||
|
||||
public Sexe getSexe() {
|
||||
return sexe;
|
||||
}
|
||||
|
||||
public void setSexe(Sexe sexe) {
|
||||
this.sexe = sexe;
|
||||
}
|
||||
|
||||
public int getAge(){
|
||||
//TODO: Implement this function
|
||||
return 1;
|
||||
}
|
||||
|
||||
private String hashPassword(String password) {
|
||||
try {
|
||||
MessageDigest digest = MessageDigest.getInstance("SHA-256");
|
||||
|
||||
byte[] encodedhash = digest.digest(password.getBytes());
|
||||
|
||||
StringBuilder hexString = new StringBuilder();
|
||||
for (byte b : encodedhash) {
|
||||
String hex = Integer.toHexString(0xff & b);
|
||||
if (hex.length() == 1) hexString.append('0');
|
||||
hexString.append(hex);
|
||||
}
|
||||
|
||||
return hexString.toString();
|
||||
} catch (NoSuchAlgorithmException e) {
|
||||
e.printStackTrace();
|
||||
return null;
|
||||
}
|
||||
}
|
||||
|
||||
public boolean verifyPassword(String password) {
|
||||
String hashedPassword = hashPassword(password);
|
||||
return hashedPassword != null && hashedPassword.equals(this.password);
|
||||
}
|
||||
|
||||
@Override
|
||||
public String toString() {
|
||||
return "User{" +
|
||||
"id=" + id +
|
||||
", username='" + username + '\'' +
|
||||
", age=" + getAge() +
|
||||
", sexe=" + sexe +
|
||||
'}';
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user