Skip to content

Commit

Permalink
Merge pull request #6 from qq1060656096/develop
Browse files Browse the repository at this point in the history
Develop
  • Loading branch information
qq1060656096 authored Apr 23, 2018
2 parents b2bf297 + 692746b commit 11498bd
Show file tree
Hide file tree
Showing 2 changed files with 93 additions and 0 deletions.
22 changes: 22 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -30,12 +30,34 @@ use Zwei\LoanCalculator\Calculator\EqualTotalPaymentCalculator;
use Zwei\LoanCalculator\Calculator\EqualPrincipalPaymentCalculator;
use Zwei\LoanCalculator\Calculator\MonthlyInterestPaymentCalculator;
use Zwei\LoanCalculator\Calculator\OncePayPrincipalInterestPaymentCalculator;
use \Zwei\LoanCalculator\PaymentCalculatorFactory;

$principal = 50000;// 本金
$yearInterestRate = "0.10";// 年利率10%
$months = 12;// 借款12个月
$time = strtotime("2018-03-20 10:05");// 借款时间
$decimalDigits = 2;// 保留小数点后3位,默认保留2位

// 等额本金计算器
$obj = PaymentCalculatorFactory::getPaymentCalculatorObj(PaymentCalculatorFactory::TYPE_EQUAL_PRINCIPAL, $principal, $yearInterestRate, $month, 0);
$lists = $obj->getPlanLists();
print_r($lists);

// 等额本息计算器
$obj = PaymentCalculatorFactory::getPaymentCalculatorObj(PaymentCalculatorFactory::TYPE_EQUAL_TOTAL_PAYMENT, $principal, $yearInterestRate, $month, 0);
$lists = $obj->getPlanLists();
print_r($lists);

// 每月还息到期还本还款方式计算器
$obj = PaymentCalculatorFactory::getPaymentCalculatorObj(PaymentCalculatorFactory::TYPE_MONTHLY_INTEREST, $principal, $yearInterestRate, $month, 0);
$lists = $obj->getPlanLists();
print_r($lists);

// 一次性还本付息还款方式计算器
$obj = PaymentCalculatorFactory::getPaymentCalculatorObj(PaymentCalculatorFactory::TYPE_ONCE_PAY_PRINCIPAL_INTEREST, $principal, $yearInterestRate, $month, 0);
$lists = $obj->getPlanLists();
print_r($lists);

// 等额本金计算器
$obj = new EqualPrincipalPaymentCalculator($principal, $yearInterestRate, $months, $time, $decimalDigits);
$planLists = $obj->getPlanLists();// 获取还款计划
Expand Down
71 changes: 71 additions & 0 deletions src/PaymentCalculatorFactory.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
<?php
namespace Zwei\LoanCalculator;

use Zwei\LoanCalc\Exception\ParamsException;
use Zwei\LoanCalculator\Calculator\EqualTotalPaymentCalculator;
use Zwei\LoanCalculator\Calculator\EqualPrincipalPaymentCalculator;
use Zwei\LoanCalculator\Calculator\MonthlyInterestPaymentCalculator;
use Zwei\LoanCalculator\Calculator\OncePayPrincipalInterestPaymentCalculator;

/**
* 还款计算器工厂类
* Class PaymentCalculatorFactory
* @package Zwei\LoanCalculator
*/
class PaymentCalculatorFactory
{
/**
* 等额本金计算器
*/
const TYPE_EQUAL_PRINCIPAL = 1;

/**
* 等额本息计算器
*/
const TYPE_EQUAL_TOTAL_PAYMENT = 2;

/**
* 每月还息到期还本还款方式计算器
*/
const TYPE_MONTHLY_INTEREST = 3;

/**
* 一次性还本付息还款方式计算器
*/
const TYPE_ONCE_PAY_PRINCIPAL_INTEREST = 4;

/**
* 获取计算器对象
*
* @param integer $type 类型
* @param float $principal 本金
* @param float $yearInterestRate 年利率
* @param int $months 月数
* @param int $time 借款时间
* @param int $decimalDigits 保留几位小数(默认2)
* @return PaymentCalculatorAbstract
* @throws ParamsException
*/
public static function getPaymentCalculatorObj($type, $principal, $yearInterestRate, $months, $time, $decimalDigits = 2)
{
switch ($type) {
case self::TYPE_EQUAL_PRINCIPAL:
// 等额本金计算器
$obj = new EqualPrincipalPaymentCalculator($principal, $yearInterestRate, $months, $time, $decimalDigits);
break;
case self::TYPE_EQUAL_TOTAL_PAYMENT:
$obj = new EqualTotalPaymentCalculator($principal, $yearInterestRate, $months, $time, $decimalDigits);
break;
case self::TYPE_MONTHLY_INTEREST:
$obj = new MonthlyInterestPaymentCalculator($principal, $yearInterestRate, $months, $time, $decimalDigits);
break;
case self::TYPE_ONCE_PAY_PRINCIPAL_INTEREST:
$obj = new OncePayPrincipalInterestPaymentCalculator($principal, $yearInterestRate, $months, $time, $decimalDigits);
break;
default:
throw new ParamsException('参数非法');
break;
}
return $obj;
}
}

0 comments on commit 11498bd

Please sign in to comment.