Skip to content
11 changes: 11 additions & 0 deletions src/main/java/shopManager/MyProduct.java
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ public class MyProduct implements Product,Serializable{
//identificador del producto
private String id;
private int number;
private float price;

MyProduct(String id) {
this.id=id;
Expand All @@ -33,6 +34,7 @@ public class MyProduct implements Product,Serializable{
MyProduct(String id,int number) {
this.id=id;
this.number=number;
this.price = 0;
}
@Override
public void setId(String id) {
Expand All @@ -57,6 +59,15 @@ public int oneMore() {
number++;
return number;
}
//Nuevos metodos
public float getPrice() {
return this.price;
}
public void setPrice(float price) {
this.price = price;
}


@Override
public int oneLess(){
if (number!= 0){
Expand Down
29 changes: 29 additions & 0 deletions src/main/java/shopManager/PriceComparator.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
package shopManager;

import shopManager.MyProduct;
import shopManager.Product;

/**
* @author Guillermo Roche
*
*/

public class PriceComparator {
int compare (MyProduct producto1, MyProduct producto2) {
float precio1 = 0;
float precio2 = 0;
int resultado = 0;
precio1 = producto1.getPrice();
precio2 = producto2.getPrice();
if(precio1 > precio2) {
resultado = -1;
}
else if (precio2 > precio1) {
resultado = 1;
}
else {
resultado = 0;
}
return resultado;
}
}
41 changes: 41 additions & 0 deletions src/test/java/shopManager/PrizeComparatorTest.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
/**
*
*/
package shopManager;

import static java.lang.System.*;
import static org.junit.Assert.*;
import static org.junit.jupiter.api.Assertions.assertTrue;

import org.junit.Before;
import org.junit.jupiter.api.BeforeAll;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.DisplayName;
import org.junit.jupiter.api.RepeatedTest;
import org.junit.jupiter.api.Tag;
import org.junit.jupiter.api.Test;

import shopManager.MyProduct;
import shopManager.PriceComparator;

class PrizeComparatorTest {
@Before
@Test
@Tag("unidad")
@DisplayName("Prueba del metodo que compara dos precios")
void testCompare() {
MyProduct producto1 = new MyProduct("Producto1");
MyProduct producto2 = new MyProduct("Producto2");
PriceComparator comparador = new PriceComparator();
producto1.setPrice(3);
producto2.setPrice(3);
//Probamos el caso de precios de igual producto
assertEquals("El comparador no detecta que los precios son iguales", 0, comparador.compare(producto1, producto2));
//Probamos el caso de que el segundo precio sea mayor
producto2.setPrice(4);
assertTrue(comparador.compare(producto1, producto2)>0,"El comparador no detecta que el segundo precio es mayor");
//Probamos el caso de que el primer precio sea mayor
assertTrue(comparador.compare(producto2, producto1)<0,"El comparador no detecta que el primer precio es mayor");
}

}