Skip to content
Snippets Groups Projects
Commit e14525cc authored by @potel5u's avatar @potel5u
Browse files

construction des classes du modèle (TODO : a tester)

parent 80b07b4a
No related branches found
No related tags found
No related merge requests found
package fr.ul.projet.modele;
import fr.ul.projet.outils.FabriqueIdentifiant;
public class Acces {
private int id;
private int xCoinHautGauche, yCoinHautGauche;
private int xCoinBasDroite, yCoinBasDroite;
private Piece destination;
public Acces(Piece dest, int x1, int y1, int x2, int y2){
this.destination = dest;
this.xCoinHautGauche = x1;
this.yCoinHautGauche = y1;
this.xCoinBasDroite = x2;
this.yCoinBasDroite = y2;
this.id = FabriqueIdentifiant.getInstance().getCptAcces();
}
public int getId() {
return this.id;
}
public int getXCoinBasDroite() {
return this.xCoinBasDroite;
}
public void setxCoinBasDroite(int xCoinBasDroite) {
this.xCoinBasDroite = xCoinBasDroite;
}
public int getXCoinHautGauche() {
return this.xCoinHautGauche;
}
public void setxCoinHautGauche(int xCoinHautGauche) {
this.xCoinHautGauche = xCoinHautGauche;
}
public int getYCoinBasDroite() {
return this.yCoinBasDroite;
}
public void setyCoinBasDroite(int yCoinBasDroite) {
this.yCoinBasDroite = yCoinBasDroite;
}
public int getYCoinHautGauche() {
return this.yCoinHautGauche;
}
public void setyCoinHautGauche(int yCoinHautGauche) {
this.yCoinHautGauche = yCoinHautGauche;
}
public Piece getDestination() {
return this.destination;
}
public void setDestination(Piece destination) {
this.destination = destination;
}
}
package fr.ul.projet.modele;
import org.json.simple.JSONObject;
import org.json.simple.JSONValue;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileWriter;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.HashMap;
import java.util.LinkedList;
import java.util.List;
import fr.ul.projet.outils.FabriqueIdentifiant;
public class Modele {
private boolean pieceAffichee;
private String fichierJson;
private String orientationCourante;
HashMap<Integer, Piece> pieces;
Piece pieceCourante;
/**
* Constructeur d'un Modèle
*
*/
public Modele(){
this.pieceAffichee = false;
this.pieceCourante = null;
this.fichierJson = new File("").getAbsolutePath() + "/modele" + FabriqueIdentifiant.getInstance().getCptJson() + ".json";
}
public void nouveau(){
this.pieceAffichee = false;
this.pieceCourante = null;
this.fichierJson = new File("").getAbsolutePath() + "/modele" + FabriqueIdentifiant.getInstance().getCptJson() + ".json";
this.pieces.clear();
FabriqueIdentifiant.reset();
}
/**
* Getters et Setters ------------------------------------------------------------------------------------------------------------------------
*/
public Piece getPieceCourante() {
return this.pieceCourante;
}
public String getFichierJson() {
return this.fichierJson;
}
public String getOrientationCourante() {
return this.orientationCourante;
}
public HashMap<Integer, Piece> getPieces() {
return this.pieces;
}
public boolean isPieceAffichee() {
return this.pieceAffichee;
}
public void setFichierJson(String fichierJson) {
this.fichierJson = fichierJson;
}
public void setOrientationCourante(String orientationCourante) {
this.orientationCourante = orientationCourante;
}
public void setPieceAffichee(boolean pieceAffichee) {
this.pieceAffichee = pieceAffichee;
}
public void setPieceCourante(Piece pieceCourante) {
this.pieceCourante = pieceCourante;
this.pieceAffichee = true;
}
/**
* Méthodes d'ajout et suppression ------------------------------------------------------------------------------------------------------------------------
*/
public void ajouterPiece(String nom, String photoNord, String photoSud, String photoEst, String photoOuest){
Piece piece = new Piece(nom, photoNord, photoSud, photoEst, photoOuest);
this.pieces.put(piece.getId(), piece);
this.setPieceCourante(piece);
}
public void modifierPieceCourante(String nom, String photoNord, String photoSud, String photoEst, String photoOuest){
this.getPieceCourante().setNom(nom);
this.getPieceCourante().setPhotoNord(photoNord);
this.getPieceCourante().setPhotoSud(photoSud);
this.getPieceCourante().setPhotoEst(photoEst);
this.getPieceCourante().setPhotoOuest(photoOuest);
}
public void supprimerPiece(int idPiece){
Piece pieceSupp = this.getPieces().get(idPiece);
pieceSupp.getAcces().clear();
for (Piece p : this.getPieces().values()){
if (!p.equals(pieceSupp)) {
for (Acces a : p.getAcces().values()) {
if (a.getDestination().equals(pieceSupp)) {
p.getAcces().remove(a.getId());
}
}
}
}
this.getPieces().remove(idPiece);
}
public void ajouterAcces(Piece destination, int x1, int y1, int x2, int y2){
this.getPieceCourante().ajouterAcces(destination, x1, y1, x2, y2);
}
public void supprimerAcces(String idAcces){
this.getPieceCourante().supprimerAcces(idAcces);
}
/**
* Méthodes de déplacement ------------------------------------------------------------------------------------------------------------------------
*/
public void utiliserAcces(String idAcces){
this.setPieceCourante(this.getPieceCourante().getAcces().get(idAcces).getDestination());
}
public void tournerHoraire(){
switch (this.getOrientationCourante()){
case "NORD":
this.setOrientationCourante("EST");
break;
case "SUD":
this.setOrientationCourante("OUEST");
break;
case "EST":
this.setOrientationCourante("SUD");
break;
case "OUEST":
this.setOrientationCourante("NORD");
break;
default:
break;
}
}
public void tournerAntiHoraire(){
switch (this.getOrientationCourante()){
case "NORD":
this.setOrientationCourante("OUEST");
break;
case "SUD":
this.setOrientationCourante("EST");
break;
case "EST":
this.setOrientationCourante("NORD");
break;
case "OUEST":
this.setOrientationCourante("SUD");
break;
default:
break;
}
}
/**
* Méthodes de lecture ou écriture d'un modèle ------------------------------------------------------------------------------------------------------------------------
*/
public String toJson(){
JSONObject modeleJson = new JSONObject();
List pieces = new LinkedList();
for (Piece p : this.pieces.values()){
pieces.add(p.toJson());
}
modeleJson.put("pièces", pieces);
String json = JSONValue.toJSONString(modeleJson);
return json;
}
public void enregistrerModele() throws Exception {
PrintWriter flotFiltre;
FileWriter flot;
try {
File file = new File(this.fichierJson);
if (file.exists()){
file.delete();
}
flot = new FileWriter(file.getAbsoluteFile());
flotFiltre = new PrintWriter(new BufferedWriter(flot));
flotFiltre.print(this.toJson());
flotFiltre.close();
} catch (IOException e){
throw new Exception("Impossble d'écrire (" + e + ")");
}
}
public void chargerModele() throws Exception{
try{
// Charger via json
} catch (Exception e) {
throw new Exception("Impossible de charger le modèle (" + e + ")");
}
}
}
package fr.ul.projet.modele;
import java.util.HashMap;
import java.util.LinkedHashMap;
import java.util.Map;
import fr.ul.projet.outils.FabriqueIdentifiant;
public class Piece {
private int id;
String nom;
String photoNord, photoSud, photoEst, photoOuest;
HashMap<Integer, Acces> acces = new HashMap<>();
public Piece(String nom, String photoNord, String photoSud, String photoEst, String photoOuest){
this.id = FabriqueIdentifiant.getInstance().getIdentifiantPiece();
this.photoNord = photoNord;
this.photoSud = photoSud;
this.photoEst = photoEst;
this.photoOuest = photoOuest;
this.nom = nom;
}
public int getId(){
return this.id;
}
public String getNom() {
return this.nom;
}
public void setNom(String nom){
this.nom = nom;
}
public String getPhotoNord() {
return this.photoNord;
}
public void setPhotoNord(String photoNord){
this.photoNord = photoNord;
}
public String getPhotoEst() {
return this.photoEst;
}
public void setPhotoEst(String photoEst){
this.photoEst = photoEst;
}
public String getPhotoSud() {
return this.photoSud;
}
public void setPhotoSud(String photoSud){
this.photoSud = photoSud;
}
public String getPhotoOuest(){
return this.photoOuest;
}
public void setPhotoOuest(String photoOuest){
this.photoOuest = photoOuest;
}
public HashMap<Integer, Acces> getAcces(){
return this.acces;
}
public void ajouterAcces(Piece destination, int x1, int y1, int x2, int y2){
Acces acc = new Acces(destination, x1, y1, x2, y2);
this.getAcces().put(acc.getId(), acc);
}
public void supprimerAcces(String idAcces){
this.getAcces().remove(idAcces);
}
public Map toJson(){
Map page = new LinkedHashMap();
page.put("id", this.getId());
page.put("nom", this.getNom());
page.put("cheminPhotoNord", this.getPhotoNord());
page.put("cheminPhotoSud", this.getPhotoSud());
page.put("cheminPhotoEst", this.getPhotoEst());
page.put("cheminPhotoOuest", this.getPhotoOuest());
return page;
}
}
package fr.ul.projet.outils;
public class FabriqueIdentifiant {
private int cptPiece;
private int cptJson;
private int cptAcces;
static FabriqueIdentifiant instance;
public FabriqueIdentifiant(){
cptPiece = 0;
cptJson=1;
cptAcces = 0;
}
public static FabriqueIdentifiant getInstance(){
if (instance == null){
instance = new FabriqueIdentifiant();
}
return instance;
}
public int getIdentifiantPiece(){
cptPiece++;
return cptPiece;
}
public int getCptJson(){
cptJson++;
return cptJson;
}
public int getCptAcces(){
cptAcces++;
return cptAcces;
}
public static void reset(){
int cpt = getInstance().getCptJson();
instance = new FabriqueIdentifiant();
instance.cptJson=cpt-1;
}
}
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment