gfucci's avatar

Attendance rotation algorithm

Hello! I have a system that has some services to buy, customers buy randomly at any time of the day, before the service is performed there is a pre-check of the registered information. The company has employees to do this work, each customer that comes in has an employee responsible for checking their information. I need to implement a service rotation, for example: If I have 3 employees online, then I have [1,2,3], when a customer buys, employee 1 checks the information and the queue becomes [2,3,1], now the next customer to buy will be the attendant 2 getting [3, 1, 2]. .. and so on. Does anyone have any ideas on how I can implement this?? The algorithm needs to work regardless of the size of the array.

0 likes
1 reply
LaryAI's avatar
LaryAI
AI
Best Answer
Level 58

Certainly! What you're describing is a round-robin scheduling algorithm. This is a simple and fair way to distribute tasks among a set of resources (in your case, employees). Here's a basic implementation in PHP, which you can adapt to your specific needs:

class RoundRobinScheduler {
    private $queue;

    public function __construct($employees) {
        // Initialize the queue with the employees
        $this->queue = $employees;
    }

    public function getNextEmployee() {
        // Take the first employee from the queue
        $employee = array_shift($this->queue);
        // Add the employee back to the end of the queue
        $this->queue[] = $employee;
        // Return the employee
        return $employee;
    }

    public function getQueue() {
        // Return the current state of the queue
        return $this->queue;
    }
}

// Example usage:
$employees = [1, 2, 3]; // Array of employee IDs
$scheduler = new RoundRobinScheduler($employees);

// Simulate customer service
for ($i = 0; $i < 6; $i++) {
    $employeeId = $scheduler->getNextEmployee();
    echo "Customer $i is being served by employee $employeeId\n";
    echo "Queue is now: " . implode(', ', $scheduler->getQueue()) . "\n";
}

This script will output something like:

Customer 0 is being served by employee 1
Queue is now: 2, 3, 1
Customer 1 is being served by employee 2
Queue is now: 3, 1, 2
Customer 2 is being served by employee 3
Queue is now: 1, 2, 3
Customer 3 is being served by employee 1
Queue is now: 2, 3, 1
Customer 4 is being served by employee 2
Queue is now: 3, 1, 2
Customer 5 is being served by employee 3
Queue is now: 1, 2, 3

This demonstrates that each customer is being served by employees in a round-robin fashion. You can integrate this logic into your system, ensuring that when an employee becomes available (or "online"), they are added to the queue, and when they are no longer available, they are removed from the queue.

Please or to participate in this conversation.