Books database VB.Net Exercise - Visual Basic Programming Course


 Exercise

Books database

Objetive

Create a small database, which will be used to store data about books. For a certain book, we want to keep the following information:

Title
Author
The program must be able to store 1000 books, and the user will be allowed to:

Add data for one book
Display all the entered books (just title and author, in the same line)
Search for the book(s) with a certain title
Delete a book at a known position (for example, book number 6)
Exit the program

Hint: to delete an item in an array, you must move backwards every item which was placed after it, and the decrease the counter.

Code

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

More VB.Net Exercises of Arrays, Structures and Strings

 Reverse array
Write a Visual Basic (VB.Net) program to ask the user for 5 numbers, store them in an array and show them in reverse order....
 Search in array
Write a Visual Basic (VB.Net) program that says if a data belongs in a list that was previously created. The steps to take are: - Ask the user how...
 Array of even numbers
Write a Visual Basic (VB.Net) program to ask the user for 10 integer numbers and display the even ones....
 Array of positive and negative numbers
Write a Visual Basic (VB.Net) program to ask the user for 10 real numbers and display the average of the positive ones and the average of the negative...
 Many numbers and sum
Write a Visual Basic (VB.Net) program which asks the user for several numbers (until he enters "end" and displays their sum). When the execution is go...
 Two dimensional array
Write a Visual Basic (VB.Net) program to ask the user for marks for 20 pupils (2 groups of 10, using a two-dimensional array), and display the average...
 Statistics V2
Write a Visual Basic (VB.Net) statistical program which will allow the user to: - Add new data - See all data entered - Find an item, to see whe...
 Struct
Write a Visual Basic (VB.Net) Struct to store data of 2D points. The fields for each point will be: x coordinate (short) y coordinate (short) r (...
 Array of struct
Write a Visual Basic (VB.Net) program that expand the previous exercise (struct point), so that up to 1.000 points can be stored, using an "array of s...
 Array of struct and menu
Write a Visual Basic (VB.Net) program that expand the previous exercise (array of points), so that it displays a menu, in which the user can choose to...
 Triangle V2
Write a Visual Basic (VB.Net) program to ask the user for his/her name and display a triangle with it, starting with 1 letter and growing until it has...
 Rectangle V3
Write a Visual Basic (VB.Net) program to ask the user for his/her name and a size, and display a hollow rectangle with it: Enter your name: Yo Ent...
 Centered triangle
Write a Visual Basic (VB.Net) program that Display a centered triangle from a string entered by the user: __a__ _uan_ Juan...
 Cities database
Create a database to store information about cities. In a first approach, we will store only the name of each city and the number of inhabitants, a...
 Banner
Write a Visual Basic (VB.Net) program to imitate the basic Unix SysV "banner" utility, able to display big texts....
 Triangle right side
Write a Visual Basic (VB.Net) program that asks the user for a string and displays a right-aligned triangle: ____n ___an __uan Juan...
 Strings manipulation
Write a Visual Basic (VB.Net) program that asks the user for a string and: - Replace all lowercase A by uppercase A, except if they are preceded wi...
 Nested structs
Write a Visual Basic (VB.Net) Struct to store two data for a person: name and date of birth. The date of birth must be another struct consisti...
 Sort data
Write a Visual Basic (VB.Net) program to ask the user for 10 integer numbers (from -1000 to 1000), sort them and display them sorted....
 Two dimensional array as buffer for screen
Write a Visual Basic (VB.Net) program that declares a 70x20 two-dimensional array of characters, "draws" 80 letters (X, for example) in random positio...
 Two dimensional array 2: circunference on screen
Write a Visual Basic (VB.Net) program that declares creates a 70x20 two-dimensional array of characters, "draws" a circumference or radius 8 inside it...
 Computer programs
Write a Visual Basic (VB.Net) program that can store up to 1000 records of computer programs. For each program, you must keep the following data: *...
 Exercise tasks
Write a Visual Basic (VB.Net) program that can store up to 2000 "to-do tasks". For each task, it must keep the following data: • Date (a set of 3 d...
 Household accounts
Write a Visual Basic (VB.Net) program in Visual Basic (VB.Net) that can store up to 10000 costs and revenues, to create a small domestic accounting sy...


Juan A. Ripoll - Systems Tutorials and Programming Courses © 2024 All rights reserved.  Legal Conditions.