C#
Putting it together
Combine the Library class with a console menu.
Putting it together with a console menu
In the final lesson we combine every piece behind a console menu. The user picks an option; if branches call either ListBooks or SearchByAuthor based on the choice. This makes every method reachable from one place.
We will simulate the menu with an int variable: int choice = 1; runs both branches in turn. In a real app you would read with Console.ReadLine, but here we keep the output deterministic by using a fixed value.
Deterministic menu output
For a testable app the output must be deterministic: every run gives the same result. To ensure this we set the menu choice to a fixed value: int choice = 1;. Then we set choice = 2; to enter the second branch too.
The first branch calls lib.ListBooks(); the second branch prints SearchByAuthor results in a foreach. So a single run shows both listing and search behavior. In a real menu, the value read from the user would run only one branch.
Exercise Call ListBooks and SearchByAuthor from a menu
In Main create a Library instance; add two books (one by Tolkien, one by Herbert). Print Console.WriteLine("Menu:") as a header. Set int choice = 1; and call if (choice == 1) lib.ListBooks();. Then set choice = 2; and inside if (choice == 2) use a foreach over lib.SearchByAuthor("Herbert") and print each result with Console.WriteLine("Found: " + b.Title).
Show example solution
"); write. int choice = 1; if (choice == 1) lib.ListBooks(); then choice = 2; if (choice == 2) { foreach (var b in lib.SearchByAuthor("Herbert")) Console.WriteLine("Found: " + b.Title); }.
Example solution:
using System;
using System.Collections.Generic;
class Book
{
public string Title { get; set; }
public string Author { get; set; }
public bool IsBorrowed { get; set; }
}
class Member
{
public string Name { get; set; }
public int BorrowedCount { get; set; }
}
class Library
{
private List<Book> _books = new List<Book>();
private List<Member> _members = new List<Member>();
public void AddBook(Book b) { _books.Add(b); }
public void AddMember(Member m) { _members.Add(m); }
public void ListBooks()
{
foreach (var b in _books)
{
Console.WriteLine(b.Title);
}
}
public void Checkout(Member m, Book b)
{
if (!b.IsBorrowed)
{
b.IsBorrowed = true;
m.BorrowedCount++;
}
}
public void Return(Member m, Book b)
{
if (b.IsBorrowed)
{
b.IsBorrowed = false;
m.BorrowedCount--;
}
}
public List<Book> SearchByAuthor(string author)
{
var result = new List<Book>();
foreach (var b in _books)
{
if (b.Author == author) result.Add(b);
}
return result;
}
}
class Program
{
static void Main()
{
var lib = new Library();
lib.AddBook(new Book { Title = "The Hobbit", Author = "Tolkien" });
lib.AddBook(new Book { Title = "Dune", Author = "Herbert" });
Console.WriteLine("Menu:");
int choice = 1;
if (choice == 1) lib.ListBooks();
choice = 2;
if (choice == 2)
{
foreach (var b in lib.SearchByAuthor("Herbert"))
{
Console.WriteLine("Found: " + b.Title);
}
}
}
}
The library app is ready
Congratulations! You finished the five-lesson library app. You now have Book and Member models, a Library class that manages them, a checkout flow, an author-based search and a console menu. This project shows how object-oriented C# fits together on a small example.
In the next groups we will dig deeper into generic classes and collections. You will learn how List<T> grows, how Dictionary hashes, and how to write your own iterators.
Test yourself
-
Which structure branches on the menu choice?
int choice = 1;
// ?
- if (choice == 1) { ... }
- foreach (choice) { ... }
- while (choice) { ... }
- return choice;
Show answer
if (choice == 1) { ... } — Correct. We branch with if based on the choice.
-
Match each menu piece to its job
- if
- Branches by choice
- choice
- Selection variable
- ListBooks
- Lists the books
- SearchByAuthor
- Filters by author
-
The equality check
if (choice ____ 1) lib.ListBooks();
Show answer
if (choice == 1) lib.ListBooks();
Next lesson: Generic class