1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20
function amortization_array($principal, $interest_rate, $start_year, $start_month, $term, $group_by = 'M, Y', $month_names){
$periods = $term * 12;
$balance = $principal;
$interest = $interest_rate / 100 / 12;
$payment = $principal * $interest / (1 - pow((1 + $interest), -$periods));
for($period = 0; $period < $periods; $period++){
$date = get_date($start_year, $start_month + $period, $group_by, $month_names);
$results[$date]['date'] = $date;
$results[$date]['p'] = $period + 1;
$results[$date]['year'] = substr($date, -4);
$results[$date]['interest'] += $balance * $interest;
$results[$date]['principal'] += $payment - $balance * $interest;
$results[$date]['balance'] = $balance = $balance - $payment + $balance * $interest;
if($results[$date]['balance'] < 0) $results[$date]['balance'] = 0;
}
return $results;
}
Refactorings
No refactoring yet !
rikkus
September 25, 2008, September 25, 2008 16:59, permalink
You're confusing the terms 'refactor' and 'reimplement'
macournoyer
September 26, 2008, September 26, 2008 18:00, permalink
yo guys, what's up with all the hate?
You need sweat and tender loving.
xx
Chris Dean
October 28, 2008, October 28, 2008 07:34, permalink
Maybe you should post this under the java category?
That said I'd expect it to work pretty much as is, just with a change to how you reference the variables php uses $varname, not sure how java does this...
Hope that helps
How would you refactor this in java?