How to edit a list in C #?

12

I want to edit a customer record in which I can already create a record, consult and remove it. But I have no idea how to edit just a few items without losing others.

class Program
{
    static List<Cliente> ClienteList = new List<Cliente>();

    static void Main(string[] args)
    {
        int op = -1;

        while (op != 0)
        {
            Console.WriteLine("Digite 1 para cadastrar: ");
            Console.WriteLine("Digite 2 para consultar: ");
            Console.WriteLine("Digite 3 para remover cadastro");
            Console.WriteLine("Digite 4 para editar cadastro: ");
            Console.WriteLine("Digite 0 para sair: "); 

            op = int.Parse(Console.ReadLine());
            {
                if (op == 1)
                {
                    CadastroCliente();
                }
                else
                    if (op == 2)
                    {
                        ConsultaCadaCli();
                    }
                    else
                        if (op == 3)
                        {
                            RemoverCadastro();
                        }
                        else
                            if (op == 4)
                            {
                                EditarCliente();
                            }
            }

        }
    }
    public static void CadastroCliente()
    {
        Cliente cli = new Cliente();
        Console.WriteLine("Digite o nome: ");
        cli.Nome = Console.ReadLine();
        Console.WriteLine("Digite o CPF: ");
        cli.Cpf = int.Parse(Console.ReadLine());
        Console.WriteLine("Digite o telefone: ");
        cli.Tel = int.Parse(Console.ReadLine());
        Console.WriteLine("Digiete o endereco: ");
        cli.End = Console.ReadLine();

        ClienteList.Add(cli);

    }
    public static void ConsultaCadaCli()
    {
        int Consulta;
        Console.WriteLine("Digite seu CPF");
        Consulta = int.Parse(Console.ReadLine());
        var ConsultCadastro = ClienteList.Where(c => c.Cpf.Equals(Consulta)).FirstOrDefault();

        if (ConsultCadastro != null)
        {
            Console.WriteLine("Nome: " + ConsultCadastro.Nome);
            Console.WriteLine("CPF: " + ConsultCadastro.Cpf);
            Console.WriteLine("Telefone: " + ConsultCadastro.Tel);
            Console.WriteLine("Endereco: " + ConsultCadastro.End);
        }
        else
        {
            Console.WriteLine("Cliente nao cadastrado");
        }
    }
    public static void RemoverCadastro()
    {
        int RemCadast;
        Console.WriteLine("Digite seu CPF");
        RemCadast = int.Parse(Console.ReadLine());
        var RCli = ClienteList.Where(r => r.Cpf.Equals(RemCadast)).FirstOrDefault();

        if(RCli != null)
        {
            ClienteList.Remove(RCli);
            Console.WriteLine("Cadastro removido com sucesso. ");
        }
        else
        {
            Console.WriteLine("Cliente nao cadastrado");
        }
    }
}
    
asked by anonymous 15.12.2014 / 12:42

2 answers

9

It's almost a mix of registering and consulting. You need to find the client as in the query and request new data as in the registration. But instead of adding you change the data of the found client:

public static void EditarCliente()
{
    Console.WriteLine("Digite seu CPF");
    int Consulta = int.Parse(Console.ReadLine());
    var cli = ClienteList.Where(c => c.Cpf.Equals(RemCadast)).FirstOrDefault();

    if(cli != null) {
        Console.WriteLine("CPF: " + cli.Cpf);
        Console.WriteLine("Nome: " + cli.Nome);
        Console.WriteLine("Telefone: " + cli.Tel);
        Console.WriteLine("Endereco: " + cli.End);

        Console.WriteLine("Digite o nome: ");
        cli.Nome = Console.ReadLine();
        Console.WriteLine("Digite o telefone: ");
        cli.Tel = int.Parse(Console.ReadLine());
        Console.WriteLine("Digiete o endereco: ");
        cli.End = Console.ReadLine();
    } else {
        Console.WriteLine("Cliente nao cadastrado");
    }
}

You can see full code working in .NET Fiddle with some minor improvements you can make, but still with some issues. Also put it in GitHub for future reference .

Some possible improvements

I'm not saying what you should do in this job. It is probably beyond what you have learned and the effort is probably very large.

Could allow the data shown to be availed for editing during data entry, which would complicate well the logic you are doing. But that's basically it.

I understand that this is a naive implementation of a cadaster. If it were to do something real, the code would have to do much more than this. You could check for errors that may occur, double registration, validate, do not use bad data types like int for phone and CPF (data that will not be used in calculation must always be string ), etc. It would have many changes to get something really functional, but there's no use improving just a little bit for something that is clearly being used as learning.

I made just a few changes to keep naming consistency for all code. This is very important to keep your code organized and you should learn to do it right from the start. Call from the same name things that are the same. The variables of a function are self contained within it and are not confused with variables of other functions. There is no risk of disturbing the other. Therefore, the variable cli of the query is not the same%% variable of the removal. I used to join the statement with the assignment of the value of the variable where it gave. I also changed the order of the fields. The CPF being key should always be the first one in the registry.

When you learn more, you'll be able to do something that looks more sophisticated but actually simpler (I'm not saying you should change the way you work now):

using static System.Console;
using System.Collections.Generic;
using System.Linq;

public class Cliente {
    public int Cpf {get; set; }
    public string Nome {get; set; }
    public int Tel {get; set; }
    public string End {get; set; }
}               

public class Program {
    static List<Cliente> ClienteList = new List<Cliente>();

    public static void Main(string[] args) {
        var op = -1;
        while (op != 0) {       
            WriteLine("Digite seu CPF (0 p/ sair)");
            if (!int.TryParse(ReadLine(), out var consulta)) continue;
            if (consulta == 0) {
                return;
            }
            var cliente = ClienteList.Where(c => c.Cpf == consulta).FirstOrDefault();
            if (cliente != null) {
                WriteLine("Nome: " + cliente.Nome);
                WriteLine("Telefone: " + cliente.Tel);
                WriteLine("Endereco: " + cliente.End);
                WriteLine("[1 - Editar] [2 - Remover] [3 - Nova consulta] [0 - Sair]");
                if (!int.TryParse(ReadLine(), out op)) continue;
                switch (op) {
                case 1:
                    CadastroCliente(cliente);
                    break;
                case 2:
                    ClienteList.Remove(cliente);
                    WriteLine("Cadastro removido com sucesso. ");
                    break;
                }
            } else {
                var novo = new Cliente() { Cpf = consulta };
                CadastroCliente(novo);
                ClienteList.Add(novo);
            }
        }
    }
    public static void CadastroCliente(Cliente cliente) {
        WriteLine("Digite o nome: ");
        cliente.Nome = ReadLine();
        WriteLine("Digite o telefone: ");
        cliente.Tel = int.Parse(ReadLine());
        WriteLine("Digite o endereco: ");
        cliente.End = ReadLine();
    }
}

See running on .NET Fiddle . And no Coding Ground . Also I placed GitHub for future reference .

    
15.12.2014 / 12:52
6

Well, @Maniero went ahead, but I'll post a solution as well, as I made some changes mainly in the choice of options.

class Program
{
    static List<Cliente> ClienteList = new List<Cliente>();
    static void Main(string[] args)
    {
        int op = -1;

        while (op != 0)
        {

            Console.WriteLine("Digite 1 para cadastrar: ");
            Console.WriteLine("Digite 2 para consultar: ");
            Console.WriteLine("Digite 3 para remover cadastro");
            Console.WriteLine("Digite 4 para editar cadastro: ");
            Console.WriteLine("Digite 0 para sair: ");

            op = int.Parse(Console.ReadLine());

            //usar um switch/case é bem mais bonito que vários IFs aninhados
            switch (op)
            {
                case 1:
                    CadastroCliente();
                    break;
                case 2:
                    ConsultaCadaCli();
                    break;
                case 3:
                    RemoverCadastro();
                    break;
                case 4:
                    EditarCliente();
                    break;
            }

        }
    }

    private static void EditarCliente()
    {
        foreach(var cliente in ClienteList)
        {
            Console.WriteLine(cliente.Nome);
            Console.WriteLine("\"e\" para editar ou somente enter para ir para o próximo registro");
            var a = Console.ReadLine();
            if (a == "e")
            {                    
                Console.WriteLine("Digite o nome [{0}]: ", cliente.Nome);
                var nome = Console.ReadLine();
                Console.WriteLine("Digite o CPF [{0}]: ", cliente.Cpf);
                var cpf = Console.ReadLine();
                Console.WriteLine("Digite o telefone [{0}]: ", cliente.Tel);
                var tel = Console.ReadLine();
                Console.WriteLine("Digiete o endereco [{0}]: ", cliente.End);
                var end = Console.ReadLine();

                if (nome != "" && nome != cliente.Nome)
                    cliente.Nome = nome;

                if (cpf != "" && cpf != cliente.Cpf)
                    cliente.Cpf = cpf;

                if (tel != "" && tel != cliente.Tel)
                    cliente.Tel = tel;

                if (end != "" && end != cliente.End)
                    cliente.End = end;
            }
        }
    }
    public static void CadastroCliente()
    {
        Cliente cli = new Cliente();
        Console.WriteLine("Digite o nome: ");
        cli.Nome = Console.ReadLine();
        Console.WriteLine("Digite o CPF: ");
        cli.Cpf = Console.ReadLine();
        Console.WriteLine("Digite o telefone: ");
        //o telefone tem que ser uma string
        cli.Tel = Console.ReadLine().ToString();
        Console.WriteLine("Digiete o endereco: ");
        cli.End = Console.ReadLine();


        ClienteList.Add(cli);

    }
    public static void ConsultaCadaCli()
    {
        int Consulta;
        Console.WriteLine("Digite seu CPF");
        Consulta = int.Parse(Console.ReadLine());
        var ConsultCadastro = ClienteList.Where(c => c.Cpf.Equals(Consulta)).FirstOrDefault();

        if (ConsultCadastro != null)
        {
            Console.WriteLine("Nome: " + ConsultCadastro.Nome);
            Console.WriteLine("CPF: " + ConsultCadastro.Cpf);
            Console.WriteLine("Telefone: " + ConsultCadastro.Tel);
            Console.WriteLine("Endereco: " + ConsultCadastro.End);
        }
        else
        {
            Console.WriteLine("Cliente nao cadastrado");
        }


    }
    public static void RemoverCadastro()
    {
        int RemCadast;
        Console.WriteLine("Digite seu CPF");
        RemCadast = int.Parse(Console.ReadLine());
        var RCli = ClienteList.Where(r => r.Cpf.Equals(RemCadast)).FirstOrDefault();

        if (RCli != null)
        {
            ClienteList.Remove(RCli);
            Console.WriteLine("Cadastro removido com sucesso. ");
        }
        else
        {
            Console.WriteLine("Cliente nao cadastrado");
        }
    }
}
    
15.12.2014 / 13:04