Get month value from a list

Asked

Viewed 120 times

-1

I need the person to be able to get the information and month they want, so I need to get this information from the list and then show the license plate, date, time of entry and exit, paid amount and total of the month. I was trying with a Split() but I couldn’t.

public class Informacao2
{
    public string placa { get; set; }
    public float precoHora { get; set; }
    public DateTime entrada { get; set; }
}
class Ex3
{
    Informacao2 novaInformacao = new Informacao2();
    List<Informacao2> informacoes = new List<Informacao2>();
    List<Informacao2> mostraVeicMes = new List<Informacao2>();
    public void Executar()
    {
        int opcao = 0;
        do
        {
            Console.Clear();
            Console.WriteLine("1 - Cadastrar Entrada de Veiculo");
            Console.WriteLine("2 - Cadastrar Saida do Veiculo");
            Console.WriteLine("3 - Mostrar Veiculos Estacionados no Mês");
            Console.WriteLine("0 - Sair");
            opcao = Convert.ToInt32(Console.ReadLine());

            switch (opcao)
            {
                case 0: break;
                case 1:
                    CadastraEntradaEPrecoVeiculo();
                    break;
                case 2:
                    CadastraSaidaVeiculo();
                    break;
                case 3:
                    MostraVeiculoEstcionadoMes();
                    break;
                default:
                    Console.WriteLine("Opção inválida");
                    Console.ReadLine();
                    break;
            }
        } while (opcao != 0);
    }
    public void CadastraEntradaEPrecoVeiculo()
    {
        DateTime ent;
        string data1;
        bool f1;
        Console.Write("Informe o preço da hora: ");
        novaInformacao.precoHora = Convert.ToSingle(Console.ReadLine());
        do
        {
            Console.WriteLine("Informe a data e hora de entrada do automóvel dd/MM/yyyy hh:mm");
            data1 = Console.ReadLine();
            f1 = DateTime.TryParseExact(data1, "dd/MM/yyyy HH:mm", System.Globalization.CultureInfo.InvariantCulture, System.Globalization.DateTimeStyles.AssumeLocal, out ent);
            if (f1 == false)
            {
                Console.WriteLine("Formato Inválido!");
            }
        } while (f1 == false);
        novaInformacao.entrada = ent;
        Console.Write("Informe a placa do veículo: ");
        novaInformacao.placa = Console.ReadLine();
        novaInformacao.placa = novaInformacao.placa.ToUpper();
        informacoes.Add(novaInformacao);
        mostraVeicMes.Add(novaInformacao);
    }
    private void MostrarInformacoes()
    {
        Console.Clear();
        foreach (Informacao2 informacao in informacoes)
        {
            Console.WriteLine($"Placa: {novaInformacao.placa}");
            Console.WriteLine($"Preço da hora: {novaInformacao.precoHora}");
            Console.WriteLine($"Horario de entrada: {novaInformacao.entrada.ToString("dd/MM/yyyy hh:mm")}");
            Console.WriteLine("---------------------------------------------");
        }
    }
    public void CadastraSaidaVeiculo()
    {
        MostrarInformacoes();
        DateTime atual;
        Console.Write("Informe a placa do veículo: ");
        string placaInformada = Console.ReadLine();
        List<Informacao2> placaencontrada = informacoes.Where(c => c.placa.ToUpper().Contains(placaInformada.ToUpper())).ToList();
        atual = DateTime.Now;
        TimeSpan tempoEst = atual - novaInformacao.entrada;
        double valorvaloraserpago = Math.Ceiling(tempoEst.Hours * novaInformacao.precoHora);
        foreach (Informacao2 informacao in placaencontrada)
        {
            Console.WriteLine($"PLACA ENCONTRADA: {informacao.placa}");
            Console.WriteLine($"VALOR A SER PAGO {valorvaloraserpago}");
        }
        Console.ReadLine();
    }
    public void MostraVeiculoEstcionadoMes()
    {
        /*d) Mostrar um Relatório de veículos estacionados no mês (na tela)
       - Solicitar o mês para o usuário
       - Mostrar a placa, data e horário de entrada, data e horário de saída e valor pago e o
       total do mês.*/
        Int32 pegaMes,compare=0;
        Console.Write("Informe o mês que deseja o relatório com 2 digitos: ");
        pegaMes = Convert.ToInt32(Console.ReadLine());
        foreach (Informacao2 informacao in informacoes)
        {
            string m = novaInformacao.entrada.ToString("dd/MM/yyyy");
            string[] mes = m.Split('/');
            pegaMes = Convert.ToInt32(mes[1]);
            compare = Convert.ToInt32(mes[1]);
        }
        Console.ReadLine();
    }
}
  • Have you tried using Lincoln? Where(d=> d.entrada.Month == novaInformacao.entrada.Month); if you only need to compare use Any instead of Where

  • Did the answer solve your question? Do you think you can accept it? See [tour] if you don’t know how you do it. This would help a lot to indicate that the solution was useful to you. You can also vote on any question or answer you find useful on the entire site

1 answer

3

There are several problems in this code, but what you want is simpler than it looks. Just ask for the month on the date itself, do not have to juggle.

mes = novaInformacao.entrada.Month;

I put in the Github for future reference.

At all times see the documentation to see if you already have not ready what you want. And almost every type conversion performed indicates that the code has some problem.

Browser other questions tagged

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