Ejercicio
Base de datos de libros
Objetivo
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.
Código
Imports System
Public Class exercise82
Structure book
Public title As String
Public author As String
End Structure
Public Shared Sub Main()
Dim capacity As Integer = 1000
Dim books As book() = New book(capacity - 1) {}
Dim repeat As Boolean = True
Dim [option] As String
Dim amount As Integer = 0
Dim search As String
Dim found As Boolean
Do
Console.WriteLine()
Console.WriteLine("Books database")
Console.WriteLine()
Console.WriteLine("1- Add a new book")
Console.WriteLine("2- Display all books")
Console.WriteLine("3- Exact search for any book")
Console.WriteLine("4- Partial search")
Console.WriteLine("5- Delete a book")
Console.WriteLine("0- Exit")
Console.Write("Enter an option: ")
[option] = Console.ReadLine()
Select Case [option]
Case "1"
If amount < capacity Then
Console.WriteLine("Enter data for book {0}", amount + 1)
Console.Write("Enter the name of the book: ")
books(amount).title = Console.ReadLine()
Console.Write("Enter the author: ")
books(amount).author = Console.ReadLine()
amount += 1
Console.WriteLine()
Else
Console.WriteLine("Database full")
End If
Case "2"
If amount = 0 Then
_
Else
For i As Integer = 0 To amount - 1
Console.WriteLine("{0}: Title = {1}, Author = {2}", i + 1, books(i).title, books(i).author)
Next
Console.WriteLine()
End If
Case "3"
Console.WriteLine("Enter the name of the book")
search = Console.ReadLine()
found = False
For i As Integer = 0 To amount - 1
If books(i).title = search Then
Console.WriteLine("Book {0} found", books(i).title)
found = True
End If
Next
If Not found Then Console.WriteLine("Not found!")
Console.WriteLine()
Case "4"
Console.WriteLine("Enter the search string")
search = Console.ReadLine()
found = False
For i As Integer = 0 To amount - 1
If books(i).title.ToUpper().Contains(search.ToUpper()) Then books(i).author.ToUpper().Contains(search.ToUpper())
Next
Console.WriteLine("{0} found in {1}", search, books(i).title)
found = True
Console.WriteLine()
If Not found Then Console.WriteLine("Not found!")
Case "5"
If amount = 0 Then
Console.WriteLine("No data to delete")
Else
Console.WriteLine("Enter the number of book to delete (1 to {0})", amount)
Dim posToDelete As Integer = Convert.ToInt32(Console.ReadLine()) - 1
For i As Integer = posToDelete To amount - 1 - 1
books(i) = books(i + 1)
Next
amount -= 1
End If
Case "0"
repeat = False
Case Else
Console.WriteLine()
Console.WriteLine("Wrong option. Please re-enter" & vbLf)
End Select
Loop While repeat
End Sub
End Class