(Business rule) -Repeated registration

Asked

Viewed 120 times

0

How do I make this business rule that prevents the user from registering an account with the same number and agency ?

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, null, null)) {
        JOptionPane.showMessageDialog(null, "Gravado Com Sucesso!");
    } else {
        JOptionPane.showMessageDialog(null, "Não foi possível Gravar!");
    }
}


      public static void menu() {
                String opcoes = "1-Cadastrar

                int op = 0;
                do {
                    op = new Integer(JOptionPane.showInputDialog(opcoes));
                    switch (op) {
                    case 1:
                        cadastrar();
                        break;

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 ContaBancariaBean pesquisa(String numero, String agencia) {
        for (ContaBancariaBean item : vetor) {
            if ((item != null) && (item.getNumero().equals(numero))
                    && (item.getAgencia().equals(agencia))) {
                return item;
            }
        }
        return null;
    }

BEAN-CLASS

package modeloContaBancaria;

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


    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";
    }


}
  • Your code doesn’t even compile because the ContaBancariaDao.inserir(conta) passes only one parameter, while the method has three parameters in its signature.

  • The compiler reports this problem, tells you to add arguments, but when I click to add it fills the rest with null

  • I think my problem is in the same DAO

  • I’ll put the whole code in so you can understand the situation

  • ------Done--------

  • I just want that when registering the same number and agency the user is warned

  • Your code still does not compile for the same reason: ContaBancariaDao.inserir(conta) passes a parameter whereas the method has three in its signature. Note that without solving this problem your code will never work.

  • I know that but I want to know if my method insert is correct, if the business rule is correct, probably not

  • anyway I’ll fix my method by registering the way the compiler suggested

Show 4 more comments

2 answers

1

Your code is correct. What is not is the way you work with the parameters.

You can do it like this:

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.getNumer(), conta.getAgencia())) {
        JOptionPane.showMessageDialog(null, "Gravado Com Sucesso!");
    } else {
        JOptionPane.showMessageDialog(null, "Não foi possível Gravar!");
    }
}

Note that as you do not pass the agency and number parameters for comparison, there will never be a true comparison where it checks whether these values are repeated.

1


You can change the line that is like this ContaBancariaDao.inserir(conta, null, null) for something like ContaBancariaDao.inserir(conta, conta.getNumero(), conta.getAgencia()). But the best thing would be to change the signature of the method in Bankroll or create a new method like this public static boolean inserir(ContaBancariaBean conta) {...}

Browser other questions tagged

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