Untitled

mail@pastecode.io avatar
unknown
java
a year ago
5.1 kB
2
Indexable
Never
package main;

public class Atm {
  private List<DTOTransaction> transacciones = new ArrayList<DTOTransaction>();
  private float balance;
  private String serialNumber;
  public Atm() {
    super();
    this.balance = 10000f;
    this.serialNumber = UUID.randomUUID().toString();
    DTOTransaction transaccion = new DTOTransaction(
        "Creacion de cuenta con saldo", getBalance(), getSerialNumber());
    transacciones.add(transaccion);
  }
  public Atm(float balance) {
    super();
    this.balance = balance;
  }
  public float getBalance() {
    return balance;
  }
  public void setBalance(float balance) {
    this.balance = balance;
  }
  public String getSerialNumber() {
    return serialNumber;
  }
  public void setSerialNumber(String serialNumber) {
    this.serialNumber = serialNumber;
  }
  public List<DTOTransaction> getTransacciones() {
    return transacciones;
  }
  public void setTransacciones(List<DTOTransaction> transacciones) {
    this.transacciones = transacciones;
  }
  public void deposito(float amount) throws Exception {
    if (amount <= 0)
      throw new Exception("Monto invalido, monto debe ser mayor a 0");
    setBalance(getBalance() + amount);
    DTOTransaction transaccion =
        new DTOTransaction("Retiro", amount, getSerialNumber());
    transacciones.add(0, transaccion);
  }
  public void retiro(float amount) throws Exception {
    if (amount > getBalance())
      throw new Exception("Monto invalido, monto mayor al disponible");
    else if (amount <= 0)
      throw new Exception("Monto invalido, monto debe ser mayor a 0");
    setBalance(getBalance() - amount);
    DTOTransaction transaccion =
        new DTOTransaction("Retiro", amount, getSerialNumber());
    transacciones.add(0, transaccion);
  }
  public void mostrarSaldo() {
    System.out.println("Saldo disponible: $" + getBalance());
  }
  public void mostrarTransacciones() {
    for (DTOTransaction dtoTransaccion : getTransacciones()) {
      System.out.println(dtoTransaccion.toString());
    }
  }
}
package main;
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
public class Main {
  public static void main(String[] args) {
    try ( // TODO Auto-generated method stub
        Scanner sc = new Scanner(System.in)) {
      int opcion;
      System.out.println("Bienvenido");
      Atm atm = new Atm();
      do {
        System.out.println("-------------Menú------------");
        System.out.println("1. Retiro");
        System.out.println("2. Deposito");
        System.out.println("3. Saldo");
        System.out.println("4. Movimientos");
        System.out.println("9. Salir");
        System.out.println("Seleccione: ");
        opcion = Integer.valueOf(sc.nextLine());
        switch (opcion) {
          case 1:
            System.out.println("Retiro");
            System.out.println("Ingresa monto a retirar:");
            System.out.print("$");
            float montoRetiro = Float.parseFloat(sc.nextLine());
            try {
              atm.retiro(montoRetiro);
            } catch (Exception e) {
              e.printStackTrace();
              System.out.println(e.getMessage());
            }
            break;
          case 2:
            System.out.println("Deposito");
            System.out.println("Ingresa monto a depositar:");
            System.out.print("$");
            try {
              float montoDeposito = Float.parseFloat(sc.nextLine());
              atm.retiro(montoDeposito);
            } catch (Exception e) {
              e.printStackTrace();
              System.out.println(e.getMessage());
            }
            break;
          case 3:
            atm.mostrarSaldo();
            break;
          case 4:
            atm.mostrarTransacciones();
            break;
          case 9:
            System.out.println("Hasta la proxima.");
            break;
          default:
            System.out.println("Seleccione una opcion valida.");
        }
        System.out.println("");
      } while (opcion != 9);
    } catch (Exception e) {
      e.printStackTrace();
      System.out.println(e.getMessage());
    }
  }
}
package main;
import java.util.ArrayList;
import java.util.List;
import java.util.Scanner;
import java.util.UUID;

public class DTOTransaction {
  DateTimeFormatter dtf = DateTimeFormatter.ofPattern("uuuu/MM/dd HH:mm:ss");

  private String serialNumber;
  private String fechaTransaccion;
  private String movimiento;
  private float monto;

  public DTOTransaction(String movimiento, float monto, String serialNumber) {
    super();
    LocalDateTime now = LocalDateTime.now();
    this.fechaTransaccion = dtf.format(now);
    this.movimiento = movimiento;
    this.monto = monto;
    this.serialNumber = serialNumber;
  }

  @Override
  public String toString() {
    return this.serialNumber + " " + this.fechaTransaccion + " - "
        + this.movimiento + " de $" + this.monto;
  }
}