Commit 45a42a3c authored by Josue's avatar Josue

Patron Command

parent 733d5118
Pipeline #324 failed with stages
package org.example.designpatterns.command;
import org.example.designpatterns.command.commands.Cuenta;
import org.example.designpatterns.command.commands.DepositarImpl;
import org.example.designpatterns.command.commands.Invoker;
import org.example.designpatterns.command.commands.RetirarImpl;
public class App {
public static void main(String[] args) {
Cuenta cuenta = new Cuenta(1, 200);
DepositarImpl depositar = new DepositarImpl(cuenta, 100);
RetirarImpl retirar = new RetirarImpl(cuenta, 50);
Invoker ivk = new Invoker();
ivk.recibirOperacion(depositar);
ivk.recibirOperacion(retirar);
ivk.realizarOperaciones();
}
}
package org.example.designpatterns.command.commands;
public class Cuenta {
private int id;
private double saldo;
public Cuenta(int id, double saldo) {
this.id = id;
this.saldo = saldo;
}
public void retirar(double monto) {
this.saldo = this.saldo - monto;
System.out.println("[COMANDO RETIRAR] Cuenta: " + id + " Saldo: " + this.saldo);
}
public void depositar(double monto) {
this.saldo = this.saldo + monto;
System.out.println("[COMANDO DEPOSITAR] Cuenta: " + id + " Saldo: " + this.saldo);
}
}
package org.example.designpatterns.command.commands;
public class DepositarImpl implements IOperation{
private Cuenta cuenta;
private double monto;
public DepositarImpl(Cuenta cuenta, double monto) {
this.cuenta = cuenta;
this.monto = monto;
}
@Override
public void execute() {
this.cuenta.depositar(this.monto);
}
}
package org.example.designpatterns.command.commands;
@FunctionalInterface
public interface IOperation {
void execute();
}
package org.example.designpatterns.command.commands;
import java.util.ArrayList;
import java.util.List;
public class Invoker {
private List<IOperation> operations = new ArrayList<>();
public void recibirOperacion(IOperation operation) {
this.operations.add(operation);
}
public void realizarOperaciones() {
this.operations.forEach(IOperation::execute);
this.operations.clear();
}
}
package org.example.designpatterns.command.commands;
public class RetirarImpl implements IOperation{
private Cuenta cuenta;
private double monto;
public RetirarImpl(Cuenta cuenta, double monto) {
this.cuenta = cuenta;
this.monto = monto;
}
@Override
public void execute() {
this.cuenta.retirar(this.monto);
}
}
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment