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.– EduardoFernandes
The compiler reports this problem, tells you to add arguments, but when I click to add it fills the rest with null
– Marcelo T. Cortes
I think my problem is in the same DAO
– Marcelo T. Cortes
I’ll put the whole code in so you can understand the situation
– Marcelo T. Cortes
------Done--------
– Marcelo T. Cortes
I just want that when registering the same number and agency the user is warned
– Marcelo T. Cortes
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.– EduardoFernandes
I know that but I want to know if my method insert is correct, if the business rule is correct, probably not
– Marcelo T. Cortes
anyway I’ll fix my method by registering the way the compiler suggested
– Marcelo T. Cortes