List contents of a list with a method

Asked

Viewed 38 times

2

I have the following code:

using System;
using System.Collections.Generic;

public class Program
{
    class Candidato
    {
        public int numeroPartido {get;set;}
        public string nomeCandidato {get;set;}
        public int quantidadeVotos {get;set;}
    }
    class Eleitor
    {
        public string CPF {get;set;}
        public string nomeEleitor {get;set;}
        public Boolean votoRealizado {get;set;}
    }
    public static void Main()
    {
        var x = 0;
        var y = 0;
        var z = 0;
        var contador = 0;
        var opcao = 0;

        List<Candidato> listaCandidatos = new List<Candidato>();
        listaCandidatos.Add(new Candidato {numeroPartido = 17, nomeCandidato = "Fulano", quantidadeVotos = 0});
        listaCandidatos.Add(new Candidato {numeroPartido = 13, nomeCandidato = "Ciclano", quantidadeVotos = 0});
        listaCandidatos.Add(new Candidato {numeroPartido = 45, nomeCandidato = "Deltrano", quantidadeVotos = 0});
        listaCandidatos.Add(new Candidato {numeroPartido = 99, nomeCandidato = "Branco", quantidadeVotos = 0});
        listaCandidatos.Add(new Candidato {numeroPartido = 999, nomeCandidato = "Nulo", quantidadeVotos = 0});

        List<Eleitor> listaEleitores = new List<Eleitor>();
        listaEleitores.Add(new Eleitor {CPF = "1", nomeEleitor = "João", votoRealizado = false});
        listaEleitores.Add(new Eleitor {CPF = "2", nomeEleitor = "Maria", votoRealizado = false});
        listaEleitores.Add(new Eleitor {CPF = "3", nomeEleitor = "Antonio", votoRealizado = false});
        listaEleitores.Add(new Eleitor {CPF = "4", nomeEleitor = "Marcos", votoRealizado = false});
        listaEleitores.Add(new Eleitor {CPF = "5", nomeEleitor = "Ana", votoRealizado = false});
        listaEleitores.Add(new Eleitor {CPF = "6", nomeEleitor = "Marcia", votoRealizado = false});
        listaEleitores.Add(new Eleitor {CPF = "7", nomeEleitor = "Marcio", votoRealizado = false});
        listaEleitores.Add(new Eleitor {CPF = "8", nomeEleitor = "Carla", votoRealizado = false});
        listaEleitores.Add(new Eleitor {CPF = "9", nomeEleitor = "Bruna", votoRealizado = false});
        listaEleitores.Add(new Eleitor {CPF = "10", nomeEleitor = "Mara", votoRealizado = false});

        while (opcao == 1 || opcao == 2 || contador < 10){
            Console.WriteLine("DIGITE A OPÇÃO DESEJADA:");
            Console.WriteLine("1 - PARA VOTAR.");
            Console.WriteLine("2 - LISTAR VOTAÇÃO.");
            Console.WriteLine("------------------------");
            opcao = int.Parse(Console.ReadLine());
            if (opcao == 1){
                Console.Write("DIGITE SEU CPF PARA REALIZAR O VOTO: ");
                var CPFEleitor = Console.ReadLine();
                foreach (var itemEleitores in listaEleitores)
                {
                    if (itemEleitores.CPF.Equals(CPFEleitor) && itemEleitores.votoRealizado == true){
                        Console.WriteLine("ELEITOR: "+itemEleitores.nomeEleitor);
                        y = 1;
                    }
                    if (itemEleitores.CPF.Equals(CPFEleitor) && itemEleitores.votoRealizado == false){
                        Console.WriteLine("ELEITOR: "+itemEleitores.nomeEleitor.ToUpper());
                        contador++;
                        itemEleitores.votoRealizado = true;
                        Console.Write("DIGITE O NUMERO DO SEU CANDIDATO: ");        
                        var numeroCandidato = Console.ReadLine();
                        if (!numeroCandidato.ToString().Equals("")){
                            foreach (var itemCandidados in listaCandidatos)
                            {
                                if (itemCandidados.numeroPartido.ToString().Equals(numeroCandidato)){
                                    Console.WriteLine("CANDIDATO: "+itemCandidados.nomeCandidato.ToUpper());
                                    Console.WriteLine("VOTAÇÃO CONCLUÍDA.");
                                    Console.WriteLine("------------------");
                                    itemCandidados.quantidadeVotos = itemCandidados.quantidadeVotos + 1;
                                    z = 1;
                                }
                            }
                            if (z == 0){
                                foreach (var itemCandidados in listaCandidatos)
                                {
                                    if (itemCandidados.numeroPartido == 999){
                                        itemCandidados.quantidadeVotos = itemCandidados.quantidadeVotos + 1;
                                    }
                                }
                            }
                            x = 1;
                        } else {
                            foreach (var itemCandidados in listaCandidatos)
                            {
                                if (itemCandidados.numeroPartido == 99){
                                    itemCandidados.quantidadeVotos = itemCandidados.quantidadeVotos + 1;
                                }
                            }
                        }
                    }
                }
                if (x == 0 && y == 0){
                    Console.WriteLine("CPF NÃO CADASTRADO");
                }
                if (y == 1){
                    Console.WriteLine("ELEITOR JÁ REALIZOU SEU VOTO");
                }
                x = 0;
                y = 0;
                z = 0;
            } else if (opcao == 2){
                foreach (var item in listaCandidatos)
                {
                    Console.WriteLine("Candidato {0} ({1}). Votos {2}.", item.nomeCandidato, item.numeroPartido, item.quantidadeVotos);
                }
                Console.WriteLine("------------------------");
            } else {
                Console.WriteLine("OPÇÃO INVÁLIDA. TENTE NOVAMENTE.");
            }
            opcao = 0;
        }
        if (contador == 10){
            Console.WriteLine("-------------------------------------------------");
            Console.WriteLine("VOTAÇÃO ENCERRADA. TODOS OS ELEITORES JÁ VOTARAM.");
            Console.WriteLine("-------------------------------------------------");
            foreach (var item in listaCandidatos)
            {
                Console.WriteLine("Candidato {0} ({1}). Votos {2}.", item.nomeCandidato, item.numeroPartido, item.quantidadeVotos);
            }
        }
    }
}

I would like to create a method to list the contents of one of these lists, as I already do here:

Console.WriteLine("VOTAÇÃO ENCERRADA. TODOS OS ELEITORES JÁ VOTARAM.");
Console.WriteLine("-------------------------------------------------");
foreach (var item in listaCandidatos)
{
    Console.WriteLine("Candidato {0} ({1}). Votos {2}.", item.nomeCandidato, item.numeroPartido, item.quantidadeVotos);
}

However I am not able to pass this my list as a parameter to my method to go through it and display its content.

1 answer

6


You are probably not able to pass as a parameter because the class is not published.

I changed your code to the following way:

I also adjusted the names of the properties, in C# we used Pascal Case and the class Boolean has a "nickname", bool which is easier and shorter to type ;)

using System;
using System.Collections.Generic;

public class Program
{
    public class Candidato
    {
        public int NumeroPartido { get; set; }
        public string NomeCandidato { get; set; }
        public int QuantidadeVotos { get; set; }
    }
    public class Eleitor
    {
        public string Cpf { get; set; }
        public string NomeEleitor { get; set; }
        public bool VotoRealizado { get; set; }
    }

    public static void ExibeResultado(List<Candidato> listaCandidatos)
    {
        Console.WriteLine("VOTAÇÃO ENCERRADA. TODOS OS ELEITORES JÁ VOTARAM.");
        Console.WriteLine("-------------------------------------------------");
        foreach (var item in listaCandidatos)
        {
            Console.WriteLine("Candidato {0} ({1}). Votos {2}.", item.NomeCandidato, item.NumeroPartido, item.QuantidadeVotos);
        }
    }

    public static void Main()
    {
        var x = 0;
        var y = 0;
        var z = 0;
        var contador = 0;
        var opcao = 0;

        List<Candidato> listaCandidatos = new List<Candidato>
        {
            new Candidato { NumeroPartido = 17, NomeCandidato = "Fulano", QuantidadeVotos = 0 },
            new Candidato { NumeroPartido = 13, NomeCandidato = "Ciclano", QuantidadeVotos = 0 },
            new Candidato { NumeroPartido = 45, NomeCandidato = "Deltrano", QuantidadeVotos = 0 },
            new Candidato { NumeroPartido = 99, NomeCandidato = "Branco", QuantidadeVotos = 0 },
            new Candidato { NumeroPartido = 999, NomeCandidato = "Nulo", QuantidadeVotos = 0 }
        };

        List<Eleitor> listaEleitores = new List<Eleitor>
        {
            new Eleitor { Cpf = "1", NomeEleitor = "João", VotoRealizado = false },
            new Eleitor { Cpf = "2", NomeEleitor = "Maria", VotoRealizado = false },
            new Eleitor { Cpf = "3", NomeEleitor = "Antonio", VotoRealizado = false },
            new Eleitor { Cpf = "4", NomeEleitor = "Marcos", VotoRealizado = false },
            new Eleitor { Cpf = "5", NomeEleitor = "Ana", VotoRealizado = false },
            new Eleitor { Cpf = "6", NomeEleitor = "Marcia", VotoRealizado = false },
            new Eleitor { Cpf = "7", NomeEleitor = "Marcio", VotoRealizado = false },
            new Eleitor { Cpf = "8", NomeEleitor = "Carla", VotoRealizado = false },
            new Eleitor { Cpf = "9", NomeEleitor = "Bruna", VotoRealizado = false },
            new Eleitor { Cpf = "10", NomeEleitor = "Mara", VotoRealizado = false }
        };

        while (opcao == 1 || opcao == 2 || contador < 10)
        {
            Console.WriteLine("DIGITE A OPÇÃO DESEJADA:");
            Console.WriteLine("1 - PARA VOTAR.");
            Console.WriteLine("2 - LISTAR VOTAÇÃO.");
            Console.WriteLine("------------------------");
            opcao = int.Parse(Console.ReadLine());
            if (opcao == 1)
            {
                Console.Write("DIGITE SEU CPF PARA REALIZAR O VOTO: ");
                var CPFEleitor = Console.ReadLine();
                foreach (var itemEleitores in listaEleitores)
                {
                    if (itemEleitores.Cpf.Equals(CPFEleitor) && itemEleitores.VotoRealizado == true)
                    {
                        Console.WriteLine("ELEITOR: " + itemEleitores.NomeEleitor);
                        y = 1;
                    }
                    if (itemEleitores.Cpf.Equals(CPFEleitor) && itemEleitores.VotoRealizado == false)
                    {
                        Console.WriteLine("ELEITOR: " + itemEleitores.NomeEleitor.ToUpper());
                        contador++;
                        itemEleitores.VotoRealizado = true;
                        Console.Write("DIGITE O NUMERO DO SEU CANDIDATO: ");
                        var numeroCandidato = Console.ReadLine();
                        if (!numeroCandidato.ToString().Equals(""))
                        {
                            foreach (var itemCandidados in listaCandidatos)
                            {
                                if (itemCandidados.NumeroPartido.ToString().Equals(numeroCandidato))
                                {
                                    Console.WriteLine("CANDIDATO: " + itemCandidados.NomeCandidato.ToUpper());
                                    Console.WriteLine("VOTAÇÃO CONCLUÍDA.");
                                    Console.WriteLine("------------------");
                                    itemCandidados.QuantidadeVotos = itemCandidados.QuantidadeVotos + 1;
                                    z = 1;
                                }
                            }
                            if (z == 0)
                            {
                                foreach (var itemCandidados in listaCandidatos)
                                {
                                    if (itemCandidados.NumeroPartido == 999)
                                    {
                                        itemCandidados.QuantidadeVotos = itemCandidados.QuantidadeVotos + 1;
                                    }
                                }
                            }
                            x = 1;
                        }
                        else
                        {
                            foreach (var itemCandidados in listaCandidatos)
                            {
                                if (itemCandidados.NumeroPartido == 99)
                                {
                                    itemCandidados.QuantidadeVotos = itemCandidados.QuantidadeVotos + 1;
                                }
                            }
                        }
                    }
                }
                if (x == 0 && y == 0)
                {
                    Console.WriteLine("CPF NÃO CADASTRADO");
                }
                if (y == 1)
                {
                    Console.WriteLine("ELEITOR JÁ REALIZOU SEU VOTO");
                }
                x = 0;
                y = 0;
                z = 0;
            }
            else if (opcao == 2)
            {
                foreach (var item in listaCandidatos)
                {
                    Console.WriteLine("Candidato {0} ({1}). Votos {2}.", item.NomeCandidato, item.NumeroPartido, item.QuantidadeVotos);
                }
                Console.WriteLine("------------------------");
            }
            else
            {
                Console.WriteLine("OPÇÃO INVÁLIDA. TENTE NOVAMENTE.");
            }
            opcao = 0;
        }
        if (contador == 10)
        {
            ExibeResultado(listaCandidatos);
        }
    }
}
  • 1

    Perfect was just that, thanks for the help

  • Actually it’s not just the problems of marry, This code doesn’t even seem to be C#, it has a huge amount of errors, although it works in most cases. Some will break, others, are just confused, just conceptual errors, just make the whole code less elegant and verbose too much, but everything that induces errors.

  • @Maniero Well put, I confess that I did not look at all the code, at first I hit my eyes on the marry and there I was (my bad). Once I have a little time I will try to reformulate the answer by analyzing the code better.

  • The answer answered well what was the focus of the question :) Generally it would be Cpf and not CPF.

  • @Maniero this of cpf was a question of mine, that Agra you replied rsrs

Browser other questions tagged

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