-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlendinvest.go
73 lines (61 loc) · 1.93 KB
/
lendinvest.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
package lendinvest
import (
"fmt"
"time"
)
// Lendinvest struct containing information about loans in which
// investors can invest and information about future paychecks - to know
// when, to whom and how much cash should Inv be paid.
//
// Important: paychecks are generated automatically (by Lendinvest.MakeInvestment method)
type Lendinvest struct {
loans []loan
paychecks []*paycheck
}
// Cash - simple type for representing money/cash
type Cash float64
func (c Cash) String() string {
return fmt.Sprintf("%.2f", c)
}
// Investor - anyone who implements this interface can invest in Lendinvest
type Investor interface {
LendMoney(money Cash) (Cash, error)
TakeMoney(money Cash)
}
// InvestmentRequest is the DTO struct containing neccessary data to make investment request,
// so we know: who invests, how much, the target, investment start date and investment end date
type InvestmentRequest struct {
Inv Investor
MoneyToInvest Cash
LoanID int
Tranche TrancheID
StartDate time.Time
EndDate time.Time
}
// MakeInvestment - method to make investment according to given investment request
// If something goes wrong, it returns false and error message
func (li *Lendinvest) MakeInvestment(i InvestmentRequest) (ok bool, err error) {
loan := li.loans[i.LoanID]
investment, err := loan.makeInvestment(i)
if nil != err {
return
}
for i := range investment.paychecks {
li.paychecks = append(li.paychecks, &investment.paychecks[i])
}
return
}
// PayPaychecks - pay to assigned investors paychecks for given date
func (li *Lendinvest) PayPaychecks(date time.Time) {
for i := range li.paychecks {
if !li.paychecks[i].dateOfPayment.Equal(date) || true == li.paychecks[i].paid {
continue
}
li.paychecks[i].investor.TakeMoney(li.paychecks[i].moneyToPay)
li.paychecks[i].paid = true
}
}
// AddLoan add loan to lendinvest
func (li *Lendinvest) AddLoan(l loan) {
li.loans = append(li.loans, l)
}