动态调用动态语言,第 2 部分: 在运行时寻找、执行和修改脚本
2009-11-19 00:00:00 来源:WEB开发网脚本设置 result 的属性,从而指出贷款人是否符合抵押贷款的要求以及应该采用的利率。脚本可以通过 message 和 productName 属性指出导致贷款人不合格的原因和返回相关的产品名称。
脚本文件
在给出 ScriptMortgageQualifierRunner 的输出之前,我们先看看这个程序运行的 Groovy、JavaScript 和 Ruby 脚本文件。Groovy 脚本中的业务逻辑定义了一种条件相当宽松的抵押产品,同时由于金融风险比较高,因此利率比较高。JavaScript 脚本代表一种政府担保的抵押贷款,这种贷款要求贷款人必须满足最大收入和其他限制。Ruby 脚本定义的抵押产品业务规则要求贷款人有良好的信用记录,这些人要支付足够的首付款,这种抵押贷款的利率比较低。
清单 4 给出 Groovy 脚本,即使您不了解 Groovy,也应该能够看懂这个脚本。
清单 4. Groovy 抵押脚本/*
This Groovy script defines the "Groovy Mortgage" product.
This product is relaxed in its requirements of borrowers.
There is a higher interest rate to make up for the looser standard.
All borrowers will be approved if their credit history is good, they can
make a down payment of at least 5%, and they either earn more than
$2,000/month or have a net worth (assets minus liabilities) of $25,000.
*/
// Our product name.
result.productName = 'Groovy Mortgage'
// Check for the minimum income and net worth
def netWorth = borrower.totalAssets - borrower.totalLiabilities
if (borrower.monthlyIncome < 2000 && netWorth < 25000) {
scriptExit.withMessage "Low monthly income of ${borrower.monthlyIncome}" +
' requires a net worth of at least $25,000.'
}
def downPaymentPercent = loan.downPayment / property.salesPrice * 100
if (downPaymentPercent < 5) {
scriptExit.withMessage 'Down payment of ' +
"${String.format('%1$.2f', downPaymentPercent)}% is insufficient." +
' 5% minimum required.'
}
if (borrower.creditScore < 600) {
scriptExit.withMessage 'Credit score of 600 required.'
}
// Everyone else qualifies. Find interest rate based on down payment percent.
result.qualified = true
result.message = 'Groovy! You qualify.'
switch (downPaymentPercent) {
case 0..5: result.interestRate = 0.08; break
case 6..10: result.interestRate = 0.075; break
case 11..15: result.interestRate = 0.07; break
case 16..20: result.interestRate = 0.065; break
default: result.interestRate = 0.06; break
}
更多精彩
赞助商链接