Port C++ Room Objects
Lesson 48 of 48 in Coddy's Lua for C++ Developers course.
Challenge
MediumPort the C++ reference. First input line is nonnegative room capacity; remaining lines are signed integer reservation requests. Accept only strictly positive requests that fit. Failed requests leave used seats unchanged. Return remaining:accepted. Implement Model.new(capacity) and Model:reserve(seats) in model.lua; reserve returns a boolean. Use a fresh object per run.
Edit app.lua, model.lua. The visible locked main.lua reads stdin into a sequence of lines, calls app.run(lines), and prints its returned value with one final newline. Do not print diagnostics or change the module names. Empty input gives an empty sequence. All numeric inputs are small enough to avoid overflow.
Public API checks: Model.new(capacity) exposes capacity and used, initially zero. reserve returns a boolean and updates used only for a positive request that fits. Rooms are independent. The locked driver checks this behavior before calling app.run. These checks print nothing when successful.
C++17 reference
#include <iostream>
class Room {
long long capacity_;
long long used_ = 0;
public:
explicit Room(long long capacity) : capacity_(capacity) {}
bool reserve(long long seats) {
if (seats <= 0 || seats > capacity_ - used_) return false;
used_ += seats;
return true;
}
long long remaining() const { return capacity_ - used_; }
};
int main() {
long long capacity;
if (!(std::cin >> capacity)) return 1;
Room room(capacity);
long long seats;
int accepted = 0;
while (std::cin >> seats) {
if (room.reserve(seats)) ++accepted;
}
std::cout << room.remaining() << ':' << accepted << '\n';
}The C++ class keeps state private and exposes a const observer. The Lua contract exposes capacity and used for the driver checks, while reservation behavior remains the same. Allocate each room independently; a failed request must leave its state unchanged. Do not port the C++ conditional by using Lua logical operators as a boolean-to-integer conversion.
Try it yourself
-- Public API checks
do
local Model=require("model")
local a,b=Model.new(5),Model.new(2)
assert(a:reserve(6)==false and a.used==0,"oversized reservation must not mutate")
assert(a:reserve(0)==false and a:reserve(-1)==false and a.used==0,"nonpositive requests are rejected")
assert(a:reserve(2)==true and a.used==2,"accepted seats update used")
assert(a:reserve(3)==true and a.used==5 and a.capacity==5,"exact fit succeeds without changing capacity")
assert(b.used==0 and b.capacity==2,"rooms must be independent")
end
local app = require("app")
local lines = {}
for line in io.lines() do lines[#lines + 1] = line end
print(app.run(lines))
All lessons in Lua for C++ Developers
1First Lua Programs
Run Lua ModulesPrint and CommentNames and Local ScopeNumbers and ConversionStrings and Byte IndexesRecap: Format a Receipt4Functions and Closures
Functions and ReturnsDefaults and Options TablesMultiple Results and VarargsCallbacks and Captured StateRecap: Pricing Functions2Values and Decisions
Absence and Missing FieldsTruth and Default ValuesBranches and BoundsEquality and IdentityRecap: Admission RulesPractice on your own: Online Lua compiler