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
C# Exercise Example
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;
}
}
}
}