Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Select an option

  • Save SidIcarus/5df57d38bf7f9f40d99baf8994c5a1d2 to your computer and use it in GitHub Desktop.

Select an option

Save SidIcarus/5df57d38bf7f9f40d99baf8994c5a1d2 to your computer and use it in GitHub Desktop.
lumion-implementation-challenge-Q1-2025.php
<?php
use Carbon\CarbonImmutable;
class PaymentScheduleCalculator {
/**
* add an implementation here that calculates a payment schedule, returned as an array
* of CarbonImmutable dates and float amounts
* @return array<array{date: CarbonImmutable, amount: float}>
*/
public static function calculatePaymentSchedule(
float $total,
CarbonImmutable|string $first_payment_date,
string $payment_frequency, /** 'weekly, 'bi-weekly', 'monthly' */
int $number_of_payments,
): array {
// validate $total and $number_of_payments are <=0
if ($total <= 0 || $number_of_payments <= 0) {
// throw InvalidArgumetException
}
// $payment_frequency will be one of 'weekly', 'bi-weekly', or 'monthly'
// validate payment_frequency type
// throw InvalidArgumetException
try {
$firstDate = Carbon::parse($first_payment_date);
$baseAmount = $total / $number_of_payments;
$remainder = $total - ($baseAmount * $number_of_payments);
$amount = $baseAmount;
// add loop for aggregating paymentDates around this
// loop
for ($i = 0; $i < $number_of_payments; $i++) {
// strategy for $payment_frequency type
switch ($payment_frequency) {
case 'weekly':
$paymentDate = Carbon::calculatePaymentSchedule($firstDate, 1);
break;
case 'bi-weekly':
$paymentDate = Carbon::calculatePaymentSchedule($firstDate, 2); // week
break;
case 'monthly':
$paymentDate = Carbon::calculatePaymentSchedule($firstDate, 4); // monthy
break;
}
}
if ($i === $number_of_payments - 1) {
$amount += $remainder;
}
// the count of the values in the returned array should equal $number_of_payments
$returnData = [
// the first date in the returned array should be the $first_payment_date
$paymentDate,
$amount,
// the sum of the values in the returned array should equal $total
];
return $returnData;
} catch (\Exception $e) { // if the payment schedule cannot be calculated, an exception should be thrown
// throw RuntimeException();
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment