Repeated name in c #

0

I have this code, what I want is that you can not repeat the name and surname, for example if a user joins Juan Perez that later can not enter another bet with the name Juan Perez, what I did is wrong but it seems to me that the hand is coming around.

    bool repetidoo = false;
                    string nom,ape;
                    if (tope < nombres.Length)
                    {
                        repetidoo = false;
                            Console.ForegroundColor = ConsoleColor.Cyan;
                            Console.Write("INGRESE NOMBRE : ");
                            nom = Console.ReadLine().ToUpper().Trim();
                            Console.ForegroundColor = ConsoleColor.Cyan;
                            Console.Write("\n" + "INGRESE APELLIDO : ");
                            ape = Console.ReadLine().ToUpper().Trim();
                            for (int j = 0; j <= nom.Length; j++)
                            {
                                if (nom == nombres[tope])
                                {
                                    repetidoo = true;
                                }
                            }
                            if (repetidoo)
                            {
                                Console.ForegroundColor = ConsoleColor.Red;
                                Console.WriteLine("Nombre repetido"); // 
                            }
                            else
                            {
                                nombres[tope] = nom;
                            }
    
asked by Francop 01.07.2017 в 23:30
source

1 answer

2

I see a lot of problems in the code.

If you are going to add elements to the list of names, that is, it will not have a fixed length, you should better use a generic list instead of an array.

You collect the name and surname but only store and compare the name.

If the user is going to have different properties (such as name and surname) that you are going to deal with separately, it would be best if you create a class or structure to encapsulate the user's information.

If you are going to show the messages in red, it would be better if you created a method that would do it, so that, if in the future you want to change the format of the error messages, you should only change it in one place.

You should also check that the user does not enter a first or last name (or both) empty.

Take a look at this example:

using System;
using System.Collections.Generic;
using System.Linq;

namespace Console_CSharp
{
    class Program
    {

        public class Persona
        {
            public Persona(string nombre, string apellido)
            {
                Nombre = nombre;
                Apellido = apellido;
            }

            public string Nombre { get; }
            public string Apellido { get; }
        }

        private const int TopePersonas = 10;

        static void Main(string[] args)
        {
            var personas = new List<Persona>();
            while (ObtenerOpcion()!='X')
            {
                NuevaPersona(personas);
            }

            Console.Write("\n\n--------------------\n");
            Console.Write("Personas introducidas\n\n");
            foreach (var persona in personas)
            {
                Console.WriteLine($"{persona.Nombre} {persona.Apellido}");
            }
            Console.ReadKey();
        }

        private static char ObtenerOpcion()
        {
            Console.Write("\n\n--------------------\n");
            Console.WriteLine("(1) Introducir nombre");
            Console.WriteLine();
            Console.WriteLine("(X) Salir");
            ConsoleKeyInfo c;
            Console.Write("--------------------\n");
            do
            {
                c = Console.ReadKey();
            } while (c.KeyChar != '1' && c.KeyChar != 'x' && c.KeyChar != 'X');
            Console.WriteLine();
            return c.KeyChar == 'x' ? 'X' : c.KeyChar;
        }

        private static void MostrarError(string error)
        {
            var previousColor = Console.ForegroundColor;
            Console.ForegroundColor = ConsoleColor.Red;
            Console.WriteLine(error);
            Console.ForegroundColor = previousColor;
        }

        private static string SolicitarCadena(string mensaje)
        {
            var previousColor = Console.ForegroundColor;
            Console.ForegroundColor = ConsoleColor.Blue;
            Console.Write($"{mensaje}: ");
            var cadena = Console.ReadLine()?.Trim();
            Console.ForegroundColor = previousColor;
            return cadena;
        }

        private static void NuevaPersona(List<Persona> personas)
        {
            if (TopePersonas <= personas.Count)
            {
                MostrarError("Se ha llegado al tope");
                return;
            }

            var nom = SolicitarCadena("Ingrese Nombre");
            var apellido = SolicitarCadena("Ingrese apellido");

            if (string.IsNullOrEmpty(nom) || string.IsNullOrEmpty(apellido))
            {
                MostrarError("Debe introducir ambos valores");
                return;
            }

            if (personas.Any(p => string.Equals(p.Nombre, nom, StringComparison.CurrentCultureIgnoreCase) 
                    && string.Equals(p.Apellido, apellido, StringComparison.CurrentCultureIgnoreCase)))
            {
                MostrarError($"Ya se ha introducido una persona con el nombre {nom} {apellido}");
                return;
            }

            personas.Add(new Persona(nom, apellido));
        }
    }
}

It is fully functional. You can try it.

The NuevaPersona method is the one that contains the functionality of your example. It uses two methods: MostrarError and SolicitarCadena to show the error messages and ask for data entry respectively.

    
answered by 02.07.2017 / 00:40
source