Remove elements from a two-dimensional array in C #

0

I am doing a program that simulates a database of a library, which should allow operations such as entering new books, unsubscribing, consulting and others, this is the code I have:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Data;

namespace ConsoleApp2
{
    class Program
    {
        static void Main(string[] args)
        {

            int opcio = 0, comptador = 0, posicioVector1 = 0;
            string[,] llibres = new string[100, 5];
            char final = ' ';
            bool acabar = false;

            while (!acabar)
            {
                Console.Clear();
                opcio = menuPrincipal();
                switch (opcio)
                {
                    case 1:
                        afegeixLlibre(ref llibres, ref comptador);
                        break;
                    case 2:
                        eliminaLlibre(ref llibres, ref comptador);
                        break;
                    case 3:
                        llistaLlibres(llibres, comptador);
                        break;
                    case 4:
                        consultaTitol(llibres);
                        break;
                    case 5:
                        sortida();
                        break;
                }

                final = espera();

                if (final == 'n')
                {
                    acabar = true;
                }

            }
        }



        static void afegeixLlibre(ref string[,] llibres, ref int comptador)
        {

            if (comptador == 100)
            {
                Console.WriteLine("No es poden afegir més llibres, prova eliminant algun");
            }
            else
            {

                for (int x = comptador; x < comptador + 1; x++)
                {

                    for (int y = 0; y < 5; y++)
                    {

                        if (y == 0)
                        {
                            Console.WriteLine("Entra el ISBN");
                            llibres[x, y] = Console.ReadLine();
                        }else if (y == 1)
                        {
                            Console.WriteLine("Entra el títol del llibre");
                            llibres[x, y] = Console.ReadLine();
                        }else if (y == 2)
                        {
                            Console.WriteLine("Entra el autor del llibre");
                            llibres[x, y] = Console.ReadLine();
                        }else if (y == 3)
                        {
                            Console.WriteLine("Entra la editorial del llibre");
                            llibres[x, y] = Console.ReadLine();
                        }else if (y == 4)
                        {
                            Console.WriteLine("Entra el any de edició");
                            llibres[x, y] = Console.ReadLine();
                        }

                    }
                }

                comptador++;

            }

        }

        static void eliminaLlibre(ref string[,] llibres, ref int comptador)
        {
            string isbn;
            bool trobat = false;
            int posicioX = 0;

            Console.WriteLine("Entra el ISBN del llibre que vols donar de baixa:");
            isbn = Console.ReadLine();


            for (int x = 0; x < comptador; x++)
            {
                if (llibres[x, 0] == isbn)
                {
                    trobat = true;
                    break;
                }
                else
                {
                    posicioX++;
                }
            }


            if (!trobat)
            {
                Console.WriteLine("Llibre trobat a la base de dades, donant de baixa");
                comptador--;
            }
            else
            {
                Console.WriteLine("No s'ha pogut trobar el libre, Comrpvoa que el ISBN sigui correcte o que el llibre es trobi a la base de dades");
            }

        }

        static void llistaLlibres(string[,] llibres, int comptador)
        {

            Console.Clear();

            Console.WriteLine("Llistat de llibres:");

            for (int x = 0; x < comptador; x++)
            {

                for (int y = 0; y < 5; y++)
                {


                    if (y == 0)
                    {
                        Console.WriteLine("ISBN: "+ llibres[x, y]);
                    }
                    else if (y == 1)
                    {
                        Console.WriteLine("Títol: " + llibres[x, y]);
                    }
                    else if (y == 2)
                    {
                        Console.WriteLine("Autor: " + llibres[x, y]);
                    }
                    else if (y == 3)
                    {
                        Console.WriteLine("Editorial: " + llibres[x, y]);
                    }
                    else if (y == 4)
                    {
                        Console.WriteLine("Any de edició: " + llibres[x, y]);
                    }

                    Console.WriteLine("----------------------------------------------------------------");

                }
            }

        }


        static void consultaTitol(string[,] llibres)
        {

        }


        public static int menuPrincipal()
        {
            int opcio = 0;
            bool entradaValida = false;
            ConsoleKeyInfo cki;

            escriureMenu();

            do
            {
                cki = Console.ReadKey(true);
                if (Char.IsNumber(cki.KeyChar))
                {
                    opcio = Int32.Parse(cki.KeyChar.ToString());

                    if (opcio > 6 || opcio < 1)
                    {
                        Console.WriteLine("El nombre ha d'estar entre 1 i 6, torna a provar");
                    }
                    else
                    {
                        entradaValida = true;
                    }

                }
                else
                {
                    opcio = 0;
                    Console.WriteLine("La opció ha de ser un nombre, torna a provar");
                }

            } while (!entradaValida);
            return opcio;
        }
        public static void escriureMenu()
        {
            Console.WriteLine("-----------");
            Console.WriteLine("|Llibreria|");
            Console.WriteLine("-----------");
            Console.WriteLine("1 - Afegeix un llibre");
            Console.WriteLine("2 - Donar de baixa un llibre");
            Console.WriteLine("3 - Llistat de llibres");
            Console.WriteLine("4 - Consulta per títol");
            Console.WriteLine("5 - Sortir");
        }


        static char espera()
        {
            char opcio;
            bool entradaValida = false;

            Console.WriteLine("vols continuar? s/n");
            do
            {
                opcio = Convert.ToChar(Console.ReadLine());

                if (opcio != 's' && opcio != 'n')
                {
                    Console.WriteLine("La opció només pot ser s o n, en minúscules");
                }
                else
                {
                    entradaValida = true;
                }
            } while (!entradaValida);

            return opcio;
        }

        static void sortida()
        {
            Environment.Exit(0);
        }
    }
}
  

Well, the question is that in the function of removing or deleting a   book, I do not know how I can delete the elements of an array   two-dimensional, the algorithm would be something like that (I leave the function cited first):

    static void eliminaLlibre(ref string[,] llibres, ref int comptador)
    {
        string isbn;
        bool trobat = false;
        int posicioX = 0;

        Console.WriteLine("Entra el ISBN del llibre que vols donar de baixa:");
        isbn = Console.ReadLine();


        for (int x = 0; x < comptador; x++)
        {
            if (llibres[x, 0] == isbn)
            {
                trobat = true;
                break;
            }
            else
            {
                posicioX++;
            }
        }


        if (!trobat)
        {
            Console.WriteLine("Llibre trobat a la base de dades, donant de baixa");
            comptador--;
        }
        else
        {
            Console.WriteLine("No s'ha pogut trobar el libre, Comrpvoa que el ISBN sigui correcte o que el llibre es trobi a la base de dades");
        }

    }

With a for loop I go through the first box, which is equivalent to the ISBN, then from there, once found, I can only advance horizontally and go erasing these elements, and that last is what I do not know how I can do it . Which brings me to another question:

  

Once the element is removed, it would be ideal if you re-enter   another book in the database, it will be entered in the same position in   that was eliminated, and there and if I'm completely lost, some   idea? thank you very much from now!

    
asked by THR4SH3RP0L0 11.03.2017 в 18:45
source

1 answer

1

Your code has several problems. The main problem you have is derived from the data structure you are using. An array is as if you had a shelf with books. You can put a book on a shelf, and you can remove it from it, but the gap will always be there.

In your case, a list List would be more appropriate, since in that structure you do not have to worry about finding the free position to put a new book, simply add it and place it at the end. When it comes to eliminating the same, you look for the book to be deleted, you remove it and you do not have to worry about anything else.

On the other hand, when you are adding a new book, I do not understand why you use a loop to use only one position in the array, when you can use llibres[comptador]... .

Another detail is that instead of using a two-dimensional array to put the fields of each book into the second dimension, it is not appropriate either. The logical thing is to create a class Libro that has the necessary fields, something like this:

class Libro
{
     public string ISBN {get;set;}
     public string Titulo {get;set;}
     public string Autor {get;set;}
     public string Editorial {get;set;}
     public string Año {get;set;}
}

In this way, you would use a list defined as: List<Libro> lista= new List<Libro>();

The process to add a book, search it and delete it is that simple:

//Añadimos un nuevo libro
lista.Add(new Libro(){ ISBN = "ABCD", 
                       Titulo = "titulo", 
                       Autor = "autor",
                       Editorial = "ed", 
                       Año = "1999" });

//Buscamos el libro con ISBN="ABCD"
Libro lib=lista.Where(x => x.ISBN == "ABCD").FirstOrDefault();

//Eliminamos el libro que acabamos de encontrar
lista.Remove(lib);

//Mostrar lista completa de libros
foreach(Libro l in lista)
{
    Console.WriteLine("ISBN: " + l.ISBN);
    Console.WriteLine("Titulo: " + l.Titulo);
    Console.WriteLine("Autor: " + l.Autor);
    Console.WriteLine("Editorial: " + l.Editorial);
    Console.WriteLine("Año: " + l.Año);
}

As you see, in this way the process is very simple and clear. I hope it helps you.

    
answered by 13.03.2017 / 09:33
source