Commit bbdb7bb6 authored by Aroubate Hamza's avatar Aroubate Hamza
Browse files

TP5

parent 55ed19db
......@@ -182,12 +182,14 @@ public class Piece extends ElementStructurel {
* @return le String de description
*/
public String toString(){
StringBuilder str = new StringBuilder("PIECE \n les objets : ");
StringBuilder str = new StringBuilder("PIECE ");
str.append(this.getNom());
str.append("\n les objets :");
for (String s : objets.keySet()){
str.append(" - ");
str.append(s);
}
str.append(";\nVivants :");
str.append(";\n Vivants :");
for (String s : vivants.keySet()){
str.append(" - ");
str.append(s);
......
......@@ -53,7 +53,11 @@ public abstract class Vivant extends Entite {
* @param nom nom de l'objet a prendre si present et deplacable
*/
public void prendre( String nom)throws ObjetAbsentDeLaPieceException, ObjetNonDeplacableException{
objets.put(nom, piece.retirer(nom));
if (!piece.contientObjet(nom))
throw new ObjetAbsentDeLaPieceException();
else
objets.put(nom, piece.retirer(nom));
}
/**
......@@ -153,15 +157,17 @@ public abstract class Vivant extends Entite {
* @throws PorteInexistanteDansLaPieceException
*/
public void franchir(String nomPorte) throws PorteFermeException, PorteInexistanteDansLaPieceException{
if (!(piece.aLaPorte(nomPorte)) || piece.getPorte(nomPorte).getPieceAutreCote(piece)==null){
throw new PorteInexistanteDansLaPieceException();
}
if (piece.getPorte(nomPorte).getEtat().equals(Etat.FERME)){
throw new PorteFermeException();
}
try{
if (!(piece.aLaPorte(nomPorte)) || piece.getPorte(nomPorte).getPieceAutreCote(piece)==null){
throw new PorteInexistanteDansLaPieceException();
}
if (piece.getPorte(nomPorte).getEtat().equals(Etat.FERME)){
throw new PorteFermeException();
}
Piece piece_pro = piece.getPorte(nomPorte).getPieceAutreCote(piece);
piece.sortir(this);
piece = piece.getPorte(nomPorte).getPieceAutreCote(piece);
piece = piece_pro;
piece.entrer(this);
}
catch(VivantAbsentDeLaPieceException e) {
......
......@@ -42,7 +42,7 @@ public class TestVivant {
public void init() throws Exception{
monde = new Monde("monde");
piece = new Piece(monde, "piece");
pieddebiche = new PiedDeBiche("piedebiche", monde);
pieddebiche = new PiedDeBiche("pieddebiche", monde);
pieddebiche1 = new PiedDeBiche("pieddebiche1",monde);
vivant = new Vivant("vivant", monde, 10, 10, piece, pieddebiche, pieddebiche1){};
}
......@@ -159,29 +159,33 @@ public class TestVivant {
public void test_franchir() throws NomDEntiteDejaUtiliseDansLeMondeException, PorteFermeException, PorteInexistanteDansLaPieceException, ActivationImpossibleException {
Piece piece2 = new Piece(monde, "piece2");
Piece piece1 = new Piece(monde, "piece1");
Vivant vivant2 = new Vivant("vivant2", monde, 10, 10, piece1, pieddebiche){};
Porte porte1 = new Porte("porte1", monde, piece1, piece2);
porte1.activer();
vivant.franchir(porte1);
assertThat(vivant.getPiece(), IsEqual.equalTo(piece2));
vivant.franchir("porte1");
assertThat(vivant.getPiece(), IsEqual.equalTo(piece1));
vivant2.franchir("porte1");
assertThat(vivant2.getPiece(), IsEqual.equalTo(piece2));
vivant2.franchir("porte1");
assertThat(vivant2.getPiece(), IsEqual.equalTo(piece1));
}
@Test(expected=PorteFermeException.class)
public void test_franchir_PorteFerme() throws NomDEntiteDejaUtiliseDansLeMondeException, PorteFermeException, PorteInexistanteDansLaPieceException, ActivationImpossibleException {
Piece piece2 = new Piece(monde, "piece2");
Piece piece1 = new Piece(monde, "piece1");
Vivant vivant1 = new Vivant("vivant1", monde, 10, 10, piece1, pieddebiche){};
Porte porte1 = new Porte("porte1", monde, piece1, piece2);
vivant.franchir(porte1);
vivant1.franchir(porte1);
}
@Test(expected=PorteInexistanteDansLaPieceException.class)
public void test_franchir_PorteInexistante() throws NomDEntiteDejaUtiliseDansLeMondeException, PorteFermeException, PorteInexistanteDansLaPieceException, ActivationImpossibleException {
Piece piece1 = new Piece(monde,"piece1");
Piece piece2 = new Piece(monde,"piece2");
Piece piece3 = new Piece(monde,"piece3");
Porte porte1 = new Porte("porte1", monde, piece2, piece3);
Vivant vivant1 = new Vivant("vivant1", monde, 10, 10, piece2, pieddebiche){};
Porte porte1 = new Porte("porte1", monde, piece1, piece3);
porte1.activer();
vivant.franchir(porte1);
vivant1.franchir(porte1);
}
@Test
......@@ -192,7 +196,7 @@ public class TestVivant {
Porte porte2 = new Porte("porte2", monde, piece2, piece3);
porte1.activer();
try{
vivant.activerActivable(porte1);
vivant1.activerActivable(porte1);
}
catch(Exception e) {
System.out.println();
......
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