Menu
Coddy logo textTech

Recap - Bank Account Manager

Part of the Object Oriented Programming section of Coddy's C# journey — lesson 18 of 70.

challenge icon

Challenge

Easy

Let's build a complete bank account management system that brings together everything you've learned about class architecture. You'll create a BankAccount class that properly distinguishes between instance data (unique to each account) and static data (shared across all accounts), while using validation to keep accounts in valid states.

You'll create two files to organize your code:

  • BankAccount.cs: Define a BankAccount class in the Banking namespace. Your account should include:
    • A const field MinimumBalance set to 100 - the minimum balance all accounts must maintain
    • A static field InterestRate (decimal) that starts at 0.03m - shared across all accounts
    • A static field TotalAccounts (int) that tracks how many accounts exist
    • A readonly field AccountNumber (string) - set once during creation and never changed
    • A private backing field _ownerName with a property OwnerName that trims whitespace when setting
    • A private backing field _balance with a property Balance that prevents the balance from going below MinimumBalance (if someone tries to set it lower, keep it at MinimumBalance)
    • A constructor that accepts account number, owner name, and initial balance, and increments TotalAccounts
    • A static method SetInterestRate(decimal rate) to update the shared interest rate
    • A method CalculateInterest() that returns the balance multiplied by the interest rate
  • Program.cs: In your main file, create two bank accounts using input values, update the interest rate, and display account information along with the shared bank data.

You will receive seven inputs:

  • First account number
  • First owner name
  • First initial balance
  • Second account number
  • Second owner name
  • Second initial balance
  • New interest rate (as a decimal like 0.05)

Print the output in this format:

--- Bank Summary ---
Interest Rate: {InterestRate}
Minimum Balance: {MinimumBalance}
Total Accounts: {TotalAccounts}

Account: {AccountNumber}
Owner: {OwnerName}
Balance: {Balance}
Interest Earned: {interest}

Account: {AccountNumber}
Owner: {OwnerName}
Balance: {Balance}
Interest Earned: {interest}

For example, if the inputs are ACC001, Alice Smith , 500, ACC002, Bob Jones, 50, and 0.05, the output should be:

--- Bank Summary ---
Interest Rate: 0.05
Minimum Balance: 100
Total Accounts: 2

Account: ACC001
Owner: Alice Smith
Balance: 500
Interest Earned: 25.0

Account: ACC002
Owner: Bob Jones
Balance: 100
Interest Earned: 5.0

Notice how Alice's name gets trimmed of extra spaces, and Bob's balance of 50 gets raised to the minimum of 100. The interest is calculated using the updated rate of 0.05 that was set after creating the accounts.

Try it yourself

using System;
using Banking;

class Program
{
    public static void Main(string[] args)
    {
        // Read inputs for first account
        string accountNumber1 = Console.ReadLine();
        string ownerName1 = Console.ReadLine();
        decimal balance1 = Convert.ToDecimal(Console.ReadLine());
        
        // Read inputs for second account
        string accountNumber2 = Console.ReadLine();
        string ownerName2 = Console.ReadLine();
        decimal balance2 = Convert.ToDecimal(Console.ReadLine());
        
        // Read new interest rate
        decimal newRate = Convert.ToDecimal(Console.ReadLine());
        
        // TODO: Create two BankAccount objects using the input values
        
        // TODO: Update the interest rate using the static method
        
        // TODO: Print the bank summary in the required format
        // --- Bank Summary ---
        // Interest Rate: {InterestRate}
        // Minimum Balance: {MinimumBalance}
        // Total Accounts: {TotalAccounts}
        // (blank line)
        // Account: {AccountNumber}
        // Owner: {OwnerName}
        // Balance: {Balance}
        // Interest Earned: {interest}
        // (blank line)
        // Account: {AccountNumber}
        // Owner: {OwnerName}
        // Balance: {Balance}
        // Interest Earned: {interest}
    }
}

All lessons in Object Oriented Programming