forked from betty200744/ultimate-go
-
Notifications
You must be signed in to change notification settings - Fork 0
/
facade.go
127 lines (112 loc) · 2.62 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
package facade
import (
"fmt"
"ultimate-go/design-pattern/creational/simple_factory"
)
type walletFacade struct {
account *account
wallet *wallet
password *password
notification *notification
}
func newWalletFacade(name string, code int) *walletFacade {
fmt.Println("Starting create account")
walletFacacde := &walletFacade{
account: newAccount(name),
password: newPassword(code),
wallet: newWallet(),
notification: ¬ification{},
}
fmt.Println("Account created")
return walletFacacde
}
func (w *walletFacade) addMoneyToWallet(accountID int64, password int, amount int) error {
fmt.Println("Starting add money to wallet")
err := w.account.checkAccount(accountID)
if err != nil {
return err
}
err = w.password.checkPassword(password)
if err != nil {
return err
}
w.wallet.creditBalance(amount)
w.notification.sendWalletCreditNotification()
return nil
}
func (w *walletFacade) deductMoneyFromWallet(accountID int64, password int, amount int) error {
fmt.Println("Starting debit money from wallet")
err := w.account.checkAccount(accountID)
if err != nil {
return err
}
err = w.password.checkPassword(password)
if err != nil {
return err
}
err = w.wallet.debitBalance(amount)
if err != nil {
return err
}
w.notification.sendWalletDebitNotification()
return nil
}
type account struct {
id int64
name string
}
func newAccount(name string) *account {
s, _ := simple_factory.NewIdGenerator()
id := s.Generate()
return &account{
id: id,
name: "",
}
}
func (a *account) checkAccount(id int64) error {
if a.id != id {
return fmt.Errorf("Account id is incorrect")
}
fmt.Println("Account Verified")
return nil
}
type password struct {
password int
}
func newPassword(pwd int) *password {
return &password{password: pwd}
}
func (p *password) checkPassword(pwd int) error {
if p.password != pwd {
return fmt.Errorf("password name is incorrect")
}
fmt.Println("password Verified")
return nil
}
type wallet struct {
balance int
}
func newWallet() *wallet {
return &wallet{balance: 0}
}
func (w *wallet) creditBalance(amount int) {
w.balance += amount
fmt.Println("Wallet balance added successfully")
return
}
func (w *wallet) debitBalance(amount int) error {
if w.balance < amount {
return fmt.Errorf("Balance is not sufficient")
}
fmt.Println("Wallet balance is Sufficient")
w.balance = w.balance - amount
return nil
}
type notification struct {
}
func (n *notification) sendWalletCreditNotification() {
fmt.Println("Sending wallet credit notification")
}
func (n *notification) sendWalletDebitNotification() {
fmt.Println("Sending wallet debit notification")
}