Sobes.tech
Junior — Senior

Calculation of net profit considering agent commissions and their subordinates

livecode

Task condition

It is necessary to implement the getProfit() method, which calculates the company's net profit after distributing percentages (commissions) to all participants in the agent chain, including their sub-agents.

class Agent {
    public $childAgent; // reference to sub-agent
    public $commission; // agent's commission percentage
}

class Policy {
    public $policyPrice; // policy cost
    public function getProfit(Agent $rootAgent) {
        // implementation of profit calculation
    }
}

The method should recursively traverse all levels of nested agents, subtract their commissions from the policy price, and return the remaining part as the company's profit.