Exercise: Secure Bank Account

Model a secure bank account using classes, required properties, encapsulation, and method overloading across multiple files.

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 BankAccount class inside the BankAccount.cs file.

  • 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 class for the data model, and place it in the Finance namespace within its own file.

  • Use the required and init keywords for the account number property.

  • Use a private backing field for the balance, and include validation logic inside a private set accessor.

  • Use method overloading to handle the two different Deposit parameter types (decimal and string).

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 Balance property, define a private decimal _balance; field and write custom get and private set blocks.

  • 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

Model a secure bank account using classes, required properties, encapsulation, and method overloading across multiple files.

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 BankAccount class inside the BankAccount.cs file.

  • 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 class for the data model, and place it in the Finance namespace within its own file.

  • Use the required and init keywords for the account number property.

  • Use a private backing field for the balance, and include validation logic inside a private set accessor.

  • Use method overloading to handle the two different Deposit parameter types (decimal and string).

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 Balance property, define a private decimal _balance; field and write custom get and private set blocks.

  • 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.

C# 14.0
namespace Finance;
// TODO: Define a public class named BankAccount
// TODO: Create a required, init-only property for AccountNumber (string)
// TODO: Create a private backing field _balance and a public Balance property
// TODO: In the Balance property's set accessor (make it private), add validation to ignore negative assignments
// TODO: Create an overloaded method Deposit(decimal amount)
// TODO: Create an overloaded method Deposit(string amountAsString)