Skip to content
Snippets Groups Projects
Commit 296791d8 authored by Ivan Gonzalez's avatar Ivan Gonzalez
Browse files

Iniciado el ciclo green con Persona con el constructor y los getters de la clase

parent 6fc2fe40
No related branches found
No related tags found
2 merge requests!3Develop,!1Persona
Pipeline #
......@@ -11,6 +11,13 @@ import java.util.ArrayList;
*/
public class Persona {
private String nombre;
private int id;
private ArrayList<Persona> amigos;
private ArrayList<Persona> conocidos;
private int maxAmigos;
private int amigosPorColar;
/**
* Constructor parametrizado de la clase Persona en el que se espifica lo
* siguiente. Como aclaración, una Persona puede hacer amigos en la cola,
......@@ -33,7 +40,44 @@ public class Persona {
* parámetros.
*/
public Persona(String nombre, int id, ArrayList<Persona> amigos, ArrayList<Persona> conocidos) {
// TODO Auto-generated constructor stub
if (nombre == null)
throw new IllegalArgumentException("El nombre no puede ser null.");
if (nombre.equals(""))
throw new IllegalArgumentException("El nombre no puede ser cadena vacía.");
if (id < 0)
throw new IllegalArgumentException("El identificador de la Persona no puede ser negativo.");
if (amigos == null)
throw new IllegalArgumentException("La lista de amigos no puede ser nula.");
if (hasElementoNull(amigos))
throw new IllegalArgumentException("La lista de amigos no puede contener elementos nulos.");
if (conocidos == null)
throw new IllegalArgumentException("La lista de conocidos no puede ser nula.");
if (hasElementoNull(conocidos))
throw new IllegalArgumentException("La lista de conocidos no puede contener elementos nulos.");
this.nombre = nombre;
this.id = id;
this.amigos = amigos;
this.conocidos = conocidos;
maxAmigos = 0;
amigosPorColar = maxAmigos;
}
/**
* Comprueba si la lista pasada como parámetro tiene elementos nulos.
*
* @param lista
* ArrayList a comprobar si tiene elementos nulos.Debe ser
* correcto: no nulo.
* @return true si hay algún elemento nulo, false en caso contrario.
*/
public boolean hasElementoNull(ArrayList<Persona> lista) {
if (lista == null)
throw new IllegalArgumentException("La lista no puede ser nula.");
for (int i = 0; i < lista.size(); i++) {
if (lista.get(i) == null)
return true;
}
return false;
}
/**
......@@ -117,13 +161,11 @@ public class Persona {
}
public String getNombre() {
// TODO Auto-generated method stub
return null;
return nombre;
}
public int getId() {
// TODO Auto-generated method stub
return 0;
return id;
}
/**
......@@ -131,8 +173,7 @@ public class Persona {
* @return numero maximo de amigos que puede colar this.
*/
public int getMaxAmigos() {
// TODO Auto-generated method stub
return 0;
return maxAmigos;
}
/**
......@@ -140,11 +181,18 @@ public class Persona {
*
* @param maxAmigos
* cantidad de amigos a colar. Debe ser correcta: Mayor o igual a
* 0 y menor de 10.
* 0 y menor o igual de 10.
* @throws IllegalArgumentException
* si se incumple alguna de las condiciones impuestas al
* parámetro.
*/
public void setMaxAmigos(int maxAmigos) {
// TODO Auto-generated method stub
if (maxAmigos < 0)
throw new IllegalArgumentException("El identificador debe ser positivo.");
if (maxAmigos > 10)
throw new IllegalArgumentException("El identificador debe ser menor de 10.");
this.maxAmigos = maxAmigos;
amigosPorColar = maxAmigos;
}
/**
......@@ -152,16 +200,14 @@ public class Persona {
* @return cantidad de amigos que aún puede colar this.
*/
public int getAmigosPorColar() {
// TODO Auto-generated method stub
return 0;
return amigosPorColar;
}
/**
* @return lista de amigos de this.
*/
public ArrayList<Persona> getAmigos() {
// TODO Auto-generated method stub
return null;
return amigos;
}
/**
......@@ -169,8 +215,7 @@ public class Persona {
* @return lista de conocidos de this.
*/
public ArrayList<Persona> getConocidos() {
// TODO Auto-generated method stub
return null;
return conocidos;
}
}
......@@ -5,7 +5,8 @@ import org.junit.runners.Suite;
import org.junit.runners.Suite.SuiteClasses;
@RunWith(Suite.class)
@SuiteClasses({ PersonaConstructorTDDTest.class, PersonaGettersTDDTest.class, PersonaOperacionesTDDTest.class })
@SuiteClasses({ PersonaConstructorTDDTest.class, PersonaGettersTDDTest.class, PersonaOperacionesTDDTest.class,
PersonaConstructorTest.class })
public class AllPersonaTest {
}
package inf.uva.es.ivagonz.practica4;
import static org.junit.Assert.*;
import java.util.ArrayList;
import org.junit.After;
import org.junit.Before;
import org.junit.Test;
public class PersonaConstructorTest {
private String nombre;
private int id;
private ArrayList<Persona> amigos;
private ArrayList<Persona> conocidos;
@Before
public void setUp() {
nombre = "Pepe";
id = 1;
amigos = new ArrayList<Persona>();
conocidos = new ArrayList<Persona>();
}
@After
public void tearDown() {
nombre = null;
id = 0;
amigos = null;
conocidos = null;
}
@Test
public void hasElementoNullCorrectoTest() {
Persona p = new Persona(nombre, id, amigos, conocidos);
p.hasElementoNull(amigos);
assertFalse(p.hasElementoNull(amigos));
}
@Test(expected = IllegalArgumentException.class)
public void hasAmigoNullConListaNullTest() {
Persona p = new Persona(nombre, id, amigos, conocidos);
p.hasElementoNull(null);
}
}
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment