Skip to content
Snippets Groups Projects
Commit c2b08cc8 authored by Javier Calvo's avatar Javier Calvo
Browse files

Cambios en Puerto: Comprobaciones

parent 2b1e6356
Branches
No related tags found
No related merge requests found
...@@ -6,6 +6,7 @@ package es.markse; ...@@ -6,6 +6,7 @@ package es.markse;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.List; import java.util.List;
import java.util.regex.Pattern;
import es.uva.inf.poo.maps.GPSCoordinate; import es.uva.inf.poo.maps.GPSCoordinate;
...@@ -16,34 +17,47 @@ import es.uva.inf.poo.maps.GPSCoordinate; ...@@ -16,34 +17,47 @@ import es.uva.inf.poo.maps.GPSCoordinate;
* *
*/ */
public class Puerto { public class Puerto {
private String pais; private final String pais;
private String ciudad; private final String ciudad;
private List<Muelle> muelles = new ArrayList<>(); private final List<Muelle> muelles = new ArrayList<>();
private final String PAIS_REGUEX ="[A-Za-z]{2}";
private final String CIUDAD_REGUEX ="[A-Za-z]{3}";
/** /**
* Constructor del objeto Puerto. * Constructor del objeto Puerto.
* @param pais Pais al que pertenece el puerto (2 letras) * @param pais Pais al que pertenece el puerto (2 letras)
* @param ciudad Ciudad al que pertenece el puerto (3 letras) * @param ciudad Ciudad al que pertenece el puerto (3 letras)
*/ */
public Puerto(String pais, String ciudad) { public Puerto(String pais, String ciudad) {
comprobarIdentificador(pais, ciudad); if(comprobarIdentificadoresNulos(pais,ciudad))
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.");
this.pais = pais.toUpperCase(); this.pais = pais.toUpperCase();
this.ciudad = ciudad.toUpperCase(); this.ciudad = ciudad.toUpperCase();
} }
/** /**
* Metodo que comprueba si tanto el pais como la ciudad son correctos (2 y 3 letras respectivamente) * Metodo que comprueba si la ciudad o el pais son nulos
* @param pais Pais del puerto (2 letras) * @param pais Pais del puerto a comprobar
* @param ciudad Ciudad del puerto(3 letras) * @param ciudad Ciudad del puerto a comprobar
* @throws IllegalArgumentException si el pais o la ciudad no corresponden con sus respectivos formatos o * @return true si son nulos o false si no lo son
* cuando son nulos
*/ */
private void comprobarIdentificador(String pais, String ciudad) { private boolean comprobarIdentificadoresNulos(String pais, String ciudad) {
if (pais == null || ciudad == null) { return (pais == null || ciudad == null) ? true : false;
throw new IllegalArgumentException("El pais o la ciudad no pueden ser nulos");
}
if (!(pais.matches("[A-Za-z]{2}") && ciudad.matches("[A-Za-z]{3}"))) {
throw new IllegalArgumentException("Formato incorrecto. Debe ser en el formato 'XX-XXX', donde XX es el país y XXX es la ciudad, usando solo letras.");
} }
/**
* Metodo que comprueba si el pais o la ciudad tienen el formato correcto mediante expresiones regulares.
* @param pais Pais del puerto a comprobar
* @param ciudad Ciudad del puerto a comprobar
* @return true si es el formto correcto o false si no lo es
*/
private boolean comprobarIdentifiadores(String pais, String ciudad) {
Pattern paisPattern = Pattern.compile(this.PAIS_REGUEX);
Pattern ciudadPattern = Pattern.compile(this.CIUDAD_REGUEX);
return (paisPattern.matcher(pais).matches() && ciudadPattern.matcher(ciudad).matches()) ? true: false;
} }
/** /**
...@@ -63,6 +77,7 @@ public class Puerto { ...@@ -63,6 +77,7 @@ public class Puerto {
/** /**
* 1.1.2 Método que elimina un muelle del puerto. * 1.1.2 Método que elimina un muelle del puerto.
* @param id : Identificador del muelle que se 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)
*/ */
public boolean eliminarMuelle(String id) { public boolean eliminarMuelle(String id) {
if (id == null) throw new IllegalArgumentException("Identificador no puede ser nulo"); if (id == null) throw new IllegalArgumentException("Identificador no puede ser nulo");
...@@ -78,6 +93,7 @@ public class Puerto { ...@@ -78,6 +93,7 @@ public class Puerto {
public boolean comprobarMuelleEnPuerto(Muelle m) { public boolean comprobarMuelleEnPuerto(Muelle m) {
String id = m.getIdentificador(); String id = m.getIdentificador();
GPSCoordinate cord = m.getGPSCoordinate(); GPSCoordinate cord = m.getGPSCoordinate();
//Para daca muelle comprobamos si ya esta entro de este puerto
for (Muelle muelle : this.muelles) { for (Muelle muelle : this.muelles) {
if (id.equals(muelle.getIdentificador())|| (cord.getDistanceTo(muelle.getGPSCoordinate())==0)) { if (id.equals(muelle.getIdentificador())|| (cord.getDistanceTo(muelle.getGPSCoordinate())==0)) {
return true; return true;
...@@ -154,6 +170,7 @@ public class Puerto { ...@@ -154,6 +170,7 @@ public class Puerto {
} }
return cercanos.toArray(new Muelle[0]); return cercanos.toArray(new Muelle[0]);
} }
/** /**
* Metodo que devuelve el pais del puerto * Metodo que devuelve el pais del puerto
* @return pais del perto * @return pais del perto
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment