Skip to content

Commit f01c7c8

Browse files
committed
update exercise 3 code and instructions
1 parent 68b7595 commit f01c7c8

9 files changed

Lines changed: 52 additions & 32 deletions

File tree

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

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,12 @@ public BankAccount(BankAccount existingAccount)
3535
this.AccountType = existingAccount.AccountType;
3636
}
3737

38+
// Method to deposit money into the account. Only accessible within the assembly
39+
internal void SetBalance(double amount)
40+
{
41+
Balance = amount;
42+
}
43+
3844
// Method to display account information
3945
public string DisplayAccountInfo()
4046
{

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

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ public partial class BankCustomer
77
private static int s_nextCustomerId;
88
private string _firstName = "Tim";
99
private string _lastName = "Shao";
10-
public readonly string customerId;
10+
public readonly string CustomerId;
1111

1212
static BankCustomer()
1313
{
@@ -19,13 +19,13 @@ public BankCustomer(string firstName, string lastName)
1919
{
2020
FirstName = firstName;
2121
LastName = lastName;
22-
this.customerId = (s_nextCustomerId++).ToString("D10");
22+
this.CustomerId = (s_nextCustomerId++).ToString("D10");
2323
}
2424

2525
// Copy constructor with unique customerId
2626
public BankCustomer(BankCustomer existingCustomer)
2727
{
28-
this.customerId = (s_nextCustomerId++).ToString("D10");
28+
this.CustomerId = (s_nextCustomerId++).ToString("D10");
2929
this.FirstName = existingCustomer.FirstName;
3030
this.LastName = existingCustomer.LastName;
3131
}

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

Lines changed: 1 addition & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -20,22 +20,6 @@ public void UpdateName(string firstName, string lastName)
2020
// Method to display customer information
2121
public string DisplayCustomerInfo()
2222
{
23-
return $"Customer ID: {customerId}, Name: {FullName()}";
24-
}
25-
26-
// Override Equals method to compare customers by customerId
27-
public override bool Equals(object obj)
28-
{
29-
if (obj == null || GetType() != obj.GetType())
30-
return false;
31-
32-
BankCustomer other = (BankCustomer)obj;
33-
return customerId == other.customerId;
34-
}
35-
36-
// Override GetHashCode method
37-
public override int GetHashCode()
38-
{
39-
return customerId.GetHashCode();
23+
return $"Customer ID: {CustomerId}, Name: {FullName()}";
4024
}
4125
}

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ public static class BankCustomerExtensions
77
// Extension method to check if the customer ID is valid
88
public static bool IsValidCustomerId(this BankCustomer customer)
99
{
10-
return customer.customerId.Length == 10;
10+
return customer.CustomerId.Length == 10;
1111
}
1212

1313
// Extension method to greet the customer

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

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ public static void Deposit(BankAccount account, double amount)
99
{
1010
if (amount > 0)
1111
{
12-
account.Balance += amount;
12+
account.SetBalance(account.Balance + amount);
1313
}
1414
}
1515

@@ -18,7 +18,7 @@ public static bool Withdraw(BankAccount account, double amount)
1818
{
1919
if (amount > 0 && account.Balance >= amount)
2020
{
21-
account.Balance -= amount;
21+
account.SetBalance(account.Balance - amount);
2222
return true;
2323
}
2424
return false;
@@ -38,6 +38,6 @@ public static bool Transfer(BankAccount sourceAccount, BankAccount targetAccount
3838
// Method to apply interest to the account balance
3939
public static void ApplyInterest(BankAccount account)
4040
{
41-
account.Balance += account.Balance * BankAccount.interestRate;
41+
account.SetBalance(account.Balance + account.Balance * BankAccount.InterestRate);
4242
}
4343
}

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

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ public class BankCustomer
77
private static int s_nextCustomerId;
88
private string _firstName = "Tim";
99
private string _lastName = "Shao";
10-
public readonly string customerId;
10+
public readonly string CustomerId;
1111

1212
static BankCustomer()
1313
{
@@ -19,7 +19,7 @@ public BankCustomer(string firstName, string lastName)
1919
{
2020
FirstName = firstName;
2121
LastName = lastName;
22-
this.customerId = (s_nextCustomerId++).ToString("D10");
22+
this.CustomerId = (s_nextCustomerId++).ToString("D10");
2323
}
2424

2525
public string FirstName
@@ -50,7 +50,7 @@ public void UpdateName(string firstName, string lastName)
5050
// Method to display customer information
5151
public string DisplayCustomerInfo()
5252
{
53-
return $"Customer ID: {customerId}, Name: {ReturnFullName()}";
53+
return $"Customer ID: {CustomerId}, Name: {ReturnFullName()}";
5454
}
5555

5656
}

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ public static class BankCustomerExtensions
77
// Extension method to check if the customer ID is valid
88
public static bool IsValidCustomerId(this BankCustomer customer)
99
{
10-
return customer.customerId.Length == 10;
10+
return customer.CustomerId.Length == 10;
1111
}
1212

1313
// Extension method to greet the customer

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

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -24,9 +24,9 @@
2424
BankAccount account2 = new BankAccount(customer2.CustomerId, 1500, "Checking");
2525
BankAccount account3 = new BankAccount(customer3.CustomerId, 2500, "Checking");
2626

27-
Console.WriteLine($"Account 1: Account # {account1.AccountNumber}, type {account1.AccountType}, balance {account1.Balance}, rate {BankAccount.interestRate}, customer ID {account1.CustomerId}");
28-
Console.WriteLine($"Account 2: Account # {account2.AccountNumber}, type {account2.AccountType}, balance {account2.Balance}, rate {BankAccount.interestRate}, customer ID {account2.CustomerId}");
29-
Console.WriteLine($"Account 3: Account # {account3.AccountNumber}, type {account3.AccountType}, balance {account3.Balance}, rate {BankAccount.interestRate}, customer ID {account3.CustomerId}");
27+
Console.WriteLine($"Account 1: Account # {account1.AccountNumber}, type {account1.AccountType}, balance {account1.Balance}, rate {BankAccount.InterestRate}, customer ID {account1.CustomerId}");
28+
Console.WriteLine($"Account 2: Account # {account2.AccountNumber}, type {account2.AccountType}, balance {account2.Balance}, rate {BankAccount.InterestRate}, customer ID {account2.CustomerId}");
29+
Console.WriteLine($"Account 3: Account # {account3.AccountNumber}, type {account3.AccountType}, balance {account3.Balance}, rate {BankAccount.InterestRate}, customer ID {account3.CustomerId}");
3030

3131
// Step 3: Demonstrate the use of BankCustomer properties
3232
Console.WriteLine("\nUpdating BankCustomer 1's name...");

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

Lines changed: 31 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -465,7 +465,37 @@ Use the following steps to complete this section of the exercise:
465465
466466
Notice that the `Deposit` method now accepts a `BankAccount` parameter named `account`, and that it uses the `account` object to access the `Balance` property. The `account.Balance` property is updated using `amount` parameter.
467467
468-
1. Repeat this method update process to correct the issues in the remaining `Transactions` class methods.
468+
1. Notice that `account.Balance` can't be modified from the static method because the setter is private.
469+
470+
To update the `Balance` property, you need to add an `internal` method to the `BankAccount` class that enabled classes within the assembly to modify the `Balance` property. This keeps the `Balance` property encapsulated within the `BankAccount` class while allowing the `Transactions` class to update the `Balance` property.
471+
472+
1. Add the following `SetBalance` method to the `BankAccount` class:
473+
474+
```csharp
475+
476+
internal void SetBalance(double balance)
477+
{
478+
Balance = balance;
479+
}
480+
481+
```
482+
483+
1. To use the `SetBalance` method in the `Transactions` class, update the `Deposit` method using the following code:
484+
485+
```csharp
486+
487+
// Method to deposit money into the account
488+
public static void Deposit(BankAccount account, double amount)
489+
{
490+
if (amount > 0)
491+
{
492+
account.SetBalance(account.Balance + amount);
493+
}
494+
}
495+
496+
```
497+
498+
1. Repeat the process used to update `Deposit` to correct the issues in the remaining `Transactions` class methods.
469499
470500
Ensure that each of the Transaction methods accepts a `BankAccount` parameter and uses the `BankAccount` instance to access the required properties and fields.
471501

0 commit comments

Comments
 (0)