Skip to content

Commit 9feb6e3

Browse files
committed
updated instructions and code to secure Deposit property
1 parent 6b50cf3 commit 9feb6e3

File tree

7 files changed

+39
-18
lines changed

7 files changed

+39
-18
lines changed

DownloadableCodeProjects/LP1_classes-properties-methods/Classes_M2/Solution/BankAccount.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ public class BankAccount
88
public static double interestRate;
99
public int AccountNumber { get; }
1010
public string CustomerId { get; }
11-
public double Balance { get; set; } = 0;
11+
public double Balance { get; private set; } = 0;
1212
public string AccountType { get; set; } = "Checking";
1313

1414
static BankAccount()

DownloadableCodeProjects/LP1_classes-properties-methods/Classes_M3/Solution/BankAccount.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ public class BankAccount
88
public static double interestRate;
99
public int AccountNumber { get; }
1010
public string CustomerId { get; }
11-
public double Balance { get; set; } = 0;
11+
public double Balance { get; private set; } = 0;
1212
public string AccountType { get; set; } = "Checking";
1313

1414
static BankAccount()

DownloadableCodeProjects/LP1_classes-properties-methods/Classes_M3/Solution/BankCustomer.cs

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,9 +5,8 @@ namespace Classes_M3;
55
public partial class BankCustomer
66
{
77
private static int nextCustomerId;
8-
private static readonly Random random = new Random(); // Shared Random instance
9-
private string fName = "John";
10-
private string lName = "Doe";
8+
private string fName = "Tim";
9+
private string lName = "Shao";
1110
public readonly string customerId;
1211

1312
static BankCustomer()

DownloadableCodeProjects/LP1_classes-properties-methods/Classes_M3/Solution/Program.cs

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -2,16 +2,16 @@
22

33
// Step 1: Create BankCustomer objects
44
Console.WriteLine("Creating BankCustomer objects...");
5-
string firstName = "John";
6-
string lastName = "Doe";
5+
string firstName = "Tim";
6+
string lastName = "Shao";
77

88
BankCustomer customer1 = new BankCustomer(firstName, lastName);
99

10-
firstName = "Jane";
10+
firstName = "Lisa";
1111
BankCustomer customer2 = new BankCustomer(firstName, lastName);
1212

13-
firstName = "Leonardo";
14-
lastName = "Rossi";
13+
firstName = "Sandy";
14+
lastName = "Zoeng";
1515
BankCustomer customer3 = new BankCustomer(firstName, lastName);
1616

1717
Console.WriteLine($"BankCustomer 1: {customer1.FirstName} {customer1.LastName} {customer1.customerId}");
@@ -30,8 +30,8 @@
3030

3131
// Step 3: Demonstrate the use of BankCustomer properties
3232
Console.WriteLine("\nUpdating BankCustomer 1's name...");
33-
customer1.FirstName = "Johnny";
34-
customer1.LastName = "Doe-Smith";
33+
customer1.FirstName = "Thomas";
34+
customer1.LastName = "Margand";
3535
Console.WriteLine($"Updated BankCustomer 1: {customer1.FirstName} {customer1.LastName} {customer1.customerId}");
3636

3737
// Step 4: Demonstrate the use of BankAccount methods
@@ -74,7 +74,7 @@
7474
Console.WriteLine("\nDemonstrating object initializers and copy constructors...");
7575

7676
// Using object initializer
77-
BankCustomer customer4 = new BankCustomer("Alice", "Smith") { FirstName = "Alicia", LastName = "Smith-Jones" };
77+
BankCustomer customer4 = new BankCustomer("Mikaela", "Lee") { FirstName = "Mikaela", LastName = "Lee" };
7878
Console.WriteLine($"BankCustomer 4: {customer4.FirstName} {customer4.LastName} {customer4.customerId}");
7979

8080
// Using copy constructor

DownloadableCodeProjects/LP1_classes-properties-methods/Classes_M3/Starter/BankAccount.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ public class BankAccount
88
public static double interestRate;
99
public int AccountNumber { get; }
1010
public string CustomerId { get; }
11-
public double Balance { get; set; } = 0;
11+
public double Balance { get; private set; } = 0;
1212
public string AccountType { get; set; } = "Checking";
1313

1414
static BankAccount()

Instructions/Labs/l2p2-lp1-m2-exercise-update-class-properties-methods.md

Lines changed: 25 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -734,7 +734,7 @@ Use the following steps to complete this section of the exercise:
734734

735735
1. Open the BankAccount.cs file.
736736

737-
The `BankAccount` class has a `Balance` property and `interestRate` field. These two class members can be used to create methods to deposit and withdraw money from the account, transfer money to another account, display account information, and apply interest to the account balance.
737+
The `BankAccount` class has a `Balance` property and `interestRate` field. These two class members can be used to create methods to deposit and withdraw money from the account, transfer money to another account, display account information, and apply interest to the account balance. Once the deposit and withdrawal methods are implemented, the `Balance` property can be converted from an auto-implemented property to a property with a private backing field.
738738

739739
1. Create a blank code line below the last constructor.
740740

@@ -791,7 +791,7 @@ Use the following steps to complete this section of the exercise:
791791

792792
```
793793

794-
The `Transfer` method takes a `BankAccount` object and an amount as parameters. It withdraws the amount from the current account and deposits it into the target account. The method returns `true` if the transfer is successful and `false` otherwise.
794+
The `Transfer` method takes a `BankAccount` object and an `amount` variable as parameters. It uses the `Withdraw` method to withdraw the `amount` value from the current account and the `Deposit` method to deposit it into the `targetAccount` account. The method returns `true` if the transfer is successful and `false` otherwise.
795795

796796
1. To create a method that applies interest to the account balance, add the following code:
797797

@@ -807,6 +807,28 @@ Use the following steps to complete this section of the exercise:
807807

808808
The `ApplyInterest` method calculates the interest on the account balance using the `interestRate` field and adds the interest to the `Balance` property. At this point, the `interestRate` field is a static field that's shared among all instances of the `BankAccount` class. Since interest rate is initialized to 0, the `ApplyInterest` method doesn't actually apply any interest. You can update the `interestRate` field to a non-zero value in the static constructor to see the effect of the `ApplyInterest` method.
809809

810+
1. Take a minute to consider the current implementation of the `Balance` property.
811+
812+
```csharp
813+
814+
public double Balance { get; set; } = 0;
815+
816+
```
817+
818+
The `Balance` property currently uses auto-implemented property syntax, which defines the property without explicitly declaring a backing field. The `{ get; set; }` syntax automatically creates a private backing field for the value, which is initialized to `0`. Since `Balance` is declared `public`, the value of the `Balance` property can be modified directly from outside the class. Public access to the property setter allows the balance to be updated directly without going through the `Deposit`, `Withdraw`, and `Transfer` methods. This can lead to inconsistent account balances and make it difficult to track changes to the balance.
819+
820+
You can convert the `Balance` property to a read-only property with a private backing field to prevent direct modification of the balance value from outside the class. This ensures that the balance can only be updated through the `Deposit`, `Withdraw`, and `Transfer` methods.
821+
822+
1. To convert the `Balance` property to a read-only property with a private backing field, replace the `Balance` property definition with the following code:
823+
824+
```csharp
825+
826+
public double Balance { get; private set; } = 0;
827+
828+
```
829+
830+
The `{ get; private set; }` syntax indicates that the `Balance` property has a private setter, meaning the value of the property can only be set from within the `BankAccount` class. The `Balance` property can still be read from outside the class, but it can only be updated through the `Deposit`, `Withdraw`, and `Transfer` methods.
831+
810832
1. To create a method that displays account information, add the following code:
811833

812834
```csharp
@@ -891,7 +913,7 @@ Use the following steps to complete this section of the exercise:
891913
public static double interestRate;
892914
public int AccountNumber { get; }
893915
public string CustomerId { get; }
894-
public double Balance { get; set; } = 0;
916+
public double Balance { get; private set; } = 0;
895917
public string AccountType { get; set; } = "Checking";
896918

897919
static BankAccount()

Instructions/Labs/l2p2-lp1-m3-exercise-implement-classes-csharp-apps.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -739,7 +739,7 @@ In this task, you update a constructor in the BankAccount class using optional p
739739
public static double interestRate;
740740
public int AccountNumber { get; }
741741
public string CustomerId { get; }
742-
public double Balance { get; set; } = 0;
742+
public double Balance { get; private set; } = 0;
743743
public string AccountType { get; set; } = "Checking";
744744
745745
static BankAccount()

0 commit comments

Comments
 (0)