
Problem
Solution
Submissions
Bank Account Implementation
Certification: Intermediate Level
Accuracy: 50%
Submissions: 12
Points: 10
Write a C# program to implement a 'BankAccount' class with deposit, withdraw, and balance features.
Example 1
- Input:
- BankAccount account = new BankAccount("12345", 1000);
- account.Deposit(500);
- account.Withdraw(200);
- Console.WriteLine(account.Balance);
- Output:
- 1300
- Explanation:
- Step 1: Create a new bank account with initial balance 1000.
- Step 2: Deposit 500, increasing the balance to 1500.
- Step 3: Withdraw 200, decreasing the balance to 1300.
- Step 4: Output the final balance of 1300.
Example 2
- Input:
- BankAccount account = new BankAccount("67890");
- account.Deposit(300);
- try {
- account.Withdraw(500);
- } catch (InsufficientFundsException ex) {
- Console.WriteLine(ex.Message);
- }
- Console.WriteLine(account.Balance);
- Output:
- Insufficient funds: Available: $300, Requested: $500
- 300
- Explanation:
- Step 1: Create a new bank account with default balance 0.
- Step 2: Deposit 300, making the balance 300.
- Step 3: Attempt to withdraw 500, which fails since balance is insufficient.
- Step 4: An exception is thrown and caught, displaying the error message.
- Step 5: Output the final balance which remains 300.
Constraints
- Account number must be a non-empty string
- Deposit and withdrawal amounts must be positive values
- Time Complexity: O(1) for all operations
- Space Complexity: O(1)
Editorial
My Submissions
All Solutions
| Lang | Status | Date | Code |
|---|---|---|---|
| You do not have any submissions for this problem. | |||
| User | Lang | Status | Date | Code |
|---|---|---|---|---|
| No submissions found. | ||||
Solution Hints
- Create a custom exception class `InsufficientFundsException` that inherits from `Exception`
- Use private fields to store the account state and implement public methods to manipulate it
- Validate all inputs before processing operations
- Implement proper exception handling for edge cases
- Consider using decimal type for currency values to avoid floating-point precision issues