¡HOLA!
Tengo que pasar este test unitario
@Test
public void testCompareTo() {
try {
Wrestler romanReigns = new Powerhouse("Leati Joseph \"Joe\" Anoa'i", LocalDate.of(1985, 5, 25),
"Pensacola, Florida, U.S.", Gender.MALE, 191, 130, "Roman Reigns");
romanReigns.setSignature("Spear");
romanReigns.setFinisher("Superman Punch");
WrestlerProperties strengthProperties = new StrengthProperties(98, 95);
WrestlerProperties agilityProperties = new AgilityProperties(80, 80, 60);
WrestlerProperties staminaProperties = new StaminaProperties(95, 95, 95);
WrestlerProperties techniqueProperties = new TechniqueProperties(85);
WrestlerProperties defenseProperties = new DefenseProperties(80, 85, 80, 97);
WrestlerProperties[] properties = {strengthProperties, agilityProperties, staminaProperties, techniqueProperties, defenseProperties};
romanReigns.setProperties(properties);
assertEquals(0, wrestler.compareTo(wrestler));
assertEquals(0, romanReigns.compareTo(romanReigns));
assertEquals(-11, wrestler.compareTo(romanReigns));
assertEquals(11, romanReigns.compareTo(wrestler));
assertThrows(NullPointerException.class, () -> wrestler.compareTo(null));
} catch (SuperstarException | WrestlerException ex) {
fail("There was an error with compareTo: " + ex.getMessage());
}
}
Classe Wrestler
import java.time.LocalDate;
import java.time.format.DateTimeFormatter;
import java.util.Arrays;
import java.lang.Comparable;
public abstract class Wrestler extends Superstar implements Speakable, Comparable<Wrestler>{
private double strength;
private double agility;
private double stamina;
private double technique;
private double defense;
private double energy;
private String signature;
private String finisher;
private boolean isFace;
private final WrestlerProperties[] properties;
public static final int ATTRIBUTES_MIN_VALUE = 1;
public static final int ATTRIBUTES_MAX_VALUE = 96;
private static final int ATTRIBUTES_SUM_VALUE = 100;
private static final String DEFAULT_SIGNATURE = "Superkick";
private static final String DEFAULT_FINISHER = "Canadian Destroyer";
protected Wrestler(String birthName, LocalDate birthDate, String birthplace,
Gender gender, double height, double weight, String ringName,
double strength, double agility, double stamina, double technique,
double defense) throws SuperstarException, WrestlerException {
super(birthName, birthDate, birthplace, gender, height, weight, ringName);
//setBirthName(birthName);
//setBirthDate(birthDate);
//setBirthplace(birthplace);
//setGender(gender);
//setHeight(height);
//setWeight(weight);
//setRingName(ringName);
this.strength = 20;
this.agility = 20;
this.stamina = 20;
this.technique = 20;
this.defense = 20;
this.energy = 100;
this.signature = DEFAULT_SIGNATURE;
this.finisher = DEFAULT_FINISHER;
this.isFace = true;
this.properties= new WrestlerProperties[5];
properties[0] = new StrengthProperties(0, 0);
properties[1] = new AgilityProperties(0,0,0);
properties[2] = new StaminaProperties(0, 0,0);
properties[3] = new TechniqueProperties(0);
properties[4] = new DefenseProperties(0,0,0,0);
String[] attributes = {"strength", "agility", "stamina", "technique", "defense"};
for (String attribute : attributes) {
double value = switch (attribute) {
case "strength" -> strength;
case "agility" -> agility;
case "stamina" -> stamina;
case "technique" -> technique;
case "defense" -> defense;
default -> throw new WrestlerException(WrestlerException.MSG_ERR_ATTRIBUTES_VALUES);
};
if (!isAttributeInRange(value)) {
throw new WrestlerException(WrestlerException.MSG_ERR_ATTRIBUTES_VALUES + attribute);
}
}
if (!areAttributesSumInRange()){
throw new WrestlerException(WrestlerException.MSG_ERR_ATTRIBUTES_MAX_VALUE);
}
}
public WrestlerProperties[] setProperties(WrestlerProperties[] properties) throws WrestlerException {
if (properties == null || Arrays.asList(properties).contains(null)) {
throw new WrestlerException(WrestlerException.MSG_ERR_PROPERTIES_NULL);
}
//Els valors a l'atribut properties
return properties;
}
private boolean isAttributeInRange(double value) {
return value >= ATTRIBUTES_MIN_VALUE && value <= ATTRIBUTES_MAX_VALUE;
}
/*
private boolean areAttributesSumInRange(double strength, double agility, double stamina, double technique, double defense ) {
return (strength + agility + stamina + technique + defense) == ATTRIBUTES_SUM_VALUE;
}
*/
private boolean areAttributesSumInRange() {
return (strength + agility + stamina + technique + defense) == ATTRIBUTES_SUM_VALUE;
}
public WeightClass getWeightClass() {
return WeightClass.getWeightClass(getWeight());
}
@Override
public void speak() {
System.out.print("I'm the best wrestler in the world!!");
}
public double getStrength() {return strength;}
private void setStrength(double strength) throws WrestlerException {
if (isAttributeInRange(strength)) {
this.strength = strength;
} else {
throw new WrestlerException(WrestlerException.MSG_ERR_ATTRIBUTES_VALUES + " - strength");
}
}
public double getAgility() {return agility;}
private void setAgility(double agility) throws WrestlerException {
if (isAttributeInRange(agility)) {
this.agility = agility;
} else {
throw new WrestlerException(WrestlerException.MSG_ERR_ATTRIBUTES_VALUES + "agility");
}
}
public double getStamina() {return stamina;}
private void setStamina(double stamina) throws WrestlerException {
if (isAttributeInRange(stamina)) {
this.stamina = stamina;
} else {
throw new WrestlerException(WrestlerException.MSG_ERR_ATTRIBUTES_VALUES + "stamina");
}
}
public double getTechnique() {return technique;}
private void setTechnique(double technique) throws WrestlerException {
if (isAttributeInRange(technique)) {
this.technique = technique;
} else {
throw new WrestlerException(WrestlerException.MSG_ERR_ATTRIBUTES_VALUES + "technique");
}
}
public double getDefense() {return defense;}
private void setDefense(double defense) throws WrestlerException {
if (isAttributeInRange(defense)) {
this.defense = defense;
} else {
throw new WrestlerException(WrestlerException.MSG_ERR_ATTRIBUTES_VALUES + "defense");
}
}
public double getEnergy() {return energy;}
public void setEnergy(double energy) {
if (energy < 0) {
this.energy = 0;
} else if (energy > 100) {
this.energy = 100;
} else {
this.energy = energy;
}
}
public String getSignature() {return signature;}
public void setSignature(String signature) {
this.signature = (signature != null) ? signature : DEFAULT_SIGNATURE;
}
public String getFinisher() {return finisher;}
public void setFinisher(String finisher) {
this.finisher = (finisher != null) ? finisher : DEFAULT_FINISHER;
}
public boolean isFace() {return isFace;}
public void setFace(boolean face) {this.isFace = face;}
public double getOverall() {
double average = (strength + agility + stamina + technique + defense) / 5.0;
return Math.round(average * 10.0) / 10.0;
}
@Override
public String toString() {
DateTimeFormatter dateFormatter = DateTimeFormatter.ofPattern("dd-MM-yyyy");
return getRingName().toUpperCase() + System.lineSeparator() +
"\tBirth name: " + getBirthName() + System.lineSeparator() +
"\tBorn: " + getBirthDate().format(dateFormatter) + System.lineSeparator() +
"\t " + getBirthplace() + System.lineSeparator() +
"\tSignature: " + signature + System.lineSeparator() +
"\tFinisher: " + finisher + System.lineSeparator() +
"\tOverall: 0.0";
}
@Override
public int compareTo(Wrestler wrestler) {
if (wrestler == null) {
throw new NullPointerException("Cannot compare with a null wrestler");
}
//Comparar pel valor de getOverall
double thisOverall = this.getOverall();
double otherOverall = wrestler.getOverall();
//Ordre descendent
return Double.compare(otherOverall, thisOverall);
}
}
El atributo properties será un array donde su primer valor almacenará un objeto de tipo StrengthProperties, el segundo un objeto AgilityProperties, el tercero StaminaProperties, el cuarto TechniqueProperties y el quinto DefenseProperties.
private final WrestlerProperties[] properties;
this.properties= new WrestlerProperties[5];
properties[0] = new StrengthProperties(0, 0);
properties[1] = new AgilityProperties(0,0,0);
properties[2] = new StaminaProperties(0, 0,0);
properties[3] = new TechniqueProperties(0);
properties[4] = new DefenseProperties(0,0,0,0);
El método getOverall devuelve la media ponderada de las propiedades del luchador.Cada conjunto de propiedades almacenado en el atributo properties debe ser ponderado por el peso de la propiedad (valor indicado por los atributos strength, agility, stamina, technique y defense, respectivamente). El valor resultante debe estar redondeado.
public double getOverall() {
double average = (strength + agility + stamina + technique + defense) / 5.0;
return Math.round(average * 10.0) / 10.0;
}
Esta clase implementa la interfaz Comparable, que es una interfaz de la API Java.Esto nos obliga a sobreescribir el método compareTo. Gracias a compareTo estamos diciendo en la clase que implementa Comparable cómo comparar dos objetos que pertenecen a esta clase.
En el caso de mi clase Wrestler queremos que este método indique cuál de los dos objetos va primero en orden teniendo en cuenta que debe priorizar aquel que tiene más media ( más valor al invocar el método getOverall). Si el objeto pasado como parámetro es null, el método compareTo debe lanzar la excepción de Java NullPointerException sin mensaje.
@Override
public int compareTo(Wrestler wrestler) {
if (wrestler == null) {
throw new NullPointerException();
}
Double thisOverall = this.getOverall();
Double otherOverall = wrestler.getOverall();
return otherOverall.compareTo(thisOverall);
}
Pregunta actualizada. Gracias!
BUEN INICIO DE SEMANA