我喜欢一些,但不是所有肯尼的答案。我查找速率数组是一个好主意,但它应该只声明一次,而不是每次调用函数时都声明一次。电池短路
if
s确定信用评分指数是好的,但第二个条件是(
<=
)在逻辑上是不必要的。
我建议将查找数组定义为常量,以便它只声明一次,并具有全局范围。这是完全合适的,因为查找表中的金额在处理过程中不应更改。
当正确设计查找数组时,维护/扩展应该只涉及调整查找数组,而不涉及处理代码。
这使得您的php代码只需一步就可以传输到数据库表中,以防您的应用程序发现对数据维护或扩展的需求增加。
代码:(
Demo
)
define(
'TERM_RATES_BY_YEAR',
[
2001 => ['term' => 24, 'rates' => [15.90, 19.90, 22.90, 22.90, 22.90]],
2002 => ['term' => 24, 'rates' => [15.90, 19.90, 22.90, 22.90, 22.90]],
2003 => ['term' => 24, 'rates' => [15.90, 19.90, 22.90, 22.90, 22.90]],
2004 => ['term' => 24, 'rates' => [15.90, 19.90, 22.90, 22.90, 22.90]],
2005 => ['term' => 24, 'rates' => [15.90, 19.90, 22.90, 22.90, 22.90]],
2006 => ['term' => 24, 'rates' => [15.90, 18.90, 21.90, 22.90, 22.90]],
2007 => ['term' => 36, 'rates' => [15.90, 18.90, 21.90, 22.90, 22.90]],
2008 => ['term' => 36, 'rates' => [14.90, 17.90, 21.90, 22.90, 22.90]],
2009 => ['term' => 36, 'rates' => [14.90, 17.90, 20.90, 20.90, 20.90]],
2010 => ['term' => 36, 'rates' => [14.90, 17.90, 20.90, 20.90, 20.90]],
2011 => ['term' => 42, 'rates' => [14.90, 16.90, 19.90, 19.90, 20.90]],
2012 => ['term' => 42, 'rates' => [12.90, 15.90, 18.90, 19.90, 19.90]],
2013 => ['term' => 42, 'rates' => [12.90, 15.90, 18.90, 18.90, 18.90]],
2014 => ['term' => 48, 'rates' => [11.90, 14.90, 17.90, 18.90, 18.90]],
]
);
define('CREDIT_SCORE_MINIMUM_THRESHOLDS', [720, 610, 580, 530, 489]);
/** @throws Exception */
function getRateIndexFromCreditScore(int $creditScore): ?int
{
foreach (CREDIT_SCORE_MINIMUM_THRESHOLDS as $rateIndex => $minThreshold) {
if ($creditScore >= $minThreshold) {
return $rateIndex;
}
}
throw new Exception("Sorry, your credit score ($creditScore) does not qualify for financing");
}
foreach (range(2000, 2015) as $year) {
try {
if (!isset(TERM_RATES_BY_YEAR[$year])) {
throw new Exception("Sorry, your vehicle year ($year) does not qualify for financing");
}
$score = rand(300, 850);
$rateIndex = getRateIndexFromCreditScore($score);
$outcome = sprintf(
'Vehicle Year: %d, Credit Score: %d, Term: %d, Rate: %.2f',
$year,
$score,
TERM_RATES_BY_YEAR[$year]['term'],
TERM_RATES_BY_YEAR[$year]['rates'][$rateIndex]
);
} catch (Exception $e) {
$outcome = $e->getMessage();
}
echo "$outcome\n";
}
潜在输出:
Sorry, your vehicle year (2000) does not qualify for financing
Vehicle Year: 2001, Credit Score: 758, Term: 24, Rate: 15.90
Sorry, your credit score (401) does not qualify for financing
Vehicle Year: 2003, Credit Score: 751, Term: 24, Rate: 15.90
Vehicle Year: 2004, Credit Score: 545, Term: 24, Rate: 22.90
Sorry, your credit score (481) does not qualify for financing
Vehicle Year: 2006, Credit Score: 658, Term: 24, Rate: 18.90
Vehicle Year: 2007, Credit Score: 745, Term: 36, Rate: 15.90
Vehicle Year: 2008, Credit Score: 836, Term: 36, Rate: 14.90
Vehicle Year: 2009, Credit Score: 803, Term: 36, Rate: 14.90
Sorry, your credit score (466) does not qualify for financing
Vehicle Year: 2011, Credit Score: 792, Term: 42, Rate: 14.90
Vehicle Year: 2012, Credit Score: 763, Term: 42, Rate: 12.90
Vehicle Year: 2013, Credit Score: 534, Term: 42, Rate: 18.90
Vehicle Year: 2014, Credit Score: 493, Term: 48, Rate: 18.90
Sorry, your vehicle year (2015) does not qualify for financing