Skip to content

Commit 2ce7e7c

Browse files
committed
LP6 M1 - exercise review complete and Solution code updated.
1 parent 9383666 commit 2ce7e7c

6 files changed

Lines changed: 79 additions & 11 deletions

File tree

DownloadableCodeProjects/LP6_implement-delegates-events/Delegates/Solution/Interfaces/IBankAccount.cs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ public interface IBankAccount
1717
void ApplyRefund(double refund, DateOnly transactionDate, TimeOnly transactionTime, string description);
1818
bool IssueCashiersCheck(double amount, DateOnly transactionDate, TimeOnly transactionTime, string description);
1919
string DisplayAccountInfo();
20-
void AddTransaction(Transaction transaction);
20+
// void AddTransaction(Transaction transaction, TransactionProcessedCallback? transactionProcessedCallback = null);
21+
void AddTransaction(Transaction transaction, Action<Transaction>? transactionProcessedCallback = null);
2122
List<Transaction> GetAllTransactions();
2223
}

DownloadableCodeProjects/LP6_implement-delegates-events/Delegates/Solution/Models/Bank.cs

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,8 @@
33

44
namespace Delegates;
55

6+
// public delegate int CustomerComparison(BankCustomer x, BankCustomer y);
7+
68
public class Bank
79
{
810
// Fields
@@ -26,6 +28,20 @@ internal IEnumerable<BankCustomer> GetAllCustomers()
2628
return new List<BankCustomer>(_customers);
2729
}
2830

31+
// public IEnumerable<BankCustomer> GetSortedCustomers(CustomerComparison comparison)
32+
// {
33+
// var sortedCustomers = _customers.ToList();
34+
// sortedCustomers.Sort((x, y) => comparison(x, y));
35+
// return sortedCustomers;
36+
// }
37+
38+
public IEnumerable<BankCustomer> GetSortedCustomers(Func<BankCustomer, BankCustomer, int> comparison)
39+
{
40+
var sortedCustomers = _customers.ToList();
41+
sortedCustomers.Sort((x, y) => comparison(x, y));
42+
return sortedCustomers;
43+
}
44+
2945
internal IEnumerable<BankCustomer> GetCustomersByName(string firstName, string lastName)
3046
{
3147
List<BankCustomer> matchingCustomers = new List<BankCustomer>();

DownloadableCodeProjects/LP6_implement-delegates-events/Delegates/Solution/Models/BankAccount.cs

Lines changed: 22 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,9 @@
33

44
namespace Delegates;
55

6+
// public delegate void TransactionProcessedCallback(Transaction transaction);
7+
8+
// Method to add a transaction to the account
69
public class BankAccount : IBankAccount
710
{
811
private static int s_nextAccountNumber;
@@ -75,13 +78,21 @@ public virtual void Deposit(double amount, DateOnly transactionDate, TimeOnly tr
7578
if (description.Contains("-(TRANSFER)"))
7679
{
7780
transactionType = "Transfer";
81+
AddTransaction(new Transaction(transactionDate, transactionTime, priorBalance, amount, AccountNumber, AccountNumber, transactionType, description));
7882
}
79-
else if(description.Contains("-(BANK REFUND)"))
83+
else if (description.Contains("-(BANK REFUND)"))
8084
{
8185
transactionType = "Bank Refund";
86+
AddTransaction(new Transaction(transactionDate, transactionTime, priorBalance, amount, AccountNumber, AccountNumber, transactionType, description), transaction =>
87+
{
88+
Console.WriteLine($"Log the refund to customer {Owner.ReturnFullName()} for account {AccountNumber}.");
89+
});
90+
}
91+
else
92+
{
93+
transactionType = "Deposit";
94+
AddTransaction(new Transaction(transactionDate, transactionTime, priorBalance, amount, AccountNumber, AccountNumber, transactionType, description));
8295
}
83-
84-
AddTransaction(new Transaction(transactionDate, transactionTime, priorBalance, amount, AccountNumber, AccountNumber, transactionType, description));
8596
}
8697
}
8798

@@ -159,9 +170,16 @@ public virtual string DisplayAccountInfo()
159170
}
160171

161172
// Method to add a transaction to the account
162-
public void AddTransaction(Transaction transaction)
173+
// public void AddTransaction(Transaction transaction, TransactionProcessedCallback? callback = null)
174+
// {
175+
// _transactions.Add(transaction);
176+
// callback?.Invoke(transaction); // Invoke the callback if provided
177+
// }
178+
179+
public void AddTransaction(Transaction transaction, Action<Transaction>? callback = null)
163180
{
164181
_transactions.Add(transaction);
182+
callback?.Invoke(transaction); // Invoke the callback if provided
165183
}
166184

167185
// Method to remove a transaction from the account

DownloadableCodeProjects/LP6_implement-delegates-events/Delegates/Solution/Program.cs

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,5 +43,30 @@ static async Task Main()
4343
Console.WriteLine($"Number of customers: {countBankCustomers}");
4444
Console.WriteLine($"Number of accounts: {countBankAccounts}");
4545
Console.WriteLine($"Number of transactions: {countBankTransactions}");
46+
47+
var sortedCustomers = bank.GetSortedCustomers((x, y) =>
48+
{
49+
double balanceX = x.Accounts.Sum(a => a.Balance);
50+
double balanceY = y.Accounts.Sum(a => a.Balance);
51+
return balanceY.CompareTo(balanceX); // Descending order
52+
});
53+
54+
Console.WriteLine("\nCustomers sorted by total balance:");
55+
foreach (var customer in sortedCustomers)
56+
{
57+
Console.WriteLine($"{customer.ReturnFullName()} - Total Balance: {customer.Accounts.Sum(a => a.Balance):C}");
58+
}
59+
60+
var sortedCustomersByName = bank.GetSortedCustomers((x, y) =>
61+
{
62+
int lastNameComparison = x.LastName.CompareTo(y.LastName);
63+
return lastNameComparison != 0 ? lastNameComparison : x.FirstName.CompareTo(y.FirstName);
64+
});
65+
66+
Console.WriteLine("\nCustomers sorted by name:");
67+
foreach (var customer in sortedCustomersByName)
68+
{
69+
Console.WriteLine($"{customer.ReturnFullName()} - Total Balance: {customer.Accounts.Sum(a => a.Balance):C}");
70+
}
4671
}
4772
}
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
placeholder

Instructions/Labs/l2p2-lp6-m1-exercise-implement-delegates.md

Lines changed: 13 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -493,11 +493,11 @@ Use the following steps to complete this section of the exercise:
493493
494494
## Replace custom delegates with strongly typed `Action` and `Func` delegates
495495
496-
In C#, strongly typed delegates like `Action` and `Func` provide a way to define and use delegates without explicitly declaring custom delegate types. These are part of the `System` namespace and are commonly used for scenarios involving callbacks, event handling, or passing methods as parameters.
496+
In C#, strongly typed delegates like `Action` and `Func` provide a way to define and use delegates without explicitly declaring custom delegate types. These strongly typed delegates are part of the `System` namespace and are commonly used for scenarios involving callbacks, event handling, and other scenaraios where passing methods as parameters is required.
497497
498-
In this task, you will replace custom delegate types with strongly typed delegates in the BankAccount and Bank classes.
498+
In this task, you replace custom delegate types with strongly typed delegates in the BankAccount and Bank classes.
499499
500-
1. Open the BankAccount.cs file and then scroll to the top of the file.
500+
1. Open the BankAccount.cs file, and then scroll to the top of the file.
501501
502502
1. Comment out the custom delegate definition:
503503
@@ -507,7 +507,7 @@ In this task, you will replace custom delegate types with strongly typed delegat
507507
508508
```
509509
510-
1. Scroll down to find the `AddTransaction` method in the BankAccount class.
510+
1. Scroll down to the bottom of the file and locate the `AddTransaction` method.
511511
512512
1. To replace the custom delegate with `Action<Transaction>`, update the method using the following code:
513513
@@ -533,13 +533,14 @@ In this task, you will replace custom delegate types with strongly typed delegat
533533
534534
```csharp
535535
536+
// void AddTransaction(Transaction transaction, TransactionProcessedCallback? transactionProcessedCallback = null);
536537
void AddTransaction(Transaction transaction, Action<Transaction>? transactionProcessedCallback = null);
537538
538539
```
539540
540541
1. Open the Bank.cs file.
541542
542-
You can replace the `CustomerComparison` delegate with `Func<BankCustomer, BankCustomer, int>`, which is a strongly typed delegate that takes two `BankCustomer` objects and returns an integer.
543+
You will be replacing the `CustomerComparison` delegate with `Func<BankCustomer, BankCustomer, int>`, which is a strongly typed delegate that takes two `BankCustomer` objects and returns an integer.
543544
544545
1. Scroll to the top of the file and then comment out the custom delegate definition:
545546
@@ -573,4 +574,10 @@ In this task, you will replace custom delegate types with strongly typed delegat
573574
574575
1. Run the app and review the output.
575576
576-
Notice that the app still works as expected. The strongly typed delegates `Action` and `Func` are used instead of custom delegate types.
577+
Notice that the strongly typed delegates (`Action` and `Func`) enable the same flexible behavior as the custom delegates, but with less code complexity and improved readability.
578+
579+
In this exercise, you learned how to declare, instantiate, and invoke delegates for scenarios that require dynamic method invocation. You implemented callback and sorting scenarios using custom delegates, and then replaced the custom delegates with strongly typed `Action` and `Func` delegates that reduce code complexity while providing the same functionality. You also learned how to use lambda expressions to define the delegate logic inline, which can make your code more concise and easier to read.
580+
581+
## Clean up
582+
583+
Now that you've finished the exercise, consider archiving your project files for review at a later time. Having your own projects available for review can be a valuable resource when you're learning to code. Also, building up a portfolio of projects can be a great way to demonstrate your skills to potential employers.

0 commit comments

Comments
 (0)