Recap - User Manager
Part of the Object Oriented Programming section of Coddy's Lua journey — lesson 68 of 70.
Challenge
EasyLet's build a user management system—a practical application you'd find in many real-world programs! You'll create two classes that work together: one representing individual users, and another that manages a collection of them efficiently.
You'll organize your code across three files:
User.lua: Create a User class that represents a single user in the system. Each user has anidand aname. Include a:getInfo()method that returns a string in the format"ID: {id}, Name: {name}".UserManager.lua: Build a manager class that handles a collection of users. Think about how to store users for efficient lookup—using the user's ID as a table key makes finding and removing users much simpler than searching through a list. Your manager needs::addUser(user)— adds a User object to the collection:removeUser(id)— removes a user by their ID:findUser(id)— returns the User with the matching ID, ornilif not found:listUsers()— prints the info for all users, one per line
main.lua: Bring everything together! Create a UserManager, add some users based on the inputs you receive, perform operations, and display the results.
You will receive five inputs:
- First user's ID (a number)
- First user's name
- Second user's ID (a number)
- Second user's name
- ID of a user to search for (a number)
In your main file:
- Create a UserManager and add both users
- Call
:listUsers()to print all users - Remove the first user (using the first ID)
- Print
"After removal:" - Call
:listUsers()again - Search for the user with the fifth input ID—if found, print their info; if not found, print
"User not found"
For example, if the inputs are 101, Alice, 102, Bob, and 102, the output should be:
ID: 101, Name: Alice
ID: 102, Name: Bob
After removal:
ID: 102, Name: Bob
ID: 102, Name: BobIf the inputs are 201, Charlie, 202, Diana, and 201, the output should be:
ID: 201, Name: Charlie
ID: 202, Name: Diana
After removal:
ID: 202, Name: Diana
User not foundNotice in the second example, after removing user 201 (Charlie), searching for that ID returns "User not found" since they're no longer in the system!
Try it yourself
-- main.lua: Bring everything together
local User = require('User')
local UserManager = require('UserManager')
-- Read inputs
local id1 = tonumber(io.read())
local name1 = io.read()
local id2 = tonumber(io.read())
local name2 = io.read()
local searchId = tonumber(io.read())
-- TODO: Create a UserManager instance
-- TODO: Create two User objects with the input data
-- TODO: Add both users to the manager
-- TODO: Call listUsers() to print all users
-- TODO: Remove the first user (using id1)
-- TODO: Print "After removal:"
-- TODO: Call listUsers() again
-- TODO: Search for the user with searchId
-- If found, print their info; if not found, print "User not found"
All lessons in Object Oriented Programming
1The 'Self' Concept
Tables with FunctionsExplicit 'self'The Colon SyntaxDot vs ColonRecap - Moving Point4Project: Digital Bank
Project SetupDeposit Method7Polymorphism & Overriding
Overriding MethodsCalling Parent MethodsDuck TypingCommon InterfaceChecking TypeRecap - Employee Roles10Project: Shape Manager
Project SetupRectangle Class2Class Prototype Pattern
The Prototype ConceptLinking with __indexThe :new() ConstructorInitializing AttributesIndependent InstancesRecap - Car Factory5Operator Overloading in OOP
Adding ObjectsSubtracting ObjectsConcatenating ObjectsComparing Objects (<, >)Recap - Wallet Math8Encapsulation
Naming ConventionsClosures for PrivacyAccess via ClosuresRead-Only TablesValidation LogicRecap - Secure Vault11Design Patterns (Lite)
Factory FunctionsSingleton TableIterator PatternObserver (Listener)Recap - Logger Factory3Object State and Behavior
Instance VariablesGetter MethodsSetter MethodsCalculated PropertiesFormatting StringsEquality ChecksRecap - Student Grade6Inheritance Basics
The Inheritance SetupInheriting MethodsExtending the ConstructorAdding Child MethodsShared vs UniqueRecap - Shape Hierarchy