-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsimulacionBanco.java
More file actions
49 lines (37 loc) · 1.31 KB
/
simulacionBanco.java
File metadata and controls
49 lines (37 loc) · 1.31 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
class FondosInsuficientesException extends Exception {
public FondosInsuficientesException(String mensaje) {
super(mensaje);
}
}
// Clase CuentaBancaria
class CuentaBancaria {
private double saldo;
public CuentaBancaria(double saldoInicial) {
this.saldo = saldoInicial;
}
public double getSaldo() {
return saldo;
}
public void retirar(double cantidad) throws FondosInsuficientesException {
if (cantidad > saldo) {
throw new FondosInsuficientesException("Fondos insuficientes. Tu saldo actual es: " + saldo);
}
saldo -= cantidad;
System.out.println("Has retirado: " + cantidad + ". Tu saldo actual es: " + saldo);
}
}
// Clase principal para probar la simulación
public class simulacionBanco {
public static void main(String[] args) {
CuentaBancaria cuenta = new CuentaBancaria(1000.0);
System.out.println("Saldo inicial: " + cuenta.getSaldo());
try {
System.out.println("Intentando retirar 500...");
cuenta.retirar(500);
System.out.println("Intentando retirar 600...");
cuenta.retirar(600);
} catch (FondosInsuficientesException e) {
System.out.println("Error: " + e.getMessage());
}
}
}