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!