|
| 1 | +// Package challenge7 contains the solution for Challenge 7: Bank Account with Error Handling. |
| 2 | +package challenge7 |
| 3 | + |
| 4 | +import ( |
| 5 | + "fmt" |
| 6 | + "sync" |
| 7 | + // Add any other necessary imports |
| 8 | +) |
| 9 | + |
| 10 | +// BankAccount represents a bank account with balance management and minimum balance requirements. |
| 11 | +type BankAccount struct { |
| 12 | + ID string |
| 13 | + Owner string |
| 14 | + Balance float64 |
| 15 | + MinBalance float64 |
| 16 | + mu sync.Mutex // For thread safety |
| 17 | +} |
| 18 | + |
| 19 | +// Constants for account operations |
| 20 | +const ( |
| 21 | + MaxTransactionAmount = 10000.0 // Example limit for deposits/withdrawals |
| 22 | +) |
| 23 | + |
| 24 | +// Custom error types |
| 25 | + |
| 26 | +// AccountError is a general error type for bank account operations. |
| 27 | +type AccountError struct { |
| 28 | + StatusCode int |
| 29 | + Message string |
| 30 | +} |
| 31 | + |
| 32 | +func (e *AccountError) Error() string { |
| 33 | + // Implement error message |
| 34 | + return fmt.Sprintf("Account Error, code=%d, message=%s", e.StatusCode, e.Message) |
| 35 | +} |
| 36 | + |
| 37 | +// InsufficientFundsError occurs when a withdrawal or transfer would bring the balance below minimum. |
| 38 | +type InsufficientFundsError struct { |
| 39 | + // Implement this error type |
| 40 | +} |
| 41 | + |
| 42 | +func (e *InsufficientFundsError) Error() string { |
| 43 | + // Implement error message |
| 44 | + return "" |
| 45 | +} |
| 46 | + |
| 47 | +// NegativeAmountError occurs when an amount for deposit, withdrawal, or transfer is negative. |
| 48 | +type NegativeAmountError struct { |
| 49 | + // Implement this error type |
| 50 | +} |
| 51 | + |
| 52 | +func (e *NegativeAmountError) Error() string { |
| 53 | + // Implement error message |
| 54 | + return "" |
| 55 | +} |
| 56 | + |
| 57 | +// ExceedsLimitError occurs when a deposit or withdrawal amount exceeds the defined limit. |
| 58 | +type ExceedsLimitError struct { |
| 59 | + // Implement this error type |
| 60 | +} |
| 61 | + |
| 62 | +func (e *ExceedsLimitError) Error() string { |
| 63 | + // Implement error message |
| 64 | + return "" |
| 65 | +} |
| 66 | + |
| 67 | +// NewBankAccount creates a new bank account with the given parameters. |
| 68 | +// It returns an error if any of the parameters are invalid. |
| 69 | +func NewBankAccount(id, owner string, initialBalance, minBalance float64) (*BankAccount, error) { |
| 70 | + if id == "" || owner == "" { |
| 71 | + return nil, &AccountError{} |
| 72 | + } |
| 73 | + if minBalance < 0 || initialBalance < 0 { |
| 74 | + return nil, &NegativeAmountError{} |
| 75 | + } |
| 76 | + if initialBalance < minBalance { |
| 77 | + return nil, &InsufficientFundsError{} |
| 78 | + } |
| 79 | + return &BankAccount{ID: id, Owner: owner, Balance: initialBalance, MinBalance: minBalance, mu: sync.Mutex{}}, nil |
| 80 | +} |
| 81 | + |
| 82 | +// Deposit adds the specified amount to the account balance. |
| 83 | +// It returns an error if the amount is invalid or exceeds the transaction limit. |
| 84 | +func (a *BankAccount) Deposit(amount float64) error { |
| 85 | + // Implement deposit functionality with proper error handling |
| 86 | + if amount < 0 { |
| 87 | + return &NegativeAmountError{} |
| 88 | + } |
| 89 | + if amount > MaxTransactionAmount { |
| 90 | + return &ExceedsLimitError{} |
| 91 | + } |
| 92 | + a.mu.Lock() |
| 93 | + a.Balance += amount |
| 94 | + a.mu.Unlock() |
| 95 | + |
| 96 | + return nil |
| 97 | +} |
| 98 | + |
| 99 | +// Withdraw removes the specified amount from the account balance. |
| 100 | +// It returns an error if the amount is invalid, exceeds the transaction limit, |
| 101 | +// or would bring the balance below the minimum required balance. |
| 102 | +func (a *BankAccount) Withdraw(amount float64) error { |
| 103 | + // Implement withdrawal functionality with proper error handling |
| 104 | + if amount < 0 { |
| 105 | + return &NegativeAmountError{} |
| 106 | + } |
| 107 | + if amount > MaxTransactionAmount { |
| 108 | + return &ExceedsLimitError{} |
| 109 | + } |
| 110 | + if a.Balance-amount < a.MinBalance { |
| 111 | + return &InsufficientFundsError{} |
| 112 | + } |
| 113 | + a.mu.Lock() |
| 114 | + a.Balance -= amount |
| 115 | + a.mu.Unlock() |
| 116 | + return nil |
| 117 | +} |
| 118 | + |
| 119 | +// Transfer moves the specified amount from this account to the target account. |
| 120 | +// It returns an error if the amount is invalid, exceeds the transaction limit, |
| 121 | +// or would bring the balance below the minimum required balance. |
| 122 | +func (a *BankAccount) Transfer(amount float64, target *BankAccount) error { |
| 123 | + // Implement transfer functionality with proper error handling |
| 124 | + if amount < 0 { |
| 125 | + return &NegativeAmountError{} |
| 126 | + } |
| 127 | + if amount > MaxTransactionAmount { |
| 128 | + return &ExceedsLimitError{} |
| 129 | + } |
| 130 | + if a.Balance-amount < a.MinBalance { |
| 131 | + return &InsufficientFundsError{} |
| 132 | + } |
| 133 | + a.mu.Lock() |
| 134 | + a.Balance -= amount |
| 135 | + target.Balance += amount |
| 136 | + a.mu.Unlock() |
| 137 | + return nil |
| 138 | +} |
0 commit comments