-
Notifications
You must be signed in to change notification settings - Fork 0
/
facade.go
132 lines (112 loc) · 3 KB
/
facade.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
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
package main
import "fmt"
/*
Provides a simple interface to complex subsystems with many active parts.
*/
var (
creditType = "credit"
debitType = "debit"
)
//facade wraps several components and provides add/deduct operations for clients
type walletFacade struct {
account *account
securityCode *securityCode
wallet *wallet
ledger *ledger
notification *notification
}
func newWalletFacade(accountID string, code int) *walletFacade {
fmt.Printf("Create account\n")
return &walletFacade{
account: &account{
name: accountID,
},
securityCode: &securityCode{
code: code,
},
wallet: &wallet{},
ledger: &ledger{},
notification: ¬ification{},
}
}
func (w *walletFacade) addMoneyToWallet(accountID string, securityCode int, amount int) error {
fmt.Println("Starting add money to wallet")
err := w.account.checkAccount(accountID)
if err != nil {
return err
}
err = w.securityCode.checkCode(securityCode)
if err != nil {
return err
}
w.wallet.creditBalance(amount)
w.ledger.makeEntry(accountID, creditType, amount)
return nil
}
func (w *walletFacade) deductMoneyFromWallet(accountID string, securityCode int, amount int) error {
fmt.Println("Starting debit money from wallet")
err := w.account.checkAccount(accountID)
if err != nil {
return err
}
err = w.securityCode.checkCode(securityCode)
if err != nil {
return err
}
w.wallet.debitBalance(amount)
w.ledger.makeEntry(accountID, debitType, amount)
return nil
}
type account struct {
name string
}
func (a *account) checkAccount(name string) error {
if a.name != name {
return fmt.Errorf(" account name incorrect")
} else {
fmt.Printf("Account Verified\n")
}
return nil
}
type securityCode struct {
code int
}
func (s *securityCode) checkCode(code int) error {
if s.code != code {
return fmt.Errorf(" security code incorrect")
} else {
fmt.Printf("SecurityCode Verified\n")
}
return nil
}
type wallet struct {
balance int
}
func (w *wallet) creditBalance(amount int) {
w.balance += amount
fmt.Printf("Wallet balance added successful for $ %d\n", amount)
}
func (w *wallet) debitBalance(amount int) error {
if w.balance < amount {
return fmt.Errorf("balance is insufficient")
}
w.balance -= amount
fmt.Printf("Wallet balance minused successful for $ %d\n", amount)
return nil
}
type ledger struct{}
func (l *ledger) makeEntry(account, taxType string, amount int) {
fmt.Printf("Making ledger record : account %s with taxType %s for amount $%d\n", account, taxType, amount)
}
type notification struct{}
func (n *notification) sendWalletCreditNotification(account string) {
fmt.Printf("Sending wallet credit notification to account %s\n", account)
}
func (n *notification) sendWalletDebitNotification(account string) {
fmt.Printf("Sending wallet debit notification to account %s\n", account)
}
func RunFacade() {
PersonalServer := newWalletFacade("xiao ming", 123456)
PersonalServer.addMoneyToWallet("xiao ming", 123456, 10)
PersonalServer.deductMoneyFromWallet("xiao ming", 123456, 5)
}