Menu
Coddy logo textTech

Console UI / Admin Interface

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

challenge icon

Challenge

Easy

Let's complete your Library System by building a console interface that brings all your components together! You'll create an admin interface that processes commands to manage the library - adding books, registering users, handling borrowing operations, and searching the catalog.

You'll continue building on your existing project structure:

  • Book.cs: Keep your Book class with Id, Title, Author, IsAvailable, and the Borrow() and Return() methods.
  • User.cs: Keep your User class with borrowing tracking, MaxBooks constant, CanBorrow(), and methods for managing borrowed books.
  • Library.cs: Keep your Library class with all the borrowing logic and LINQ search methods you've built. Add a new method GetAllBooks() that returns all books in the library, and GetAllUsers() that returns all registered users.
  • Program.cs: This is where your console interface comes to life! You'll build a command processor that reads commands and executes the appropriate library operations. Think of this as the admin dashboard for your library system.

Your console interface should handle these commands:

  • ADD_BOOK|id|title|author - Adds a new book and prints Added: {Title}
  • ADD_USER|name|memberId - Registers a new user and prints Registered: {Name}
  • BORROW|bookId|memberId - Processes a borrow request (use your existing BorrowBook logic)
  • RETURN|bookId|memberId - Processes a return request (use your existing ReturnBook logic)
  • SEARCH|type|value - Searches the catalog where type is title, author, available, or borrowed
  • STATUS - Prints a summary showing total books, available books, total users, and total borrowed books

For the STATUS command, print the summary in this format:

--- Library Status ---
Total Books: {count}
Available: {count}
Total Users: {count}
Books Borrowed: {count}

For search results, print each book as {Title} by {Author} on its own line, or No books found if empty.

You will receive the following inputs:

  • Library name
  • Number of commands to process
  • Each command on its own line in the formats described above

For example, if the inputs are:

Downtown Library
8
ADD_BOOK|B001|Clean Code|Robert Martin
ADD_BOOK|B002|The Pragmatic Programmer|David Thomas
ADD_USER|Alice|101
ADD_USER|Bob|102
BORROW|B001|101
SEARCH|available
BORROW|B002|102
STATUS

The output should be:

Added: Clean Code
Added: The Pragmatic Programmer
Registered: Alice
Registered: Bob
Alice borrowed Clean Code
The Pragmatic Programmer by David Thomas
Bob borrowed The Pragmatic Programmer
--- Library Status ---
Total Books: 2
Available: 0
Total Users: 2
Books Borrowed: 2

Your console interface acts as the glue that connects user input to your well-organized library components. Each command delegates to the appropriate method in your Library class, demonstrating how a clean architecture makes building user interfaces straightforward!

Try it yourself

using System;
using System.Collections.Generic;

namespace LibrarySystem
{
    class Program
    {
        public static void Main(string[] args)
        {
            // Read library name
            string libraryName = Console.ReadLine();
            
            // Create a Library instance with the library name
            Library library = new Library(libraryName);
            
            // Read number of commands
            int numCommands = Convert.ToInt32(Console.ReadLine());
            
            // Process each command
            for (int i = 0; i < numCommands; i++)
            {
                string commandLine = Console.ReadLine();
                string[] parts = commandLine.Split('|');
                string command = parts[0];
                
                // TODO: Handle ADD_BOOK command
                // Format: ADD_BOOK|id|title|author
                // Print: Added: {title}
                
                // TODO: Handle ADD_USER command
                // Format: ADD_USER|name|memberId
                // Print: Registered: {name}
                
                // TODO: Handle BORROW command
                // Format: BORROW|bookId|memberId
                // Print the result from library.BorrowBook()
                
                // TODO: Handle RETURN command
                // Format: RETURN|bookId|memberId
                // Print the result from library.ReturnBook()
                
                // TODO: Handle SEARCH command
                // Format: SEARCH|type|value (type can be title, author, available, borrowed)
                // Print each book as "{Title} by {Author}" or "No books found"
                
                // TODO: Handle STATUS command
                // Print:
                // --- Library Status ---
                // Total Books: {count}
                // Available: {count}
                // Total Users: {count}
                // Books Borrowed: {count}
            }
        }
    }
}

All lessons in Object Oriented Programming