Skip to content

Commit fd97131

Browse files
authored
ban self transfers
Refactor locking mechanism for account transactions.
1 parent e99d5b9 commit fd97131

File tree

1 file changed

+4
-2
lines changed

1 file changed

+4
-2
lines changed

challenge-7/submissions/shansing/solution-template.go

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -156,16 +156,18 @@ func (a *BankAccount) Transfer(amount float64, target *BankAccount) error {
156156
if amount < 0 {
157157
return &NegativeAmountError{}
158158
}
159-
if a.ID <= target.ID {
159+
if a.ID < target.ID {
160160
a.mu.Lock()
161161
defer a.mu.Unlock()
162162
target.mu.Lock()
163163
defer target.mu.Unlock()
164-
} else {
164+
} else if a.ID > target.ID {
165165
target.mu.Lock()
166166
defer target.mu.Unlock()
167167
a.mu.Lock()
168168
defer a.mu.Unlock()
169+
} else {
170+
return &AccountError{}
169171
}
170172
aBalance := a.Balance - amount
171173
if err := checkBalance(aBalance, a.MinBalance); err != nil {

0 commit comments

Comments
 (0)