Doubt about data distribution between classes

Asked

Viewed 110 times

0

I have a class called Socio who asks for his name, phone number... and I devised methods getters and setters. And another class called DadosDoUsuario who will ask for the information to fill out the register, but what happens is that in this class DadosDoUsuario is receiving user information and entering in the database.

I’d like to do another class InsereNoBanco receiving the data entered by the user... That is, the class DadosDoUsuario would only receive the information and pass to the class InsereNoBanco. You could give me some tips, because I have tried in every way and it is complicated. I thank you already. Thank you!

public class Socio {

    private Long codigo;
    private String nome;
    private Integer telefone;
    private Integer ddd;
    private String email;
    private String cpf;

    public Long getCodigo() {
        return codigo;
    }

    public void setCodigo(Long codigo) {
        this.codigo = codigo;
    }

    public String getNome() {
        return nome;
    }

    public void setNome(String nome) throws NomeUsuarioNaoInformado {
        if (nome.equals(null) || nome.equals("")) {
            throw new NomeUsuarioNaoInformado();
        }

        this.nome = nome;
    }

    public Integer getTelefone() {
        return telefone;
    }

    public void setTelefone(Integer telefone) {
        this.telefone = telefone;
    }

    public Integer getDdd() {
        return ddd;
    }

    public void setDdd(Integer ddd) {
        this.ddd = ddd;
    }

    public String getEmail() {
        return email;
    }

    public void setEmail(String email) {
        this.email = email;
    }

    public String getCpf() {
        return cpf;
    }

    public void setCpf(String cpf) {
        this.cpf = cpf;
    }
}
public class DadosUsuario {

    static Scanner scan = new Scanner(System.in);

    public void cadastrarUsuario() throws NomeUsuarioNaoInformado {

        Socio cadastrarUser = new Socio();

        System.out.println("Informe um nome: ");
        cadastrarUser.setNome(scan.nextLine());

        System.out.println("Informe um telefone: ");
        cadastrarUser.setTelefone(scan.nextInt());

        System.out.println("Informe o DDD: ");
        cadastrarUser.setDdd(scan.nextInt());

        System.out.println("Informe o email: ");
        cadastrarUser.setEmail(scan.next());

        System.out.println("Informe o cpf: ");
        cadastrarUser.setCpf(scan.next());

        SocioDAO dao = new SocioDAO();
        try {
            dao.salvarSocio(cadastrarUser);
            System.out.println("USUÁRIO CADASTRADO COM SUCESSO.");
        } catch (SQLException e) {
            System.out.println("ERRO AO CADASTRAR USUÁRIO.");
            //e.printStackTrace();
        }
    }
}
public class SocioDAO {

    public void salvarSocio(Socio socio) throws SQLException {
        StringBuilder sql = new StringBuilder();
        sql.append("INSERT INTO socio ");
        sql.append("(nome, telefone, ddd, email, cpf) ");
        sql.append("VALUES (?, ?, ?, ?, ?) ");

        Connection conexao = ConexaoFactory.conectar();

        PreparedStatement comando = conexao.prepareStatement(sql.toString());

        comando.setString(1, socio.getNome());
        comando.setInt(2, socio.getTelefone());
        comando.setInt(3, socio.getDdd());
        comando.setString(4, socio.getEmail());
        comando.setString(5, socio.getCpf());

        comando.executeUpdate();

    }
}
  • 1

    I cannot understand what the purpose of the class would be InsereNoBanco. She looks to me like she’d be a DAO, but you already have one DAO and is already using it.

  • Related: http://answall.com/q/63765/132

  • It’s for layered programming and I’m having a hard time understanding this... So I wanted to create more Dao class just to receive the information and not to receive the information typed by the user and enter to the bank at the same time.... got it?

  • The class Dadosusuario is getting user entries and the class "Sociodao" is entering this data in the database. You can say that Partner belongs to the layer Business or Dominion, Dadosusuario belongs to the layer Applying and Sociopath belongs to the layer Infra. The names can be improved, but your code is already well divided into layers. What in particular you didn’t like in your code?

  • Blz... is that the teacher said to give an improved one... But, I found that it was well distributed, anyway... Thanks... I thought it was out of order standard... THANK YOU...

No answers

Browser other questions tagged

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