Menu
Coddy logo textTech

Book and User Models

Part of the Object Oriented Programming section of Coddy's C# journey — lesson 63 of 70.

challenge icon

Challenge

Easy

Let's expand your Library System by building out the Book and User models with richer functionality. You'll add status tracking for books, borrowing capabilities for users, and proper encapsulation to keep your data safe.

You'll continue working with your four-file structure:

  • Book.cs: Enhance your Book class with a unique identifier and status tracking. Each book should have an Id (string), Title (string), Author (string), and IsAvailable (bool, defaulting to true). Add methods Borrow() and Return() that change the availability status and return a message indicating success or failure. If someone tries to borrow an unavailable book, return "{Title} is not available". If they try to return an already available book, return "{Title} was not borrowed". Successful operations should return "{Title} has been borrowed" or "{Title} has been returned".
  • User.cs: Expand your User class to track borrowed books. Include Name (string), MemberId (int), and a private list to store borrowed book IDs. Add a BorrowedCount read-only property that returns how many books the user currently has. Include methods AddBorrowedBook(string bookId) and RemoveBorrowedBook(string bookId) to manage the user's borrowed books. Also add a HasBook(string bookId) method that returns true if the user has borrowed that specific book.
  • Library.cs: Your Library class should maintain collections of books and users. Include methods AddBook(Book book) and RegisterUser(User user) to populate the library. Add a GetBookById(string id) method that returns the book with that ID (or null if not found), and GetUserById(int memberId) that works similarly for users.
  • Program.cs: Bring everything together by processing library operations based on input.

You will receive the following inputs:

  • Library name
  • Number of books to add
  • For each book: id|title|author
  • Number of users to register
  • For each user: name|memberId
  • Number of operations to perform
  • For each operation: action|bookId|memberId where action is either borrow or return

For each operation, print the result of the borrow or return action. After all operations, print a summary for each user showing their name and how many books they currently have borrowed in the format {Name}: {BorrowedCount} book(s).

For example, if the inputs are:

Downtown Library
2
B001|Clean Code|Robert Martin
B002|Design Patterns|Gang of Four
2
Alice|101
Bob|102
4
borrow|B001|101
borrow|B002|101
borrow|B001|102
return|B001|101

The output should be:

Clean Code has been borrowed
Design Patterns has been borrowed
Clean Code is not available
Clean Code has been returned
Alice: 1 book(s)
Bob: 0 book(s)

Notice how Alice successfully borrows two books, but when Bob tries to borrow Clean Code (which Alice already has), the operation fails. After Alice returns Clean Code, her count drops to 1. This demonstrates how your models work together to enforce library rules!

Try it yourself

using System;

namespace LibrarySystem
{
    class Program
    {
        public static void Main(string[] args)
        {
            // Read inputs
            string libraryName = Console.ReadLine();
            string bookInput = Console.ReadLine();
            string userInput = Console.ReadLine();

            // Parse book input (format: title|author)
            string[] bookParts = bookInput.Split('|');
            string bookTitle = bookParts[0];
            string bookAuthor = bookParts[1];

            // Parse user input (format: name|memberId)
            string[] userParts = userInput.Split('|');
            string userName = userParts[0];
            int memberId = Convert.ToInt32(userParts[1]);

            // Create a Library instance with the library name
            Library library = new Library(libraryName);

            // Create a Book instance with the title and author
            Book book = new Book(bookTitle, bookAuthor);

            // Create a User instance with the name and member ID
            User user = new User(userName, memberId);

            // Print the library info using GetInfo()
            Console.WriteLine(library.GetInfo());

            // Print the book info in format: Book: {Title} by {Author} - Available: {IsAvailable}
            Console.WriteLine($"Book: {book.Title} by {book.Author} - Available: {book.IsAvailable}");

            // Print the user info in format: User: {Name} (ID: {MemberId})
            Console.WriteLine($"User: {user.Name} (ID: {user.MemberId})");
        }
    }
}

All lessons in Object Oriented Programming