(Business rule) - Update balance

Asked

Viewed 270 times

0

  • I am developing a business rule in which the user when typing the number and agency from the bank account, he has two options:

    1-Credit: The user will type char 'c' through Jtextfield credit equivalent and this will add to your balance with your value that it requires.

    2-Debit: User will type char’d through Jtextfield debt equivalent and this will decrease to your balance with the respective value that it requires.

  • So the logic I’ve developed in the DAO class, now my greatest
    difficulty is in calling the update method,
    so I ask for your help.

MAIN CLASS

public class Main {



    public static void main(String[] args) {
            menu();
        }

        public static ContaBancariaBean gerarConta() {
            ContaBancariaBean e = new ContaBancariaBean();
            e.setNumero(JOptionPane.showInputDialog("Número: "));
            e.setAgencia(JOptionPane.showInputDialog("Agência: "));
            e.setLimite(new Integer(JOptionPane.showInputDialog("Limite: ")));
            e.setSaldo(new Integer(JOptionPane.showInputDialog("Saldo: ")));
            return e;

        }

        public static void cadastrar() {
            ContaBancariaBean conta = gerarConta();
            if (ContaBancariaDao.inserir(conta, conta.getNumero(), conta.getAgencia())) {
                JOptionPane.showMessageDialog(null, "Gravado Com Sucesso!");
            } else {
                JOptionPane.showMessageDialog(null, "Não foi possível Gravar!");
            }
        }
        public static void listarConta() {
            String dados = ContaBancariaDao.listar();
            TextArea ta = new TextArea();
            ta.setEditable(false);
            ta.setText(dados);
            JOptionPane.showMessageDialog(null, ta);
        }



        public static void alterarSaldo() {
            String numero = JOptionPane.showInputDialog("Número:");
            String agencia = JOptionPane.showInputDialog("Agência:");
            String valor = JOptionPane.showInputDialog("Valor:");
            String operacao = JOptionPane.showInputDialog("Operação:");
            ContaBancariaBean e = ContaBancariaDao.pesquisa(numero,agencia);
            if (e == null)
                JOptionPane.showMessageDialog(null,
                        "Agência ou número Inexistente!");
            else {
                e.setSaldo(new Integer(JOptionPane.showInputDialog("Número: ",
                        e.getSaldo())));
            }e.setAgencia(JOptionPane.showInputDialog("Agência: ",
                    e.getAgencia()));

            if (ContaBancariaDao.atualizarSaldo(e, e.getSaldo()))

                    JOptionPane.showMessageDialog(null, "Atualizado");
                else
                    JOptionPane.showMessageDialog(null, "Não foi Atualizado");
            }




        public static void menu() {
            String opcoes = "1-Cadastrar\2-Alterar saldo"
                    + "\n3-Listar\n4-Sair";
            int op = 0;
            do {
                op = new Integer(JOptionPane.showInputDialog(opcoes));
                switch (op) {
                case 1:
                    cadastrar();
                    break;
                case 2:
                    alterarSaldo();
                    break;

                case 3:
                    listarConta();
                    break;

                }
            } while (op != 4);
        }

    }

DAO class

 import javax.swing.JOptionPane;

    public class ContaBancariaDao {

        private static final int QTDE = 10;
        private static ContaBancariaBean vetor[] = new ContaBancariaBean[QTDE];

        public static boolean inserir(ContaBancariaBean conta, String numero, String agencia) {
            for (ContaBancariaBean item : vetor) {
                if ((item != null) && (item.getNumero().equals(numero))
                        && (item.getAgencia().equals(agencia))) {
                    JOptionPane.showMessageDialog(null, "Mesmo número ou Agência!");
                    return false;
                } else {
                    for (int i = 0; i < QTDE; i++) {
                        if (vetor[i] == null) {
                            vetor[i] = conta;
                            return true;
                        }
                    }
                }
            }
            return false;
        }


        public static String listar() {
            String saida = "";
            for (int i = 0; i < QTDE; i++) {
                if (vetor[i] != null)
                    saida += vetor[i].toString();
            }
            return saida;
        }

public static boolean atualizarSaldo(ContaBancariaBean numero,
            ContaBancariaBean agencia, int valor, char operacao) {
        for (int i = 0; i < QTDE; i++) {
            if (vetor[i] != null) {
                if (vetor[i].getNumero().equals(numero)
                    && vetor[i].getAgencia().equals(agencia)) {
                if (operacao == 'c')
                        vetor[i].setSaldo(vetor[i].getSaldo() + valor);
                    else
                        vetor[i].setSaldo(vetor[i].getSaldo() - valor);
                    return true;
                }
            }
        }
        return false;
    }


    }

BEAN-CLASS

public class ContaBancariaBean {
    private String numero;
    private String agencia;
    private int saldo;
    private int limite;
    private int valor;
    private int operacao;


    public int getValor() {
        return valor;
    }
    public void setValor(int valor) {
        this.valor = valor;
    }
    public int getOperacao() {
        return operacao;
    }
    public void setOperacao(int operacao) {
        this.operacao = operacao;
    }
    public String getNumero() {
        return numero;
    }
    public void setNumero(String numero) {
        this.numero = numero;
    }
    public String getAgencia() {
        return agencia;
    }
    public void setAgencia(String agencia) {
        this.agencia = agencia;
    }
    public int getSaldo() {
        return saldo;
    }
    public void setSaldo(int saldo) {
        this.saldo = saldo;
    }
    public int getLimite() {
        return limite;
    }
    public void setLimite(int limite) {
        this.limite = limite;
    }
    public String toString(){
        return "Número: "+getNumero()+"\n"+
               "Agência: "+ getAgencia()+"\n"+
               "Limite: " +getLimite()+"\n"+
               "Saldo: "+getSaldo()+"\n";
    }

    public String getDados(){
        return getNumero()+","+
               getAgencia()+","+
               getSaldo()+","+
               getLimite()+"\n";
    }


}
  • Some problems I noticed were: You’re calling ContaBancariaDao.atualizar(...) while the name of your method in the class is atualizarSaldo(...). Still in class Dao, is using the method equals in incompatible types, i.e., the result of the comparison will never be true.

  • I’ve already fixed it, but my difficulty is in the alterarSaldo() method in the MAIN class

  • In fact both number and agency are Strings, so equals

  • What difficulty are you having? Can you explain better?

No answers

Browser other questions tagged

You are not signed in. Login or sign up in order to post.