Electronic voting systems in Java

Asked

Viewed 3,602 times

2

I am making an electronic ballot box that register, list and the user can vote for the candidate, I can already register and list candidates, but how do I always count +1 in an attribute votes of each candidate in a ArrayList?

        ArrayList<Candidato>listacandi;
                    listacandi = new ArrayList<Candidato>();
                    Scanner e= new Scanner(System.in);
                    int op;

        do {
            System.out.println("Cadastrar 1 ");
            System.out.println("Consultar 2");
            op=e.nextInt();

            if (op==1){
                Candidato candidato = new Candidato();

                System.out.println("digite o nome ");
                candidato.setNome(e.next());
                System.out.println("digite o partido");
                candidato.setPartido(e.next());
                System.out.println("digite o numero");
                candidato.setNumero(e.next());

                listacandi.add(candidato);
            }else if (op==2) {
                System.out.println("Digite um numero");
                String n = e.next();

                for (int i =0; i<listacandi.size();i++){
                    if (listacandi.get(i).getNumero().equals(n)){
                        System.out.println(listacandi.get(i).getNome()+","+listacandi.get(i)
                                .getPartido()+",");
                    }
                }
            }else if (op ==3){

            }
        }while (op!=4);
    }
}

Candidate Class

public class Candidato {

    String nome;
    String partido;
    String numero;
    int votos=0;

    public int getVotos() {
        return votos;
    }

    public void setVotos( int votos) {
        this.votos = votos;
    }

    public String getNumero() {
        return numero;
    }

    public void setNumero(String numero) {
        this.numero = numero;
    }

    public String getNome() {
        return nome;
    }

    public void setNome(String nome) {
        this.nome = nome;
    }

    public String getPartido() {
        return partido;
    }

    public void setPartido(String partido) {
        this.partido = partido;
    }


}
  • 1

    Add the Candidate class to the question code.

  • It would be nice to at least start doing this part that is asking and have a specific question.

  • I added the Candidate class, had forgotten rsrs

1 answer

2


I would help more if I had provided more. I restructured all the code because this form is far from ideal. My version is not ideal either, but for an exercise is good:

import java.util.*;

class Main {
    public static void main (String[] args) {
        ArrayList<Candidato> Candidatos = new ArrayList<Candidato>();
        Scanner e = new Scanner(System.in);
        int op;
        do {
            System.out.println("Cadastrar 1 ");
            System.out.println("Consultar 2");
            System.out.println("Votar     3");
            System.out.println("Finalizar 4");
            op = e .nextInt();
            if (op == 1) {
                System.out.println("digite o numero");
                String numero = e.next();
                System.out.println("digite o nome ");
                String nome = e.next();
                System.out.println("digite o partido");
                String partido = e.next();
                Candidatos.add(new Candidato(numero, nome, partido));
            } else if (op == 2) {
                System.out.println("Digite um numero");
                String n = e.next();
                for (int i = 0; i < Candidatos.size(); i++) {
                    if (Candidatos.get(i).getNumero().equals(n)) {
                        System.out.println(Candidatos.get(i).getNome() + ", " + Candidatos.get(i).getPartido() + ", " + Candidatos.get(i).getVotos());
                    }
                }
            } else if (op == 3) {
                System.out.println("Digite um numero de quem deseja votar");
                String n = e.next();
                int i = 0;
                for (; i < Candidatos.size(); i++) {
                    if (Candidatos.get(i).getNumero().equals(n)) {
                        break;
                    }
                }
                if (i != Candidatos.size()) Candidatos.get(i).Votar();
            }
        } while (op !=4 );
    }
}

class Candidato {
    String nome;
    String partido;
    String numero;
    int votos = 0;
    public Candidato(String numero, String nome, String partido) {
        this.numero = numero;
        this.nome = nome;
        this.partido = partido;
    }
    public void Votar() { votos++; }
    public int getVotos() { return votos; }
    public String getNumero() { return numero; }
    public String getNome() { return nome; }
    public String getPartido() { return partido; }
}

Behold working in the ideone. And in the repl it.. Also put on the Github for future reference.

Actually, there are a lot of things that are in the wrong place, the wrong way. Interestingly people try to do OOP without understanding what this is and almost always the codes are very procedural.

  • 1

    It worked perfectly friend, took me several doubts. If I wanted to create an option for change of registration, I would just create the right setters?

  • Yes, but it makes no sense, a data like this is immutable, it should not be changed. The problem is precisely people want to use getters and setters by default without needing them.

  • Yes I understand, I’m starting in java now so I’m still having doubts.

Browser other questions tagged

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