Group
C# Arrays, Structures and Strings
Objective
1. Define a struct or class to store the book data, including title and author.
2. Create an array that can hold up to 1,000 books.
3. Implement the following menu options:
- Add a new book
- Display all entered books
- Search for a book by title
- Delete a book at a specific position (book number)
- Exit the program
4. Implement logic to add, display, search, and delete books.
5. Use proper array manipulation to remove a book and shift the remaining items.
6. Ensure user input is validated to prevent errors.
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 then decrease the counter.
Example C# Exercise
Show C# Code
using System;
class Program
{
// Define a Book struct to hold the title and author of a book
struct Book
{
public string Title;
public string Author;
}
static void Main()
{
const int MAX_BOOKS = 1000;
Book[] books = new Book[MAX_BOOKS];
int count = 0;
while (true)
{
Console.WriteLine("\nMenu:");
Console.WriteLine("1. Add a new book");
Console.WriteLine("2. Display all 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: ");
string option = Console.ReadLine();
switch (option)
{
case "1":
if (count < MAX_BOOKS)
{
AddBook(books, ref count);
}
else
{
Console.WriteLine("Error: Maximum number of books reached.");
}
break;
case "2":
DisplayBooks(books, count);
break;
case "3":
SearchBookByTitle(books, count);
break;
case "4":
DeleteBook(books, ref count);
break;
case "5":
Console.WriteLine("Exiting the program.");
return;
default:
Console.WriteLine("Invalid option. Please try again.");
break;
}
}
}
// Method to add a new book
static void AddBook(Book[] books, ref int count)
{
Console.Write("Enter book title: ");
string title = Console.ReadLine();
Console.Write("Enter book author: ");
string author = Console.ReadLine();
books[count] = new Book { Title = title, Author = author };
count++;
Console.WriteLine("Book added successfully.");
}
// Method to display all entered books
static void DisplayBooks(Book[] books, int count)
{
if (count == 0)
{
Console.WriteLine("No books available.");
return;
}
Console.WriteLine("Books List:");
for (int i = 0; i < count; i++)
{
Console.WriteLine($"{i + 1}. {books[i].Title} by {books[i].Author}");
}
}
// Method to search for a book by title
static void SearchBookByTitle(Book[] books, int count)
{
Console.Write("Enter the title to search for: ");
string searchTitle = Console.ReadLine();
bool found = false;
for (int i = 0; i < count; i++)
{
if (books[i].Title.Equals(searchTitle, StringComparison.OrdinalIgnoreCase))
{
Console.WriteLine($"Found: {books[i].Title} by {books[i].Author}");
found = true;
}
}
if (!found)
{
Console.WriteLine("No books found with that title.");
}
}
// Method to delete a book by position (index)
static void DeleteBook(Book[] books, ref int count)
{
Console.Write("Enter the book number to delete: ");
if (int.TryParse(Console.ReadLine(), out int bookNumber) && bookNumber > 0 && bookNumber <= count)
{
// Move all books after the deleted one backwards in the array
for (int i = bookNumber - 1; i < count - 1; i++)
{
books[i] = books[i + 1];
}
count--; // Decrease the total book count
Console.WriteLine("Book deleted successfully.");
}
else
{
Console.WriteLine("Invalid book number.");
}
}
}
Output
Menu:
1. Add a new book
2. Display all books
3. Search for a book by title
4. Delete a book by position
5. Exit
Choose an option: 1
Enter book title: The Great Gatsby
Enter book author: F. Scott Fitzgerald
Book added successfully.
Menu:
1. Add a new book
2. Display all books
3. Search for a book by title
4. Delete a book by position
5. Exit
Choose an option: 1
Enter book title: 1984
Enter book author: George Orwell
Book added successfully.
Menu:
1. Add a new book
2. Display all books
3. Search for a book by title
4. Delete a book by position
5. Exit
Choose an option: 2
Books List:
1. The Great Gatsby by F. Scott Fitzgerald
2. 1984 by George Orwell
Menu:
1. Add a new book
2. Display all books
3. Search for a book by title
4. Delete a book by position
5. Exit
Choose an option: 3
Enter the title to search for: 1984
Found: 1984 by George Orwell
Menu:
1. Add a new book
2. Display all books
3. Search for a book by title
4. Delete a book by position
5. Exit
Choose an option: 4
Enter the book number to delete: 1
Book deleted successfully.
Menu:
1. Add a new book
2. Display all books
3. Search for a book by title
4. Delete a book by position
5. Exit
Choose an option: 2
Books List:
1. 1984 by George Orwell