Skip to content

Commit 11498bd

Browse files
authored
Merge pull request #6 from qq1060656096/develop
Develop
2 parents b2bf297 + 692746b commit 11498bd

2 files changed

Lines changed: 93 additions & 0 deletions

File tree

README.md

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,12 +30,34 @@ use Zwei\LoanCalculator\Calculator\EqualTotalPaymentCalculator;
3030
use Zwei\LoanCalculator\Calculator\EqualPrincipalPaymentCalculator;
3131
use Zwei\LoanCalculator\Calculator\MonthlyInterestPaymentCalculator;
3232
use Zwei\LoanCalculator\Calculator\OncePayPrincipalInterestPaymentCalculator;
33+
use \Zwei\LoanCalculator\PaymentCalculatorFactory;
3334

3435
$principal = 50000;// 本金
3536
$yearInterestRate = "0.10";// 年利率10%
3637
$months = 12;// 借款12个月
3738
$time = strtotime("2018-03-20 10:05");// 借款时间
3839
$decimalDigits = 2;// 保留小数点后3位,默认保留2位
40+
41+
// 等额本金计算器
42+
$obj = PaymentCalculatorFactory::getPaymentCalculatorObj(PaymentCalculatorFactory::TYPE_EQUAL_PRINCIPAL, $principal, $yearInterestRate, $month, 0);
43+
$lists = $obj->getPlanLists();
44+
print_r($lists);
45+
46+
// 等额本息计算器
47+
$obj = PaymentCalculatorFactory::getPaymentCalculatorObj(PaymentCalculatorFactory::TYPE_EQUAL_TOTAL_PAYMENT, $principal, $yearInterestRate, $month, 0);
48+
$lists = $obj->getPlanLists();
49+
print_r($lists);
50+
51+
// 每月还息到期还本还款方式计算器
52+
$obj = PaymentCalculatorFactory::getPaymentCalculatorObj(PaymentCalculatorFactory::TYPE_MONTHLY_INTEREST, $principal, $yearInterestRate, $month, 0);
53+
$lists = $obj->getPlanLists();
54+
print_r($lists);
55+
56+
// 一次性还本付息还款方式计算器
57+
$obj = PaymentCalculatorFactory::getPaymentCalculatorObj(PaymentCalculatorFactory::TYPE_ONCE_PAY_PRINCIPAL_INTEREST, $principal, $yearInterestRate, $month, 0);
58+
$lists = $obj->getPlanLists();
59+
print_r($lists);
60+
3961
// 等额本金计算器
4062
$obj = new EqualPrincipalPaymentCalculator($principal, $yearInterestRate, $months, $time, $decimalDigits);
4163
$planLists = $obj->getPlanLists();// 获取还款计划

src/PaymentCalculatorFactory.php

Lines changed: 71 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,71 @@
1+
<?php
2+
namespace Zwei\LoanCalculator;
3+
4+
use Zwei\LoanCalc\Exception\ParamsException;
5+
use Zwei\LoanCalculator\Calculator\EqualTotalPaymentCalculator;
6+
use Zwei\LoanCalculator\Calculator\EqualPrincipalPaymentCalculator;
7+
use Zwei\LoanCalculator\Calculator\MonthlyInterestPaymentCalculator;
8+
use Zwei\LoanCalculator\Calculator\OncePayPrincipalInterestPaymentCalculator;
9+
10+
/**
11+
* 还款计算器工厂类
12+
* Class PaymentCalculatorFactory
13+
* @package Zwei\LoanCalculator
14+
*/
15+
class PaymentCalculatorFactory
16+
{
17+
/**
18+
* 等额本金计算器
19+
*/
20+
const TYPE_EQUAL_PRINCIPAL = 1;
21+
22+
/**
23+
* 等额本息计算器
24+
*/
25+
const TYPE_EQUAL_TOTAL_PAYMENT = 2;
26+
27+
/**
28+
* 每月还息到期还本还款方式计算器
29+
*/
30+
const TYPE_MONTHLY_INTEREST = 3;
31+
32+
/**
33+
* 一次性还本付息还款方式计算器
34+
*/
35+
const TYPE_ONCE_PAY_PRINCIPAL_INTEREST = 4;
36+
37+
/**
38+
* 获取计算器对象
39+
*
40+
* @param integer $type 类型
41+
* @param float $principal 本金
42+
* @param float $yearInterestRate 年利率
43+
* @param int $months 月数
44+
* @param int $time 借款时间
45+
* @param int $decimalDigits 保留几位小数(默认2)
46+
* @return PaymentCalculatorAbstract
47+
* @throws ParamsException
48+
*/
49+
public static function getPaymentCalculatorObj($type, $principal, $yearInterestRate, $months, $time, $decimalDigits = 2)
50+
{
51+
switch ($type) {
52+
case self::TYPE_EQUAL_PRINCIPAL:
53+
// 等额本金计算器
54+
$obj = new EqualPrincipalPaymentCalculator($principal, $yearInterestRate, $months, $time, $decimalDigits);
55+
break;
56+
case self::TYPE_EQUAL_TOTAL_PAYMENT:
57+
$obj = new EqualTotalPaymentCalculator($principal, $yearInterestRate, $months, $time, $decimalDigits);
58+
break;
59+
case self::TYPE_MONTHLY_INTEREST:
60+
$obj = new MonthlyInterestPaymentCalculator($principal, $yearInterestRate, $months, $time, $decimalDigits);
61+
break;
62+
case self::TYPE_ONCE_PAY_PRINCIPAL_INTEREST:
63+
$obj = new OncePayPrincipalInterestPaymentCalculator($principal, $yearInterestRate, $months, $time, $decimalDigits);
64+
break;
65+
default:
66+
throw new ParamsException('参数非法');
67+
break;
68+
}
69+
return $obj;
70+
}
71+
}

0 commit comments

Comments
 (0)