-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
166dadd
commit 00321e7
Showing
19 changed files
with
523 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
## Monedero | ||
|
||
### Contexto | ||
|
||
Este repositorio contiene el código de un _monedero virtual_, al que podemos agregarle y quitarle dinero, a través | ||
de los métodos `Monedero.sacar` y `Monedero.poner`, respectivamente. | ||
Pero hay algunos problemas: por un lado el código no está muy bien testeado, y por el otro, hay numeros _code smells_. | ||
|
||
### Consigna | ||
|
||
Tenés seis tareas: | ||
|
||
1. :fork_and_knife: Hacé un _fork_ de este repositorio (presionando desde Github el botón Fork) | ||
2. :arrow_down: Descargalo y construí el proyecto, utilizando `maven` | ||
2. :nose: Identificá y anotá todos los _code smells_ que encuentres | ||
3. :test_tube: Agregá los tests faltantes y mejorá los existentes. | ||
* :eyes: Ojo: ¡un test sin ningún tipo de aserción está incompleto! | ||
4. :rescue_worker_helmet: Corregí smells, de a un commit por vez. | ||
5. :arrow_up: Subí todos los cambios a tu _fork_ | ||
|
||
### Tecnologías usadas | ||
|
||
* Java 8. | ||
* JUnit 5. :warning: La versión 5 de JUnit es la más nueva del framework y presenta algunas diferencias respecto a la versión "clásica" (JUnit 4). Para mayores detalles, ver: | ||
* [Apunte de herramientas](https://docs.google.com/document/d/1VYBey56M0UU6C0689hAClAvF9ILE6E7nKIuOqrRJnWQ/edit#heading=h.dnwhvummp994) | ||
* [Entrada de Blog (en inglés)](https://www.baeldung.com/junit-5-migration) | ||
* [Entrada de Blog (en español)](https://www.paradigmadigital.com/dev/nos-espera-junit-5/) | ||
* Maven 3.3 o superior | ||
|
||
|
||
|
||
|
||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,44 @@ | ||
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" | ||
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> | ||
<modelVersion>4.0.0</modelVersion> | ||
|
||
<groupId>dds</groupId> | ||
<artifactId>dds.monedero</artifactId> | ||
<version>1.0-SNAPSHOT</version> | ||
<packaging>jar</packaging> | ||
|
||
<name>dds.monedero</name> | ||
<url>https://github.com/dds-utn</url> | ||
|
||
<properties> | ||
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding> | ||
</properties> | ||
|
||
<build> | ||
<plugins> | ||
<plugin> | ||
<groupId>org.apache.maven.plugins</groupId> | ||
<artifactId>maven-surefire-plugin</artifactId> | ||
<version>3.0.0-M5</version> | ||
</plugin> | ||
<plugin> | ||
<groupId>org.apache.maven.plugins</groupId> | ||
<artifactId>maven-compiler-plugin</artifactId> | ||
<version>3.1</version> | ||
<configuration> | ||
<source>1.8</source> | ||
<target>1.8</target> | ||
</configuration> | ||
</plugin> | ||
</plugins> | ||
</build> | ||
|
||
<dependencies> | ||
<dependency> | ||
<groupId>org.junit.jupiter</groupId> | ||
<artifactId>junit-jupiter-engine</artifactId> | ||
<version>5.7.0</version> | ||
<scope>test</scope> | ||
</dependency> | ||
</dependencies> | ||
</project> |
9 changes: 9 additions & 0 deletions
9
...ture/monedero/src/main/java/dds/monedero/exceptions/MaximaCantidadDepositosException.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
package dds.monedero.exceptions; | ||
|
||
public class MaximaCantidadDepositosException extends RuntimeException { | ||
|
||
public MaximaCantidadDepositosException(String message) { | ||
super(message); | ||
} | ||
|
||
} |
7 changes: 7 additions & 0 deletions
7
...cture/monedero/src/main/java/dds/monedero/exceptions/MaximoExtraccionDiarioException.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
package dds.monedero.exceptions; | ||
|
||
public class MaximoExtraccionDiarioException extends RuntimeException { | ||
public MaximoExtraccionDiarioException(String message) { | ||
super(message); | ||
} | ||
} |
7 changes: 7 additions & 0 deletions
7
03-lecture/monedero/src/main/java/dds/monedero/exceptions/MontoNegativoException.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
package dds.monedero.exceptions; | ||
|
||
public class MontoNegativoException extends RuntimeException { | ||
public MontoNegativoException(String message) { | ||
super(message); | ||
} | ||
} |
7 changes: 7 additions & 0 deletions
7
03-lecture/monedero/src/main/java/dds/monedero/exceptions/SaldoMenorException.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
package dds.monedero.exceptions; | ||
|
||
public class SaldoMenorException extends RuntimeException { | ||
public SaldoMenorException(String message) { | ||
super(message); | ||
} | ||
} |
141 changes: 141 additions & 0 deletions
141
03-lecture/monedero/src/main/java/dds/monedero/model/Cuenta.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,141 @@ | ||
package dds.monedero.model; | ||
|
||
import dds.monedero.exceptions.MaximaCantidadDepositosException; | ||
import dds.monedero.exceptions.MaximoExtraccionDiarioException; | ||
import dds.monedero.exceptions.MontoNegativoException; | ||
import dds.monedero.exceptions.SaldoMenorException; | ||
|
||
import java.time.LocalDate; | ||
import java.util.ArrayList; | ||
import java.util.List; | ||
|
||
public class Cuenta { | ||
|
||
private double limite = 1000; | ||
private int maximosDepositos = 3; | ||
private double saldo; | ||
private List<Movimiento> movimientos = new ArrayList<>(); | ||
|
||
/** | ||
* Verfica si se supera el monto de extaccion diaria. | ||
* | ||
* @param cuanto el monto | ||
* @param limite el limite de extaccion | ||
* @throws MaximoExtraccionDiarioException si supera el límite | ||
*/ | ||
private void verificarExtraccionDiaria(double cuanto) { | ||
double limite = getLimiteActual(); | ||
|
||
if (cuanto > limite) { | ||
throw new MaximoExtraccionDiarioException( | ||
"No puede extraer mas de $ " + this.limite + " diarios, límite: " + limite); | ||
} | ||
} | ||
|
||
/** | ||
* Valida si puede realizarse la extaccion. | ||
* | ||
* @param cuanto el monto | ||
* @throws SaldoMenorException cuando se desea sacar mas que el el saldo. | ||
*/ | ||
private void verificarSaldoMenor(double cuanto) { | ||
if (getSaldo() - cuanto < 0) { | ||
throw new SaldoMenorException("No puede sacar mas de " + getSaldo() + " $"); | ||
} | ||
} | ||
|
||
/** | ||
* Valia si se encuentra dentro de la Maxima cantidad de depósitos. | ||
* | ||
* @throws MaximaCantidadDepositosException cuando excede cierto numeros. | ||
*/ | ||
private void verificarMaximaCantidadDepositos() { | ||
if (contarDepositos() >= this.maximosDepositos) { | ||
throw new MaximaCantidadDepositosException( | ||
"Ya excedio los " + this.maximosDepositos + " depositos diarios"); | ||
} | ||
} | ||
|
||
/** | ||
* Verifica si el monto es negativo. | ||
* | ||
* @param cuanto el monto a verificar | ||
* @throws MontoNegativoException si el monto es menor a 0. | ||
*/ | ||
private void verificarMontoNegativo(double cuanto) { | ||
if (cuanto <= 0) { | ||
throw new MontoNegativoException(cuanto + ": el monto a ingresar debe ser un valor positivo"); | ||
} | ||
} | ||
|
||
public Cuenta() { | ||
this(0); | ||
} | ||
|
||
public Cuenta(double montoInicial) { | ||
saldo = montoInicial; | ||
} | ||
|
||
/** | ||
* Cuenta la cantidad de movimientos que son depositos. | ||
* | ||
* @return la cantidad de depositos. | ||
*/ | ||
public long contarDepositos() { | ||
return getMovimientos().stream().filter(movimiento -> movimiento.fueDepositado(LocalDate.now())) | ||
.count(); | ||
} | ||
|
||
private void realizarOperacion(Movimiento movimiento, Runnable verificacion) { | ||
verificarMontoNegativo(movimiento.getMonto()); | ||
verificacion.run(); | ||
agregarMovimiento(movimiento); | ||
} | ||
|
||
public void poner(double cuanto) { | ||
|
||
realizarOperacion(new Deposito(LocalDate.now(), cuanto), | ||
() -> verificarMaximaCantidadDepositos()); | ||
} | ||
|
||
public void sacar(double cuanto) { | ||
|
||
realizarOperacion(new Extraccion(LocalDate.now(), cuanto), () -> { | ||
verificarSaldoMenor(cuanto); | ||
verificarExtraccionDiaria(cuanto); | ||
}); | ||
|
||
} | ||
|
||
public void agregarMovimiento(Movimiento movimiento) { | ||
movimientos.add(movimiento); | ||
this.saldo += movimiento.getSaldo(); | ||
} | ||
|
||
public double getMontoExtraidoA(LocalDate fecha) { | ||
return getMovimientos().stream().filter(movimiento -> movimiento.fueExtraido(fecha)) | ||
.mapToDouble(Movimiento::getMonto).sum(); | ||
} | ||
|
||
public List<Movimiento> getMovimientos() { | ||
return movimientos; | ||
} | ||
|
||
/** | ||
* Obtiene el límite de extaccion restante | ||
* | ||
* @return el limite actual | ||
*/ | ||
public double getLimiteActual() { | ||
return this.limite - getMontoExtraidoA(LocalDate.now()); | ||
} | ||
|
||
public double getSaldo() { | ||
return saldo; | ||
} | ||
|
||
public void setSaldo(double saldo) { | ||
this.saldo = saldo; | ||
} | ||
|
||
} |
25 changes: 25 additions & 0 deletions
25
03-lecture/monedero/src/main/java/dds/monedero/model/Deposito.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
package dds.monedero.model; | ||
|
||
import java.time.LocalDate; | ||
|
||
public class Deposito extends Movimiento { | ||
|
||
public Deposito(LocalDate fecha, double monto) { | ||
super(fecha, monto); | ||
} | ||
|
||
@Override | ||
public boolean fueExtraido(LocalDate fecha) { | ||
return false; | ||
} | ||
|
||
@Override | ||
public boolean fueDepositado(LocalDate fecha) { | ||
return esDeLaFecha(fecha); | ||
} | ||
|
||
@Override | ||
public double getSaldo() { | ||
return this.getMonto(); | ||
} | ||
} |
25 changes: 25 additions & 0 deletions
25
03-lecture/monedero/src/main/java/dds/monedero/model/Extraccion.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
package dds.monedero.model; | ||
|
||
import java.time.LocalDate; | ||
|
||
public class Extraccion extends Movimiento { | ||
|
||
public Extraccion(LocalDate fecha, double monto) { | ||
super(fecha, monto); | ||
} | ||
|
||
@Override | ||
public boolean fueExtraido(LocalDate fecha) { | ||
return esDeLaFecha(fecha); | ||
} | ||
|
||
@Override | ||
public boolean fueDepositado(LocalDate fecha) { | ||
return false; | ||
} | ||
|
||
@Override | ||
public double getSaldo() { | ||
return -this.getMonto(); | ||
} | ||
} |
44 changes: 44 additions & 0 deletions
44
03-lecture/monedero/src/main/java/dds/monedero/model/Movimiento.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,44 @@ | ||
package dds.monedero.model; | ||
|
||
import java.time.LocalDate; | ||
|
||
public abstract class Movimiento { | ||
private LocalDate fecha; | ||
// En ningún lenguaje de programación usen jamás doubles para modelar dinero en el mundo real | ||
// siempre usen numeros de precision arbitraria, como BigDecimal en Java y similares | ||
private double monto; | ||
|
||
public Movimiento(LocalDate fecha, double monto) { | ||
this.fecha = fecha; | ||
this.monto = monto; | ||
} | ||
|
||
public double getMonto() { | ||
return monto; | ||
} | ||
|
||
public abstract double getSaldo(); | ||
|
||
public LocalDate getFecha() { | ||
return fecha; | ||
} | ||
|
||
public Movimiento setFecha(LocalDate fecha) { | ||
this.fecha = fecha; | ||
return this; | ||
} | ||
|
||
public Movimiento setMonto(double monto) { | ||
this.monto = monto; | ||
return this; | ||
} | ||
|
||
public abstract boolean fueDepositado(LocalDate fecha); | ||
|
||
public abstract boolean fueExtraido(LocalDate fecha); | ||
|
||
public boolean esDeLaFecha(LocalDate fecha) { | ||
return this.fecha.equals(fecha); | ||
} | ||
|
||
} |
Oops, something went wrong.