Skip to content

Instantly share code, notes, and snippets.

@parzibyte
Created April 20, 2021 19:47
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save parzibyte/02ef33b758ecffad3dde78623748c124 to your computer and use it in GitHub Desktop.
Save parzibyte/02ef33b758ecffad3dde78623748c124 to your computer and use it in GitHub Desktop.
import java.util.ArrayList;
import java.util.List;
abstract class Dinero {
private double dinero;
private String description;
public double getDinero() {
return dinero;
}
public void setDinero(double dinero) {
this.dinero = dinero;
}
public String getDescription() {
return description;
}
public void setDescription(String description) {
this.description = description;
}
}
class Gasto extends Dinero {
public Gasto(double gasto, String description) {
this.setDinero(gasto);
this.setDescription(description);
}
@Override
public String toString() {
return this.getDescription() + " " + this.getDinero();
}
}
class Ingreso extends Dinero {
public Ingreso(double ingreso, String description) {
this.setDinero(ingreso);
this.setDescription(description);
}
@Override
public String toString() {
return this.getDescription() + " " + this.getDinero();
}
}
class Usuario {
private String nombre;
private int edad;
private String DNI;
public Usuario() {
}
public String getNombre() {
return nombre;
}
public void setNombre(String nombre) {
this.nombre = nombre;
}
public int getEdad() {
return edad;
}
public void setEdad(int edad) {
this.edad = edad;
}
public String getDNI() {
return DNI;
}
public boolean setDNI(String DNI) {
// Solo puede medir 9 (sin guión) o 10 (con guión)
if (DNI.length() != 9 && DNI.length() != 10) {
return false;
}
// Si mide 10, debe tener un guión en la posición 8
if (DNI.length() == 10) {
// Extraerlo y regresar false si no es un guión
String posibleGuion = String.valueOf(DNI.charAt(8));
if (!posibleGuion.equals("-")) {
return false;
}
}
// Hasta ahora sabemos que mide 9 o 10 y que el guión es válido (esté presente o no)
String primerosOcho = DNI.substring(0, 8);
String ultimo = String.valueOf(DNI.charAt(DNI.length() - 1));
// Comprobar que los primeros 8 sean numéricos
if (!primerosOcho.matches("[0-9]+")) {
return false;
}
// Comprobar que el último sea una letra
if (!ultimo.matches("[A-Z]+")) {
return false;
}
// Si llegamos hasta aquí y no regresamos arriba, entonces el DNI es válido
this.DNI = DNI;
return true;
}
@Override
public String toString() {
return "Usuario{" +
"nombre='" + nombre + '\'' +
", edad=" + edad +
", DNI='" + DNI + '\'' +
'}';
}
}
class Cuenta {
private double saldo;
private Usuario usuario;
private List<Gasto> gastos;
private List<Ingreso> ingresos;
public Cuenta(Usuario usuario) {
this.usuario = usuario;
this.saldo = 0;
this.gastos = new ArrayList<>();
this.ingresos = new ArrayList<>();
}
public double getSaldo() {
return saldo;
}
public void setSaldo(double saldo) {
this.saldo = saldo;
}
public Usuario getUsuario() {
return usuario;
}
public void setUsuario(Usuario usuario) {
this.usuario = usuario;
}
public double addIngresos(String description, double cantidad) {
this.setSaldo(this.getSaldo() + cantidad);
this.ingresos.add(new Ingreso(cantidad, description));
return this.getSaldo();
}
public double addGastos(String description, double cantidad) throws GastoException {
if (this.getSaldo() < cantidad) {
throw new GastoException();
}
this.setSaldo(this.getSaldo() - cantidad);
this.gastos.add(new Gasto(cantidad, description));
return this.getSaldo();
}
public List<Gasto> getGastos() {
return gastos;
}
public List<Ingreso> getIngresos() {
return ingresos;
}
@Override
public String toString() {
return "Cuenta{" +
"saldo=" + saldo +
", usuario=" + usuario +
", gastos=" + gastos +
", ingresos=" + ingresos +
'}';
}
}
class GastoException extends Exception {
public GastoException() {
}
}
public class Main {
public static void main(String[] args) {
Usuario usuario = new Usuario();
usuario.setNombre("Luis");
usuario.setEdad(24);
boolean ok = usuario.setDNI("12345678A");
Cuenta cuenta = new Cuenta(usuario);
System.out.println(cuenta);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment