Exercise: Secure Bank Account
Problem statement
Financial systems require strict data integrity. The objective is to build a core bank account class for a fintech application. The account must enforce mandatory initial data, protect the balance from unauthorized modifications, and support multiple ways to deposit funds.
Task requirements
Create a
BankAccountclass inside theBankAccount.csfile.Ensure an account number is mandatory during object creation and cannot be changed afterward.
Enforce that the account balance cannot be set to a negative value.
Implement a primary deposit method that accepts a standard decimal amount.
Implement a secondary deposit method that accepts a string representation of an amount (e.g., from a web form) and parses it safely.
Constraints
Use a
classfor the data model, and place it in theFinancenamespace within its own file.Use the
requiredandinitkeywords for the account number property.Use a
privatebacking field for the balance, and include validation logic inside aprivate setaccessor.Use method overloading to handle the two different
Depositparameter types (decimalandstring).
Good luck trying the exercise! If you’re unsure how to proceed, check the “Solution” tab above.
Get hints
Use
public required string AccountNumber { get; init; }to enforce initialization without allowing later changes.For the
Balanceproperty, define aprivate decimal _balance;field and write customgetandprivate setblocks.Method overloading requires creating two methods with the exact same name (
Deposit) but different parameter signatures.Use
decimal.TryParse(amountAsString, out decimal parsedAmount)in the string overload to safely convert the text to a number before depositing.
Exercise: Secure Bank Account
Problem statement
Financial systems require strict data integrity. The objective is to build a core bank account class for a fintech application. The account must enforce mandatory initial data, protect the balance from unauthorized modifications, and support multiple ways to deposit funds.
Task requirements
Create a
BankAccountclass inside theBankAccount.csfile.Ensure an account number is mandatory during object creation and cannot be changed afterward.
Enforce that the account balance cannot be set to a negative value.
Implement a primary deposit method that accepts a standard decimal amount.
Implement a secondary deposit method that accepts a string representation of an amount (e.g., from a web form) and parses it safely.
Constraints
Use a
classfor the data model, and place it in theFinancenamespace within its own file.Use the
requiredandinitkeywords for the account number property.Use a
privatebacking field for the balance, and include validation logic inside aprivate setaccessor.Use method overloading to handle the two different
Depositparameter types (decimalandstring).
Good luck trying the exercise! If you’re unsure how to proceed, check the “Solution” tab above.
Get hints
Use
public required string AccountNumber { get; init; }to enforce initialization without allowing later changes.For the
Balanceproperty, define aprivate decimal _balance;field and write customgetandprivate setblocks.Method overloading requires creating two methods with the exact same name (
Deposit) but different parameter signatures.Use
decimal.TryParse(amountAsString, out decimal parsedAmount)in the string overload to safely convert the text to a number before depositing.