From 3a3ff08aaca9ed81dee8580062446f2b2f6468bb Mon Sep 17 00:00:00 2001
From: Javier Calvo <javiercalvoporro@gmail.com>
Date: Thu, 5 Dec 2024 12:10:23 +0100
Subject: [PATCH] PuertoTest refactorizado

---
 src/es/markse/Contenedor.java  | 12 +++--
 src/es/markse/Muelle.java      | 31 ++-----------
 src/es/markse/Puerto.java      | 28 +++++-------
 uses/es/markse/PuertoTest.java | 84 +++++++++++++++++++++-------------
 4 files changed, 75 insertions(+), 80 deletions(-)

diff --git a/src/es/markse/Contenedor.java b/src/es/markse/Contenedor.java
index e8119a7..107c9b3 100644
--- a/src/es/markse/Contenedor.java
+++ b/src/es/markse/Contenedor.java
@@ -21,9 +21,9 @@ public class Contenedor {
 	private boolean techo;
 	
 	//Seleccion del estado y de las medidas
-	private enum estados{ REGOGIDA, TRANSITO }
-	private enum pesos{ KILOS, LIBRAS}
-	private enum volumenes{ METROS, PIES }
+	public enum estados{ REGOGIDA, TRANSITO }
+	public enum pesos{ KILOS, LIBRAS}
+	public enum volumenes{ METROS, PIES }
 	private estados estadoActual;
 	private final pesos pesoSeleccionado;
 	private final volumenes volumenSeleccionado;
@@ -36,10 +36,12 @@ public class Contenedor {
 	
 	/**
 	 * Constructor del Objeto Contenedor
-	 * @param pesoContenedor El peso del contenedor en kilogramos
+	 * @param pesoTara El peso del contenedor en kilogramos
 	 * @param maximaCargaUtil La carga util del contenedor dada en kilogramos
 	 * @param volumen El volumen dado en metros cúbicos
-	 * @param transito Indica si esta en transito (true) o en recogida (false)
+	 * @param estadoActual estado del contenedor
+	 * @param pesoSeleccionado en que medidas se dan los pesos de la tara y la carga
+	 * @param volumenSeleccionado en que medida se da el volumen
 	 * @param techo indica si tiene techo (true) o no (false)
 	 */
 	public Contenedor(ISO6346 codigo, float pesoTara, float maximaCargaUtil, float volumen, estados estadoActual, pesos pesoSeleccionado, volumenes volumenSeleccionado, boolean techo) {
diff --git a/src/es/markse/Muelle.java b/src/es/markse/Muelle.java
index 949e4e4..9b65ed1 100644
--- a/src/es/markse/Muelle.java
+++ b/src/es/markse/Muelle.java
@@ -16,7 +16,7 @@ import es.uva.inf.poo.maps.GPSCoordinate;
 public class Muelle {
 	private final Plaza[] plazas;
 	private final String identificador;
-	private enum estado{
+	public enum estado{
 		OPERATIVO, FUERA_DE_SERVICIO
 	}
 	private estado estadoActual;
@@ -86,7 +86,7 @@ public class Muelle {
 			if(this.contenedores.size() == this.altura)
 				throw new IllegalStateException("La plaza está llena");
 			
-			if(!this.contenedores.get(this.contenedores.size()-1).tieneTecho())
+			if(!this.contenedores.isEmpty() && !this.contenedores.get(this.contenedores.size()-1).tieneTecho())
 				throw new IllegalStateException("El ultimo contenedor no tiene techo");
 				
 		    if (this.contenedores.isEmpty() || (this.ultimoContenedor().tieneTecho() && this.contenedores.size() < this.altura)) {
@@ -163,9 +163,9 @@ public class Muelle {
 	 * @param altura numero maximo de contenedores que se pueden apilar encima de otro
 	 */
 	public Muelle (String identificador, GPSCoordinate cord, estado estadoActual, int plazas, int altura) {
-		if(!comprobarValoresNulos(identificador, cord))
+		if(identificador == null || cord == null)
 			throw new IllegalArgumentException("Los valores del identificador y coordenada no pueden ser nulos");
-		if(!comprobarTamanyoAlturaPlazas(plazas, altura))
+		if(plazas<1 || altura<1)
 			throw new IllegalArgumentException("Los tamaños (altura y plaza) no puedne ser menores a 1");
 		if (!comprobarIdentificador(identificador))
 			throw new IllegalArgumentException("El formato del identificador es incorrecto");
@@ -180,27 +180,6 @@ public class Muelle {
         }
 	}
 	
-	/**
-	 * Metodo para comporbar que los valores no sean nulos
-	 * @param identificador Identificador del muelle
-	 * @param cord Coordenadas de muelle (clase GPSCoordinate)
-	 * @return true si los valores son nulos o false si no lo son
-	 */
-	private boolean comprobarValoresNulos(String identificador, GPSCoordinate cord) {
-		if (identificador == null || cord == null) return true;
-		else return false;
-	}
-	
-	/**
-	 * Metodo que sirve para comprobar si son correctas el tamaño de las plazas
-	 * @param plazas El numero de plazas del muelle
-	 * @param altura El tamaño de la altura del muelle
-	 * @return true si el tamaño es correcto, false si no lo es
-	 */
-	private boolean comprobarTamanyoAlturaPlazas (int plazas, int altura) {
-		return (plazas<1 || altura <1)? false : true;
-	}
-	
 	/**
 	 * Metodo que comprueba si el identificador es correcto
 	 * @param identificador Identificador del muelle
@@ -332,7 +311,7 @@ public class Muelle {
 			throw new IllegalStateException("La plaza está llena");
 		
 		//Verificamos si el ultimo contenedor tiene techo
-		if(!p.ultimoContenedor().tieneTecho())
+		if(p.contenedores.size() > 0 && p.ultimoContenedor() != null && !p.ultimoContenedor().tieneTecho())
 			throw new IllegalStateException("El contenedor no tiene techo");
 		else{
 			this.plazas[plaza].colocarContenedor(contenedor);
diff --git a/src/es/markse/Puerto.java b/src/es/markse/Puerto.java
index d847c0c..cf4b2e0 100644
--- a/src/es/markse/Puerto.java
+++ b/src/es/markse/Puerto.java
@@ -14,7 +14,6 @@ import es.uva.inf.poo.maps.GPSCoordinate;
  * Implementacion de la clase Puerto.
  * @author javcalv
  * @author victorm
- *
  */
 public class Puerto {
 	private final String pais;
@@ -30,7 +29,7 @@ public class Puerto {
 	 * @param ciudad Ciudad al que pertenece el puerto (3 letras)
 	 */
 	public Puerto(String pais, String ciudad) {
-		if(comprobarIdentificadoresNulos(pais,ciudad)) 
+		if(pais == null || ciudad == null) 
 			throw new IllegalArgumentException("El pais o la ciudad no pueden ser nulos");
 		if(!comprobarIdentifiadores(pais.toUpperCase(), ciudad.toUpperCase()))
 			throw new IllegalArgumentException("El pais o la ciudad no tiene el formato incorrecto. 'XX' pais y 'XXX' ciudad.");
@@ -38,16 +37,6 @@ public class Puerto {
 		this.ciudad = ciudad.toUpperCase();
 	}
 	
-	/**
-	 * Metodo que comprueba si la ciudad o el pais son nulos
-	 * @param pais Pais del puerto a comprobar
-	 * @param ciudad Ciudad del puerto a comprobar
-	 * @return true si son nulos o false si no lo son
-	 */
-	private boolean comprobarIdentificadoresNulos(String pais, String ciudad) {
-		return (pais == null || ciudad == null) ? true : false;
-	}
-	
 	/**
 	 * Metodo que comprueba si el pais o la ciudad tienen el formato correcto mediante expresiones regulares.
 	 * @param pais Pais del puerto a comprobar
@@ -62,13 +51,14 @@ public class Puerto {
 	 * 1.1.1. Metodo que agrega un nuevo muelle al puerto. Se comprueba que ese muelle no se encuentre ya agregado.
 	 * Si no lo esta, lo agrega, si ya se encuentra agregado, no.
 	 * @param m objeto tipo Muelle
+	 * @return true si el muelle se ha agregado, o false si el muelle ya esta agregado
 	 * @throws IllegalArgumentException si el muelle es nulo
 	 */
 	public boolean anyadirMuelle(Muelle m) {
-		if (m == null) throw new IllegalArgumentException("Muelle no puede ser nulo");
-		if (!this.comprobarMuelleEnPuerto(m)) {
+		if (m == null) 
+			throw new IllegalArgumentException("Muelle no puede ser nulo");
+		if (!this.comprobarMuelleEnPuerto(m))
 			return this.muelles.add(m);
-		}
 		return false;
 	}
 	
@@ -76,9 +66,11 @@ public class Puerto {
 	 * 1.1.2 Método que elimina un muelle del puerto. Si encuentra uno con el mismo ID, lo elimina
 	 * @param id : Identificador del muelle que se elimina
 	 * @return true si se ha eliminado el muelle y false si no se ha eliminado ninguno (n)
+	 * @throws IllegalArgumentException si el id es nulo;
 	 */
 	public boolean eliminarMuelle(String id) {
-		if (id == null) throw new IllegalArgumentException("Identificador no puede ser nulo");
+		if (id == null || id == "") 
+			throw new IllegalArgumentException("Identificador no puede ser nulo o vacio");
 		return this.muelles.removeIf(muelle -> muelle.getIdentificador().equals(id));
 	}
 	
@@ -156,11 +148,13 @@ public class Puerto {
 	 * ya que solo puede haber un muelle con el mismo identificador para cada puerto.
 	 * @param m obtejo Muelle
 	 * @return true si esta, y false si no se encuentra en el puerto
+	 * @throws IllegalArgumentException si el muelle es nulo
 	 */
 	public boolean comprobarMuelleEnPuerto(Muelle m) {
+		if (m == null)
+			throw new IllegalArgumentException("El muelle no puede ser nulo");
 	    String id = m.getIdentificador();
 	    GPSCoordinate cord = m.getGPSCoordinate();
-	    //Para daca muelle comprobamos si hay alguno con el mismo id o las mismas coordenadas
 	    for (Muelle muelle : this.muelles) {
 	        if (id.equals(muelle.getIdentificador())|| (cord.getDistanceTo(muelle.getGPSCoordinate())==0)) {
 	            return true;
diff --git a/uses/es/markse/PuertoTest.java b/uses/es/markse/PuertoTest.java
index b0785db..00123ec 100644
--- a/uses/es/markse/PuertoTest.java
+++ b/uses/es/markse/PuertoTest.java
@@ -5,6 +5,7 @@ package es.markse;
 import static org.junit.Assert.*;
 import org.junit.Test;
 
+import es.markse.Muelle.estado;
 import es.uva.inf.poo.maps.GPSCoordinate;
 
 
@@ -20,6 +21,7 @@ public class PuertoTest {
 		Puerto p = new Puerto("ES","BAR");
 		assertEquals("ES", p.paisDelPuerto());
         assertEquals("BAR", p.ciudadDelPuerto());
+        assertEquals("ES-BAR", p.identificadorPuerto());
 	}
 	
 	@Test(expected = IllegalArgumentException.class)
@@ -56,7 +58,7 @@ public class PuertoTest {
 	public void testAnyadirMuelle() {
 		Puerto p = new Puerto("ES","BAR");
 		GPSCoordinate cord = new GPSCoordinate(5d, 10d);
-		Muelle m = new Muelle("03", cord, true, 40, 5);
+		Muelle m = new Muelle("03", cord, Muelle.estado.OPERATIVO, 40, 5);
 		assertTrue(p.anyadirMuelle(m));
 	}
 	
@@ -68,23 +70,31 @@ public class PuertoTest {
 	
 	
 	@Test
-	public void testAnyadirMuelleRepetido() {
+	public void testAnyadirMuelleIDRepetido() {
 		Puerto p = new Puerto("ES","BAR");
 		GPSCoordinate cord1 = new GPSCoordinate(5d, 10d);
 		GPSCoordinate cord2 = new GPSCoordinate(20d, -10d);
-		Muelle m = new Muelle("03", cord1, true, 40, 5);
-		Muelle mismoID = new Muelle("03", cord2, true, 40, 5);
-		Muelle mismaCoordenada = new Muelle("05", cord1, true, 40, 5);
+		Muelle m = new Muelle("03", cord1, Muelle.estado.OPERATIVO, 40, 5);
+		Muelle mismoID = new Muelle("03", cord2, Muelle.estado.OPERATIVO, 40, 5);
 		p.anyadirMuelle(m);
-		assertFalse(p.anyadirMuelle(mismaCoordenada));
 		assertFalse(p.anyadirMuelle(mismoID));	
 	}
 	
+	@Test
+	public void testAnyadirMuelleCoordenadaRepetida() {
+		Puerto p = new Puerto("ES","BAR");
+		GPSCoordinate cord = new GPSCoordinate(5d, 10d);
+		Muelle m = new Muelle("03", cord, Muelle.estado.OPERATIVO, 40, 5);
+		Muelle mismaCoordenada = new Muelle("05", cord, Muelle.estado.OPERATIVO, 40, 5);
+		p.anyadirMuelle(m);
+		assertFalse(p.anyadirMuelle(mismaCoordenada));	
+	}
+	
 	@Test
 	public void testEliminarMuelle() {
 		Puerto p = new Puerto("ES","BAR");
 		GPSCoordinate cord = new GPSCoordinate(5d, 10d);
-		Muelle m = new Muelle("03", cord, true, 40, 5);
+		Muelle m = new Muelle("03", cord, Muelle.estado.OPERATIVO, 40, 5);
 		p.anyadirMuelle(m);
 		assertTrue(p.eliminarMuelle("03"));
 	}
@@ -93,11 +103,20 @@ public class PuertoTest {
 	public void testEliminarMuelleNulo() {
 		Puerto p = new Puerto("ES","BAR");
 		GPSCoordinate cord = new GPSCoordinate(5d, 10d);
-		Muelle m = new Muelle("03", cord, true, 40, 5);
+		Muelle m = new Muelle("03", cord, Muelle.estado.OPERATIVO, 40, 5);
 		p.anyadirMuelle(m);
 		p.eliminarMuelle(null);
 	}
 	
+	@Test(expected = IllegalArgumentException.class)
+	public void testEliminarMuelleVacio() {
+		Puerto p = new Puerto("ES","BAR");
+		GPSCoordinate cord = new GPSCoordinate(5d, 10d);
+		Muelle m = new Muelle("03", cord, Muelle.estado.OPERATIVO, 40, 5);
+		p.anyadirMuelle(m);
+		p.eliminarMuelle("");
+	}
+	
 	@Test
 	public void testEliminarMuelleQueNoEstaEnPuerto() {
 		Puerto p = new Puerto("ES","BAR");
@@ -109,13 +128,16 @@ public class PuertoTest {
 		Puerto p = new Puerto("ES","BAR");
 		GPSCoordinate cord1 = new GPSCoordinate(5d, 10d);
 		GPSCoordinate cord2 = new GPSCoordinate(20d, -10d);
-		Muelle m1 = new Muelle("01", cord1, true, 1, 1);
-		Muelle m2 = new Muelle("04", cord2, true, 2, 1);
+		Muelle m1 = new Muelle("01", cord1, Muelle.estado.OPERATIVO, 1, 1);
 		p.anyadirMuelle(m1);
+		Muelle m2 = new Muelle("04", cord2, Muelle.estado.OPERATIVO, 2, 1);
 		p.anyadirMuelle(m2);
-		Contenedor c11 = new Contenedor("ZRE", 'J', "056731", 100, 400, 500, false, true);
-		Contenedor c21 = new Contenedor("RTZ", 'Z', "569026", 100, 400, 500, false, true);
-		Contenedor c22 = new Contenedor("WCD", 'Z', "432012", 100, 400, 500, false, true);
+		ISO6346 i1 = new ISO6346("ZRE", 'J' , 56731);
+		ISO6346 i2 = new ISO6346("RTZ", 'Z', 569026);
+		ISO6346 i3 = new ISO6346("WCD", 'Z', 432012);
+		Contenedor c11 = new Contenedor(i1, 100, 400, 500, Contenedor.estados.REGOGIDA, Contenedor.pesos.KILOS, Contenedor.volumenes.METROS, true);
+		Contenedor c21 = new Contenedor(i2, 100, 400, 500, Contenedor.estados.REGOGIDA, Contenedor.pesos.KILOS, Contenedor.volumenes.METROS, true);
+		Contenedor c22 = new Contenedor(i3, 100, 400, 500, Contenedor.estados.REGOGIDA, Contenedor.pesos.KILOS, Contenedor.volumenes.METROS, true);
 		m1.colocarContenedorEnPlaza(c11, 0);
 		m2.colocarContenedorEnPlaza(c21, 0);
 		m2.colocarContenedorEnPlaza(c22, 1);
@@ -127,12 +149,15 @@ public class PuertoTest {
 		Puerto p = new Puerto("ES","BAR");
 		GPSCoordinate cord1 = new GPSCoordinate(5d, 10d);
 		GPSCoordinate cord2 = new GPSCoordinate(20d, -10d);
-		Muelle m1 = new Muelle("01", cord1, true, 1, 1);
-		Muelle m2 = new Muelle("04", cord2, true, 2, 1);
+		Muelle m1 = new Muelle("01", cord1, Muelle.estado.OPERATIVO, 1, 1);
 		p.anyadirMuelle(m1);
+		Muelle m2 = new Muelle("04", cord2, Muelle.estado.OPERATIVO, 2, 1);
 		p.anyadirMuelle(m2);
-		Contenedor c11 = new Contenedor("ZRE", 'J', "056731", 100, 400, 500, false, true);
-		Contenedor c21 = new Contenedor("RTZ", 'Z', "569026", 100, 400, 500, false, true);
+		ISO6346 i1 = new ISO6346("ZRE", 'J' , 56731);
+		ISO6346 i2 = new ISO6346("RTZ", 'Z', 569026);
+		ISO6346 i3 = new ISO6346("WCD", 'Z', 432012);
+		Contenedor c11 = new Contenedor(i1, 100, 400, 500, Contenedor.estados.REGOGIDA, Contenedor.pesos.KILOS, Contenedor.volumenes.METROS, true);
+		Contenedor c21 = new Contenedor(i2, 100, 400, 500, Contenedor.estados.REGOGIDA, Contenedor.pesos.KILOS, Contenedor.volumenes.METROS, true);
 		m1.colocarContenedorEnPlaza(c11, 0);
 		m2.colocarContenedorEnPlaza(c21, 0);
 		assertFalse(p.comprobarSiEstaLleno());
@@ -143,42 +168,38 @@ public class PuertoTest {
 		Puerto p = new Puerto("ES","BAR");
 		GPSCoordinate cord1 = new GPSCoordinate(5d, 10d);
 		GPSCoordinate cord2 = new GPSCoordinate(20d, -10d);
-		Muelle m1 = new Muelle("01", cord1, false, 1, 1);
-		Muelle m2 = new Muelle("04", cord2, false, 2, 1);
+		Muelle m1 = new Muelle("01", cord1, Muelle.estado.OPERATIVO, 1, 1);
 		p.anyadirMuelle(m1);
+		Muelle m2 = new Muelle("04", cord2, Muelle.estado.FUERA_DE_SERVICIO, 2, 1);
 		p.anyadirMuelle(m2);
-		assertEquals(0, p.muellesOperativos().length);
-		m1.alternarOperativo();
-		m2.alternarOperativo();
-		Muelle[] muellesEsperados = {m1, m2};
+		Muelle[] muellesEsperados = {m1};
 		assertArrayEquals(muellesEsperados, p.muellesOperativos());
 	}
 	
-	
 	@Test
 	public void testMuellesConEspacio() {
 		Puerto p = new Puerto("ES","BAR");
 		GPSCoordinate cord1 = new GPSCoordinate(5d, 10d);
-		Muelle m1 = new Muelle("01", cord1, true, 1, 1);
+		Muelle m1 = new Muelle("01", cord1, Muelle.estado.OPERATIVO, 1, 1);
 		p.anyadirMuelle(m1);
 		Muelle[] muellesEsperados = {m1};
 		assertArrayEquals(muellesEsperados, p.muellesConEspacio());
-		Contenedor c11 = new Contenedor("ZRE", 'J', "056731", 100, 400, 500, false, true);
+		ISO6346 i1 = new ISO6346("ZRE", 'J' , 56731);
+		Contenedor c11 = new Contenedor(i1, 100, 400, 500, Contenedor.estados.REGOGIDA, Contenedor.pesos.KILOS, Contenedor.volumenes.METROS, true);
 		m1.colocarContenedorEnPlaza(c11, 0);
 		assertEquals(0, p.muellesConEspacio().length);
 	}
 	
-	
 	@Test
 	public void testMuellesCercanoAPunto() {
 		Puerto p = new Puerto("ES","BAR");
 		GPSCoordinate cord1 = new GPSCoordinate(10d, 10d);
 		GPSCoordinate cord2 = new GPSCoordinate(-10d, -10d);
-		Muelle m1 = new Muelle("01", cord1, true, 1, 1);
-		Muelle m2 = new Muelle("05", cord2, true, 2, 2);
-		GPSCoordinate puntoDado = new GPSCoordinate(15d , 15d);
+		Muelle m1 = new Muelle("01", cord1, Muelle.estado.OPERATIVO, 1, 1);
 		p.anyadirMuelle(m1);
+		Muelle m2 = new Muelle("04", cord2, Muelle.estado.OPERATIVO, 2, 1);
 		p.anyadirMuelle(m2);
+		GPSCoordinate puntoDado = new GPSCoordinate(15d , 15d);
 		Muelle[] muelleCercanos = {m1};
 		assertArrayEquals(muelleCercanos, p.muellesCercanoAPunto(puntoDado));
 		puntoDado = new GPSCoordinate(0d , 0d);
@@ -190,6 +211,5 @@ public class PuertoTest {
 	public void testMuellesCercanoAPuntoVacio() {
 		Puerto p = new Puerto("ES","BAR");
 		p.muellesCercanoAPunto(null);
-	}
-	
+	}	
 }
\ No newline at end of file
-- 
GitLab