-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.go
85 lines (73 loc) · 1.57 KB
/
main.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
package main
import (
"errors"
"fmt"
)
type IBankAccount interface {
GetBalance() int
Deposit(amount int)
Withdraw(amount int) error
}
//----------------------------------------------------------------------
type BTCAccount struct {
balance int
fee int
}
func NewBTCAccount() *BTCAccount {
return &BTCAccount{
balance: 0,
fee: 300,
}
}
func (nsb *BTCAccount) GetBalance() int {
return nsb.balance
}
func (nsb *BTCAccount) Deposit(amount int) {
nsb.balance += amount
}
func (nsb *BTCAccount) Withdraw(amount int) error {
newBalance := nsb.balance - amount - nsb.fee
if newBalance < 0 {
return errors.New("insufficent funds")
}
nsb.balance = newBalance
return nil
}
//-------------------------------------------------------------------------------
type NSBAccount struct {
balance int
}
func NewNSBAccount() *NSBAccount {
return &NSBAccount{
balance: 0,
}
}
func (nsb *NSBAccount) GetBalance() int {
return nsb.balance
}
func (nsb *NSBAccount) Deposit(amount int) {
nsb.balance += amount
}
func (nsb *NSBAccount) Withdraw(amount int) error {
newBalance := nsb.balance - amount
if newBalance < 0 {
return errors.New("insufficent funds")
}
nsb.balance = newBalance
return nil
}
//---------------------------------------------------------------------------
func main() {
myAccounts := []IBankAccount{
NewBTCAccount(),
NewNSBAccount(),
}
for _, account := range myAccounts{
account.Deposit(500)
if err := account.Withdraw(70); err != nil {
fmt.Printf("ERR: %d\n", err)
}
balance := account.GetBalance()
fmt.Printf("balance = %d\n", balance)
}
}