Last active
August 29, 2015 14:07
-
-
Save earissola/1061c6556e6fa8f23c09 to your computer and use it in GitHub Desktop.
[POO] Clases Abstractas - Demo (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
public class CajaDeAhorros extends Cuenta { | |
public CajaDeAhorros(int numero, double saldo) { | |
super(numero, saldo); | |
} | |
@Override | |
public boolean extraerDinero(double importe) { | |
boolean resultadoOperacion; | |
if (importe <= saldo) { | |
this.actualizarSaldo(importe); | |
resultadoOperacion = true; | |
} else | |
resultadoOperacion = false; | |
return resultadoOperacion; | |
} | |
} |
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
public abstract class Cuenta { | |
protected int numero; | |
protected double saldo; | |
public Cuenta (int numero, double saldo) { | |
this.numero = numero; | |
this.saldo = saldo; | |
} | |
protected void actualizarSaldo(double importe) { | |
saldo -= importe; | |
} | |
public double consultarSaldo() { | |
return saldo; | |
} | |
public abstract boolean extraerDinero(double importe); | |
} |
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
public class CuentaCorriente extends Cuenta { | |
private double dineroAlDescubierto; | |
public CuentaCorriente (int numero, double saldo, double dineroAlDescubierto) { | |
super(numero, saldo); | |
this.dineroAlDescubierto = dineroAlDescubierto; | |
} | |
@Override | |
public boolean extraerDinero(double importe) { | |
boolean resultadoOperacion; | |
if (importe <= dineroAlDescubierto + saldo) { | |
actualizarSaldo(importe); | |
resultadoOperacion = true; | |
} else | |
resultadoOperacion = false; | |
return resultadoOperacion; | |
} | |
} |
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
import static org.junit.Assert.*; | |
import org.junit.Test; | |
public class TestClasesAbstractas { | |
@Test | |
public void test() { | |
/* ## Caja de Ahorro ## */ | |
Cuenta miCuentaDeAhorros = new CajaDeAhorros(5535, 2000.00); | |
/* Extraigo $1000 - Restan en la Cuenta $1000 */ | |
assertTrue(miCuentaDeAhorros.extraerDinero(1000.00)); | |
/* Extraigo otros $1000 - Restan en la Cuenta $0 */ | |
assertTrue(miCuentaDeAhorros.extraerDinero(1000.00)); | |
/* Mi Saldo es $0 */ | |
assertEquals(0, miCuentaDeAhorros.consultarSaldo(), 0); | |
/* Intento extraer $1000 pero el Saldo es $0 */ | |
assertFalse(miCuentaDeAhorros.extraerDinero(1000.00)); | |
/* ## Cuenta Corriente ## */ | |
Cuenta miCuentaCorriente = new CuentaCorriente(6538, 1000.00, 2000); | |
/* Extraigo $1000 */ | |
assertTrue(miCuentaCorriente.extraerDinero(1000.00)); | |
/* Mi saldo es $0 pero aún tengo un límite de $2000 para dinero al descubierto */ | |
assertEquals(0, miCuentaCorriente.consultarSaldo(), 0); | |
/* Extraigo $2000 - Alcanzé mi límite al descubierto */ | |
assertTrue(miCuentaCorriente.extraerDinero(2000.00)); | |
/* Mi Saldo es -$2000 */ | |
assertEquals(-2000, miCuentaCorriente.consultarSaldo(), 0.00); | |
/* Intento extraer $1000 pero mi saldo es $0 y superé mi límite al descubierto */ | |
assertFalse(miCuentaCorriente.extraerDinero(1000.00)); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment