Menu
Coddy logo textTech

Recap - User Manager

Part of the Object Oriented Programming section of Coddy's Lua journey — lesson 68 of 70.

challenge icon

Challenge

Easy

Let'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 an id and a name. 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, or nil if 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:

  1. First user's ID (a number)
  2. First user's name
  3. Second user's ID (a number)
  4. Second user's name
  5. ID of a user to search for (a number)

In your main file:

  1. Create a UserManager and add both users
  2. Call :listUsers() to print all users
  3. Remove the first user (using the first ID)
  4. Print "After removal:"
  5. Call :listUsers() again
  6. 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: Bob

If 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 found

Notice 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