Books Database C# Sharp Exercise


Books Database


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

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;
public class exercise82
    struct book
        public string title;
        public string author;

    public static void Main()  
        int capacity = 1000;  
        book [] books =new book [capacity];  
        bool repeat=true;  
        string option;     
        int amount=0;      
        string search;     
        bool found;       

            Console.WriteLine("Books database");
            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();

            switch (option)
                case "1": // Add a new book
                    if (amount < capacity )
                        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();
                        Console.WriteLine("Database full");

                case "2":  // Display all books
                    if (amount == 0)
                        for(int i = 0;i < amount;i++)
                        Console.WriteLine("{0}: Title = {1},  Author = {2}",
                        i+1, books[i].title, books[i].author);
                case "3":  // Exact search
                    Console.WriteLine("Enter the name of the book");
                    found = false;

                    for (int i = 0;i < amount;i++)
                        Console.WriteLine("Book {0} found",books[i].title);
                        found = true;

                    if (! found)
                    Console.WriteLine("Not found!");
                case "4":  // Partial search
                    Console.WriteLine("Enter the search string");
                    search = Console.ReadLine();
                    found = false;

                    for (int i = 0;i < amount;i++)
                        Console.WriteLine("{0} found in {1}",
                        search, books[i].title);
                        found = true;


                    if (! found)
                        Console.WriteLine("Not found!");
                case "5":  // Delete
                    if (amount == 0)
                        Console.WriteLine("No data to delete");
                        "Enter the number of book to delete (1 to {0})", amount);
                        int posToDelete = 
                        Convert.ToInt32(Console.ReadLine()) - 1;
                        for (int i = posToDelete; i < amount - 1; i++)
                        books[i] = books[i+1];
                case "0":  // End
                    Console.WriteLine("Wrong option. Please re-enter\n");

More C# Sharp Exercises of Arrays, Structures And Strings

Reverse Array
Create a C# program to ask the user for 5 numbers, store them in an array and show them in reverse order....
Search In Array
Create 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
Create 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
Create a 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 d...
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
reate a statistical program which will allow the user to: - Add new data - See all data entered - Find an item, to see whether it has been entere...
Create a "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
Expand the previous exercise (struct point), so that up to 1.000 points can be stored, using an "array of struct". Ask the user for data for the first...
Array Of Struct And Menu
Expand the previous exercise (array of points), so that it displays a menu, in which the user can choose to: - Add data for one point - Display al...
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
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...
Create a C# program to imitate the basic Unix SysV "banner" utility, able to display big texts....
Triangle Right Side
Create a C# program that asks the user for a string and displays a right-aligned triangle: ____n ___an __uan Juan...
Strings Manipulation
Create 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 - Disp...
Nested Structs
Create a 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 and ...
Sort Data
Create 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
Create a C# program that declares a 70x20 two-dimensional array of characters, "draws" 80 letters (X, for example) in random positions and displays th...
Two Dimensional Array 2: Circunference On Screen
Create a C# program that declares creates a 70x20 two-dimensional array of characters, "draws" a circumference or radius 8 inside it, and displays it ...
Computer Programs
Create 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
Create 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 an...
Household Accounts
Create a program in C# that can store up to 10000 costs and revenues, to create a small domestic accounting system. For each expense (or income), shou...