C# Booking Project
Build a capacity-limited reservation object that leaves state unchanged on every rejected request.
Your brief
Create one session with a validated capacity and a Reserve operation. A request must be positive and fit the remaining places. Rejection must not alter the previously accepted reservations. Keep the remaining-space check before mutation so overflow cannot create a misleading successful count. The type is a small single-process teaching object, not a multi-user booking service. A real service also needs authentication, persistent transactions, duplicate-request handling and concurrency control at the shared storage boundary.
Run it locally
Use the .NET 10 SDK with C# 14 in a separate console lab. Create an empty project once with dotnet new console --framework net10.0, then replace Program.cs with this complete source and add any supporting files shown. Keep only one lesson entry point in the project. C# does not currently run in Bhylu’s browser playground. Apache, PHP and MySQL are not needed.
dotnet build -p:LangVersion=14.0 -p:Nullable=enable -p:TreatWarningsAsErrors=true
dotnet run --no-buildUse the SDK’s project workflow; the review compiles against its matching reference assemblies and executes the results without external NuGet packages. File-writing examples belong in a private empty lab directory.
Worked solution
Show the worked solution
using System;
internal static class Program
{
private static void Main(string[] args)
{
var session = new BookingSession(8);
Console.WriteLine($"First: {session.Reserve(5)}");
Console.WriteLine($"Excess: {session.Reserve(4)}");
Console.WriteLine($"Exact: {session.Reserve(3)}");
Console.WriteLine($"Reserved: {session.Reserved}");
}
}
internal sealed class BookingSession
{
public int Capacity { get; }
public int Reserved { get; private set; }
public BookingSession(int capacity)
{
if (capacity is < 1 or > 1000) throw new ArgumentOutOfRangeException(nameof(capacity));
Capacity = capacity;
}
public bool Reserve(int requested)
{
if (requested < 1 || requested > Capacity - Reserved) return false;
Reserved += requested; return true;
}
}
Expected result
First: True
Excess: False
Exact: True
Reserved: 8
Five is accepted, four is rejected with five still reserved, and three exactly fills the session. The invariant bounds the subtraction and final addition. A failed call does not need rollback because the method validates before committing its one state change. Cancellation or persistence failures in a larger workflow require a separate transaction design.
Extend and test
Test zero, negative requests, exact remaining capacity, a request one too large, repeated calls after full capacity and invalid construction. Check unchanged state after every false return.
Consult Microsoft’s C# documentation for language and library contracts, including target-framework and version requirements.