Recap - Bank Account Manager
Part of the Object Oriented Programming section of Coddy's C# journey — lesson 18 of 70.
Challenge
EasyLet'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 aBankAccountclass in theBankingnamespace. Your account should include:- A
constfieldMinimumBalanceset to100- the minimum balance all accounts must maintain - A
staticfieldInterestRate(decimal) that starts at0.03m- shared across all accounts - A
staticfieldTotalAccounts(int) that tracks how many accounts exist - A
readonlyfieldAccountNumber(string) - set once during creation and never changed - A private backing field
_ownerNamewith a propertyOwnerNamethat trims whitespace when setting - A private backing field
_balancewith a propertyBalancethat prevents the balance from going belowMinimumBalance(if someone tries to set it lower, keep it atMinimumBalance) - 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
- A
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.0Notice 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
1Fundamentals of OOP
External FilesNamespaces & DirectivesIntro to Classes & ObjectsThe 'this' KeywordMethods and ParametersFields vs PropertiesConstructorsObject InitializersRecap - Simple Calculator4Inheritance
Basic Inheritance (:) SyntaxThe 'base' KeywordVirtual & Override KeywordsSealed ClassesThe 'object' Base ClassRecap - Employee Hierarchy7Advanced Features
Operator OverloadingIndexers (this[])ToString() OverrideExtension MethodsRecap - Custom List2Properties & Static Members
Auto-Implemented PropertiesRead/Write-Only PropertiesStatic Fields & MethodsStatic ClassesExpression-Bodied Members5Polymorphism & Interfaces
Compile vs Runtime PolyInterface vs Abstract ClassMultiple InterfacesExplicit InterfacesUpcasting & DowncastingRecap - Shape Calculator3Class Architecture
Instance vs Static Data'readonly' & 'const' KeywordsBacking FieldsRecap - Bank Account Manager