loan

package
v0.0.1 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Oct 30, 2022 License: MIT Imports: 8 Imported by: 0

Documentation

Index

Constants

View Source
const (
	RateTypeDay               = "日利率"
	RateTypeMonth             = "月利率"
	RateTypeYear              = "年利率"
	PeriodTypeDay             = "天"
	PeriodTypeMonth           = "月"
	PeriodTypeYear            = "年"
	PayTypeOnce               = "到期一次性还清"
	PayTypeMonthEqualInterest = "月还款, 等额本息"
	PayTypeMonthEqualCapital  = "月还款, 等额本金"
)

Variables

View Source
var (
	// Command ...
	Command = &cobra.Command{
		Use:     "loan",
		Aliases: []string{"l"},
		Short:   "计算房贷本金利息",
		Run: func(cmd *cobra.Command, args []string) {
			ans := &Answer{}
			err := survey.Ask(Question, ans)
			if err != nil {
				xcolor.Fail(config.Error, fmt.Sprintf("输入错误: %s", err.Error()))
				return
			}

			items := [][]interface{}{
				{
					"金额", ans.Capital,
				},
				{
					"利率", fmt.Sprintf("%s %.2f%%", ans.RateType, ans.RateValue*100),
				},
				{
					"周期", fmt.Sprintf("%d %s", ans.PeriodValue, ans.PeriodType),
				},
				{
					"方式", ans.PayType,
				},
			}
			xcolor.Success(config.Separator)
			xtable.New(items).Style(xtable.Dashed).Border(true).Render()
			xcolor.Success(config.Separator)

			switch ans.PayType {
			case PayTypeOnce:
				rate := ans.GetRateByPeroid()
				fee := ans.Capital * rate * float64(ans.PeriodValue)
				xcolor.Success(config.Success, fmt.Sprintf("利息: %s", internal.FormatPrice(fee)))
			case PayTypeMonthEqualInterest:
				rate, peroid := ans.GetMonthValue()
				monthList := CalMonthEqualInterest(ans.Capital, rate, peroid)
				xcolor.Success(config.Success, "等额本息")
				xtable.New(monthList).Style(xtable.Dashed).Border(true).Render()
			case PayTypeMonthEqualCapital:
				rate, peroid := ans.GetMonthValue()
				monthList := CalMonthEqualCapital(ans.Capital, rate, peroid)
				xcolor.Success(config.Success, "等额本金")
				xtable.New(monthList).Style(xtable.Dashed).Border(true).Render()
			}

			xcolor.Success(config.Separator)
		},
	}
)
View Source
var (
	Question = []*survey.Question{
		{
			Name: "Capital",
			Prompt: &survey.Input{
				Message: "请输入贷款本金?",
				Default: "880000",
			},
			Validate:  survey.Required,
			Transform: survey.Title,
		},
		{
			Name: "RateType",
			Prompt: &survey.Select{
				Message: "请选择利率类型:",
				Options: []string{
					RateTypeDay,
					RateTypeMonth,
					RateTypeYear,
				},
				Default: RateTypeYear,
			},
		},
		{
			Name: "RateValue",
			Prompt: &survey.Input{
				Message: "请输入利率?",
				Default: "0.0588",
			},
		},
		{
			Name: "PeriodType",
			Prompt: &survey.Select{
				Message: "请选择周期类型:",
				Options: []string{
					PeriodTypeDay,
					PeriodTypeMonth,
					PeriodTypeYear,
				},
				Default: PeriodTypeYear,
			},
		},
		{
			Name: "PeriodValue",
			Prompt: &survey.Input{
				Message: "请输入周期期数?",
				Default: "30",
			},
		},
		{
			Name: "PayType",
			Prompt: &survey.Select{
				Message: "请选择偿还方式:",
				Options: []string{
					PayTypeOnce,
					PayTypeMonthEqualInterest,
					PayTypeMonthEqualCapital,
				},
				Default: PayTypeMonthEqualInterest,
			},
		},
	}
)

Functions

This section is empty.

Types

type Answer

type Answer struct {
	Capital     float64 `survey:"Capital"`
	RateType    string  `survey:"RateType"`
	RateValue   float64 `survey:"RateValue"`
	PeriodType  string  `survey:"PeriodType"`
	PeriodValue int     `survey:"PeriodValue"`
	PayType     string  `survey:"PayType"`
}

Answer ...

func (*Answer) GetMonthValue

func (t *Answer) GetMonthValue() (float64, int)

GetMonthValue 获取按月还款参数

func (*Answer) GetRateByPeroid

func (t *Answer) GetRateByPeroid() float64

GetRateByPeroid 根据还款周期计算利率

type MonthPayment

type MonthPayment struct {
	PeroidNum     int    `table:"期数"`
	MonthTotal    string `table:"月还金额"`
	MonthCapital  string `table:"月还本金"`
	MonthInterest string `table:"月还利息"`
	Total         string `table:"已还金额"`
	TotalCapital  string `table:"已还本金"`
	TotalInterest string `table:"已还利息"`
	RemainCapital string `table:"剩余本金"`
}

MonthPayment 月还款信息

func CalMonthEqualCapital

func CalMonthEqualCapital(
	capital float64,
	rate float64,
	peroid int,
) []*MonthPayment

CalMonthEqualCapital 计算等额本金(总额, 月利率, 周期(月))

func CalMonthEqualInterest

func CalMonthEqualInterest(
	amount float64,
	rate float64,
	peroid int,
) []*MonthPayment

CalMonthEqualInterest 计算等额本息(总额, 月利率, 周期(月))

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL