|
| 1 | +// Package challenge7 contains the solution for Challenge 7: Bank Account with Error Handling. |
| 2 | +package challenge7 |
| 3 | + |
| 4 | +import ( |
| 5 | + "sync" |
| 6 | + // Add any other necessary imports |
| 7 | +) |
| 8 | + |
| 9 | +// BankAccount represents a bank account with balance management and minimum balance requirements. |
| 10 | +type BankAccount struct { |
| 11 | + ID string |
| 12 | + Owner string |
| 13 | + Balance float64 |
| 14 | + MinBalance float64 |
| 15 | + mu sync.Mutex // For thread safety |
| 16 | +} |
| 17 | + |
| 18 | +//var allAccounts []BankAccount |
| 19 | + |
| 20 | +// Constants for account operations |
| 21 | +const ( |
| 22 | + MaxTransactionAmount = 10000.0 // Example limit for deposits/withdrawals |
| 23 | +) |
| 24 | + |
| 25 | +// Custom error types |
| 26 | + |
| 27 | +// AccountError is a general error type for bank account operations. |
| 28 | +type AccountError struct { |
| 29 | + // Implement this error type |
| 30 | +} |
| 31 | + |
| 32 | +func (e *AccountError) Error() string { |
| 33 | + // Implement error message |
| 34 | + return "General Account Error!" |
| 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 "Insufficient Funds!" |
| 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 "Negative Amount!" |
| 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 "Exceeds Limit!" |
| 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 len(id) == 0 { |
| 71 | + return nil, &AccountError{} |
| 72 | + } |
| 73 | + //if account, _ := getAccountById(id); account != nil { |
| 74 | + // return nil, &AccountError{} |
| 75 | + //} |
| 76 | + if len(owner) == 0 { |
| 77 | + return nil, &AccountError{} |
| 78 | + } |
| 79 | + if minBalance < 0 { |
| 80 | + return nil, &NegativeAmountError{} |
| 81 | + } |
| 82 | + balance := initialBalance |
| 83 | + if err := checkBalance(balance, minBalance); err != nil { |
| 84 | + return nil, err |
| 85 | + } |
| 86 | + BankAccount := &BankAccount{ |
| 87 | + ID: id, |
| 88 | + Owner: owner, |
| 89 | + Balance: balance, |
| 90 | + MinBalance: minBalance, |
| 91 | + } |
| 92 | + return BankAccount, nil |
| 93 | +} |
| 94 | + |
| 95 | +//func getAccountById(id string) (*BankAccount, error) { |
| 96 | +// for account := range allAccounts { |
| 97 | +// if allAccounts[account].ID == id { |
| 98 | +// return &allAccounts[account], nil |
| 99 | +// } |
| 100 | +// } |
| 101 | +// return nil, &AccountError{} |
| 102 | +//} |
| 103 | + |
| 104 | +// Deposit adds the specified amount to the account balance. |
| 105 | +// It returns an error if the amount is invalid or exceeds the transaction limit. |
| 106 | +func (a *BankAccount) Deposit(amount float64) error { |
| 107 | + // Implement deposit functionality with proper error handling// Implement deposit functionality with proper error handling |
| 108 | + if amount > MaxTransactionAmount { |
| 109 | + return &ExceedsLimitError{} |
| 110 | + } |
| 111 | + if amount < 0 { |
| 112 | + return &NegativeAmountError{} |
| 113 | + } |
| 114 | + a.mu.Lock() |
| 115 | + defer a.mu.Unlock() |
| 116 | + newBalance := a.Balance + amount |
| 117 | + if err := checkBalance(newBalance, a.MinBalance); err != nil { |
| 118 | + return err |
| 119 | + } |
| 120 | + a.Balance = newBalance |
| 121 | + return nil |
| 122 | +} |
| 123 | + |
| 124 | +// Withdraw removes the specified amount from the account balance. |
| 125 | +// It returns an error if the amount is invalid, exceeds the transaction limit, |
| 126 | +// or would bring the balance below the minimum required balance. |
| 127 | +func (a *BankAccount) Withdraw(amount float64) error { |
| 128 | + // Implement withdrawal functionality with proper error handling |
| 129 | + if amount > MaxTransactionAmount { |
| 130 | + return &ExceedsLimitError{} |
| 131 | + } |
| 132 | + if amount < 0 { |
| 133 | + return &NegativeAmountError{} |
| 134 | + } |
| 135 | + a.mu.Lock() |
| 136 | + defer a.mu.Unlock() |
| 137 | + newBalance := a.Balance - amount |
| 138 | + if err := checkBalance(newBalance, a.MinBalance); err != nil { |
| 139 | + return err |
| 140 | + } |
| 141 | + if amount > MaxTransactionAmount { |
| 142 | + return &ExceedsLimitError{} |
| 143 | + } |
| 144 | + a.Balance = newBalance |
| 145 | + return nil |
| 146 | +} |
| 147 | + |
| 148 | +// Transfer moves the specified amount from this account to the target account. |
| 149 | +// It returns an error if the amount is invalid, exceeds the transaction limit, |
| 150 | +// or would bring the balance below the minimum required balance. |
| 151 | +func (a *BankAccount) Transfer(amount float64, target *BankAccount) error { |
| 152 | + // Implement transfer functionality with proper error handling |
| 153 | + if amount > MaxTransactionAmount { |
| 154 | + return &ExceedsLimitError{} |
| 155 | + } |
| 156 | + if amount < 0 { |
| 157 | + return &NegativeAmountError{} |
| 158 | + } |
| 159 | + if a.ID <= target.ID { |
| 160 | + a.mu.Lock() |
| 161 | + defer a.mu.Unlock() |
| 162 | + target.mu.Lock() |
| 163 | + defer target.mu.Unlock() |
| 164 | + } else { |
| 165 | + target.mu.Lock() |
| 166 | + defer target.mu.Unlock() |
| 167 | + a.mu.Lock() |
| 168 | + defer a.mu.Unlock() |
| 169 | + } |
| 170 | + aBalance := a.Balance - amount |
| 171 | + if err := checkBalance(aBalance, a.MinBalance); err != nil { |
| 172 | + return err |
| 173 | + } |
| 174 | + targetBalance := target.Balance + amount |
| 175 | + if err := checkBalance(targetBalance, target.MinBalance); err != nil { |
| 176 | + return err |
| 177 | + } |
| 178 | + a.Balance = aBalance |
| 179 | + target.Balance = targetBalance |
| 180 | + if amount > MaxTransactionAmount { |
| 181 | + return &ExceedsLimitError{} |
| 182 | + } |
| 183 | + if amount < 0 { |
| 184 | + return &NegativeAmountError{} |
| 185 | + } |
| 186 | + return nil |
| 187 | +} |
| 188 | + |
| 189 | +func checkBalance(balance float64, minBalance float64) error { |
| 190 | + if balance < 0 { |
| 191 | + return &NegativeAmountError{} |
| 192 | + } |
| 193 | + if balance < minBalance { |
| 194 | + return &InsufficientFundsError{} |
| 195 | + } |
| 196 | + if balance > MaxTransactionAmount { |
| 197 | + return &ExceedsLimitError{} |
| 198 | + } |
| 199 | + return nil |
| 200 | +} |
0 commit comments