Console UI / Admin Interface
Part of the Object Oriented Programming section of Coddy's C# journey — lesson 66 of 70.
Challenge
EasyLet'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 withId,Title,Author,IsAvailable, and theBorrow()andReturn()methods.User.cs: Keep your User class with borrowing tracking,MaxBooksconstant,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 methodGetAllBooks()that returns all books in the library, andGetAllUsers()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 printsAdded: {Title}ADD_USER|name|memberId- Registers a new user and printsRegistered: {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 istitle,author,available, orborrowedSTATUS- 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
STATUSThe 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: 2Your 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
1Fundamentals of OOP
External FilesNamespaces & DirectivesIntro to Classes & ObjectsThe 'this' KeywordMethods and ParametersFields vs PropertiesConstructorsObject InitializersRecap - Simple Calculator4Inheritance
Basic Inheritance (:) SyntaxThe 'base' KeywordVirtual & Override KeywordsSealed ClassesThe 'object' Base ClassRecap - Employee Hierarchy7Advanced Features
Operator OverloadingIndexers (this[])ToString() OverrideExtension MethodsRecap - Custom List2Properties & Static Members
Auto-Implemented PropertiesRead/Write-Only PropertiesStatic Fields & MethodsStatic ClassesExpression-Bodied Members5Polymorphism & Interfaces
Compile vs Runtime PolyInterface vs Abstract ClassMultiple InterfacesExplicit InterfacesUpcasting & DowncastingRecap - Shape Calculator3Class Architecture
Instance vs Static Data'readonly' & 'const' KeywordsBacking FieldsRecap - Bank Account Manager6Encapsulation
Access ModifiersProperties for EncapsulationData Hiding ImplementationImmutability PatternsRecap - Student Records12Project: Library System
Project StructureBook and User Models