Books Database - C# Programming Exercise

In this C# exercise, you are asked to create a small database that will be used to store information about books. For each book, the following data should be kept:

- Title.
- Author.

The program must be able to store up to 1000 books, and the user will be able to perform the following actions:

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

It is important to note that to delete an item in an array, you must move every item placed after it backwards, and then decrease the counter. This exercise will help you improve your skills in array manipulation and handling data within a simple database structure.

 Category

Arrays, Structures and Strings

 Exercise

Books Database

 Objective

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.

 Write Your C# Exercise

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

 Share this C# exercise

 More C# Programming Exercises of Arrays, Structures and Strings

Explore our set of C# programming exercises! Specifically designed for beginners, these exercises will help you develop a solid understanding of the basics of C#. From variables and data types to control structures and simple functions, each exercise is crafted to challenge you incrementally as you build confidence in coding in C#.

  •  Triangle V2

    In this C# exercise, you are asked to create a program that asks the user for their name and then displays a triangle made from that name, starting with 1 letter and ...

  •  Rectangle V3

    In this C# exercise, you are asked to create a program that asks the user for their name and a size, then displays a hollow rectangle made from that name.

  •  Centered triangle

    In this C# exercise, you are asked to create a program that asks the user for their name and a size, then displays a hollow rectangle made from t...

  •  Cities database

    In this exercise, you are asked to create a database to store information about cities. In the first approach, you will store only the name...

  •  Banner

    This Exercise in C# involves creating a program that imitates the basic Unix SysV "banner" utility, allowing you to display large texts in a similar manner. The purpo...

  •  Triangle right side

    This Exercise in C# involves creating a program that asks the user for a text string and displays a right-aligned triangle using that string. The triang...