Checking out
Write the checkout flow with Checkout and Return methods.
The checkout flow
In this lesson you will add Checkout and Return methods to the Library class. Checkout lends a book to a member: it sets the book's IsBorrowed flag to true and increments the member's BorrowedCount.
To avoid lending the same book twice, we first check with if (!b.IsBorrowed). Return does the reverse: it sets IsBorrowed to false and decrements the count. Together these methods form the library's core behavior.
Guarding against double checkout
Lending an already-borrowed book makes no sense. So at the top of Checkout we check with if (!b.IsBorrowed). The body runs only when the book is available.
Likewise Return starts with if (b.IsBorrowed), so a not-borrowed book cannot be returned. These small guards keep the data consistent. Later, if you want to throw, you can add a throw to the else branch.
Exercise Add Checkout and Return methods
Add public void Checkout(Member m, Book b) to the Library class. In the body check with if (!b.IsBorrowed); when true, set b.IsBorrowed = true; and m.BorrowedCount++;. Add public void Return(Member m, Book b) similarly; with if (b.IsBorrowed) set b.IsBorrowed = false; and m.BorrowedCount--;. In Main call lib.AddMember(member) and lib.Checkout(member, book); then print the result with Console.WriteLine(book.IsBorrowed).
Show example solution
Add to Library public void Checkout(Member m, Book b) { if (!b.IsBorrowed) { b.IsBorrowed = true; m.BorrowedCount++; } } and public void Return(Member m, Book b) { if (b.IsBorrowed) { b.IsBorrowed = false; m.BorrowedCount--; } }. In Main add lib.AddMember(member); lib.Checkout(member, book); Console.WriteLine(book.IsBorrowed);
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--;
}
}
}
class Program
{
static void Main()
{
var book = new Book { Title = "The Hobbit", Author = "Tolkien" };
var member = new Member { Name = "Alex" };
Console.WriteLine(book.Title);
Console.WriteLine(member.Name);
var lib = new Library();
lib.AddBook(new Book { Title = "Dune", Author = "Herbert" });
lib.ListBooks();
lib.AddMember(member);
lib.Checkout(member, book);
Console.WriteLine(book.IsBorrowed);
}
}
Test yourself
-
What should the Checkout method do?
if (!b.IsBorrowed) { b.IsBorrowed = true; // next step }- m.BorrowedCount = 0;
- m.BorrowedCount--;
- m.BorrowedCount++;
- m.BorrowedCount = null;
Show answer
m.BorrowedCount++; — Correct. The member's borrow count must increase.
-
The book's borrowed flag
b.IsBorrowed = ____;Show answer
b.IsBorrowed = true; -
Match each lending concept
- IsBorrowed
- Borrowed state
- BorrowedCount
- Member's borrow count
- Checkout
- Lends a book
- Return
- Takes a book back