Books database C# Exercise - C# 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.

 Example Code

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;
            }
        }
    }
}

More C# Exercises of Arrays, Structures and Strings

 Reverse array
Write a C# program to ask the user for 5 numbers, store them in an array and show them in reverse order....
 Search in array
Write a C# program that says if a data belongs in a list that was previously created. The steps to take are: - Ask the user how many data will he ...
 Array of even numbers
Write a C# program to ask the user for 10 integer numbers and display the even ones....
 Array of positive and negative numbers
Write a C# program to ask the user for 10 real numbers and display the average of the positive ones and the average of the negative ones....
 Many numbers and sum
Write a C# program which asks the user for several numbers (until he enters "end" and displays their sum). When the execution is going to end, it must...
 Two dimensional array
Write a C# program to ask the user for marks for 20 pupils (2 groups of 10, using a two-dimensional array), and display the average for each group....
 Statistics V2
Write a C# statistical program which will allow the user to: - Add new data - See all data entered - Find an item, to see whether it has been en...
 Struct
Write a C# Struct to store data of 2D points. The fields for each point will be: x coordinate (short) y coordinate (short) r (red colour, byte) ...
 Array of struct
Write a C# program that expand the previous exercise (struct point), so that up to 1.000 points can be stored, using an "array of struct". Ask the use...
 Array of struct and menu
Write a C# program that expand the previous exercise (array of points), so that it displays a menu, in which the user can choose to: - Add data for...
 Triangle V2
Write a C# program to ask the user for his/her name and display a triangle with it, starting with 1 letter and growing until it has the full length: ...
 Rectangle V3
Write a C# program to ask the user for his/her name and a size, and display a hollow rectangle with it: Enter your name: Yo Enter size: 4 YoYoYoY...
 Centered triangle
Write a C# 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 C# program to imitate the basic Unix SysV "banner" utility, able to display big texts....
 Triangle right side
Write a C# program that asks the user for a string and displays a right-aligned triangle: ____n ___an __uan Juan...
 Strings manipulation
Write a C# program that asks the user for a string and: - Replace all lowercase A by uppercase A, except if they are preceded with a space - Displ...
 Nested structs
Write a C# Struct to store two data for a person: name and date of birth. The date of birth must be another struct consisting on day, month an...
 Sort data
Write a C# 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 C# program that declares a 70x20 two-dimensional array of characters, "draws" 80 letters (X, for example) in random positions and displays the...
 Two dimensional array 2: circunference on screen
Write a C# program that declares creates a 70x20 two-dimensional array of characters, "draws" a circumference or radius 8 inside it, and displays it o...
 Computer programs
Write a C# program that can store up to 1000 records of computer programs. For each program, you must keep the following data: * Name * Category ...
 Exercise tasks
Write a C# program that can store up to 2000 "to-do tasks". For each task, it must keep the following data: • Date (a set of 3 data: day, month and...
 Household accounts
Write a C# program in C# that can store up to 10000 costs and revenues, to create a small domestic accounting system. For each expense (or income), sh...

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