Ejercicio
Base De Datos De Libros
Objectivo
Cree una pequeña base de datos, que se utilizará para almacenar datos sobre libros. Para un determinado libro, queremos conservar la siguiente información:
Título
Autor
El programa debe ser capaz de almacenar 1000 libros, y el usuario podrá:
Agregar datos para un libro
Mostrar todos los libros introducidos (solo título y autor, en la misma línea)
Buscar el libro (s) con un título determinado
Eliminar un libro en una posición conocida (por ejemplo, el libro número 6)
Salir del programa
Sugerencia: para eliminar un elemento de una matriz, debe mover hacia atrás cada elemento que se colocó después de él y disminuir el contador.
Ejemplo Ejercicio C#
Mostrar Código C#
using System; // Import the System namespace for basic functionality
// Define the Book struct to store data about a book
struct Book
{
public string title; // Title of the book
public string author; // Author of the book
}
class Program // Define the main class
{
static void Main() // The entry point of the program
{
// Define an array of Book structs to store up to 1000 books
Book[] books = new Book[1000];
int bookCount = 0; // Initialize the counter for the number of books entered
bool exit = false; // Variable to control the exit condition of the loop
// Menu loop to repeatedly show the options until the user chooses to exit
while (!exit)
{
// Display the menu with options
Console.WriteLine("\nMenu:");
Console.WriteLine("1. Add data for one book");
Console.WriteLine("2. Display all entered books");
Console.WriteLine("3. Search for a book by title");
Console.WriteLine("4. Delete a book by position");
Console.WriteLine("5. Exit");
Console.Write("Choose an option (1-5): ");
// Read the user's menu choice
string choice = Console.ReadLine();
// Perform actions based on the user's choice
switch (choice)
{
case "1":
// Add data for one book
if (bookCount < books.Length)
{
Console.WriteLine("\nEnter data for Book " + (bookCount + 1) + ":");
Console.Write("Enter book title: ");
books[bookCount].title = Console.ReadLine(); // Get the book title
Console.Write("Enter book author: ");
books[bookCount].author = Console.ReadLine(); // Get the book author
bookCount++; // Increment the counter for the number of books entered
}
else
{
Console.WriteLine("Maximum number of books reached.");
}
break;
case "2":
// Display all entered books
if (bookCount > 0)
{
Console.WriteLine("\nEntered Books:");
for (int i = 0; i < bookCount; i++)
{
Console.WriteLine($"{i + 1}. Title: {books[i].title}, Author: {books[i].author}");
}
}
else
{
Console.WriteLine("No books entered yet.");
}
break;
case "3":
// Search for a book by title
Console.Write("\nEnter the title of the book to search: ");
string searchTitle = Console.ReadLine(); // Get the title to search for
bool found = false; // Flag to track if the book is found
Console.WriteLine("\nSearch Results:");
for (int i = 0; i < bookCount; i++)
{
if (books[i].title.Equals(searchTitle, StringComparison.OrdinalIgnoreCase)) // Check if titles match
{
Console.WriteLine($"{i + 1}. Title: {books[i].title}, Author: {books[i].author}");
found = true; // Set found flag to true
}
}
if (!found)
{
Console.WriteLine("No book found with that title.");
}
break;
case "4":
// Delete a book by position
Console.Write("\nEnter the book number to delete: ");
int deleteIndex;
if (int.TryParse(Console.ReadLine(), out deleteIndex) && deleteIndex > 0 && deleteIndex <= bookCount)
{
// Shift all books after the deleted one to fill the gap
for (int i = deleteIndex - 1; i < bookCount - 1; i++)
{
books[i] = books[i + 1]; // Move the next book to the current position
}
bookCount--; // Decrease the count of books
Console.WriteLine($"Book number {deleteIndex} has been deleted.");
}
else
{
Console.WriteLine("Invalid book number.");
}
break;
case "5":
// Exit the program
exit = true;
Console.WriteLine("Exiting the program.");
break;
default:
// Handle invalid menu choices
Console.WriteLine("Invalid choice, please select a valid option.");
break;
}
}
}
}
Salida
Menu:
1. Add data for one book
2. Display all entered books
3. Search for a book by title
4. Delete a book by position
5. Exit
Choose an option (1-5): 1
Enter data for Book 1:
Enter book title: Magic
Enter book author: Magic
Menu:
1. Add data for one book
2. Display all entered books
3. Search for a book by title
4. Delete a book by position
5. Exit
Choose an option (1-5): 3
Enter the title of the book to search: Magic
Search Results:
1. Title: Magic, Author: Magic
Menu:
1. Add data for one book
2. Display all entered books
3. Search for a book by title
4. Delete a book by position
5. Exit
Choose an option (1-5): 5
Exiting the program.
Código de Ejemplo Copiado!
Comparte este Ejercicio C# Sharp