Skip to content

Commit 331e5c6

Browse files
committed
LP4 M4 final test pass updates
1 parent 7ca4f4d commit 331e5c6

3 files changed

Lines changed: 56 additions & 80 deletions

File tree

DownloadableCodeProjects/LP4_manage-app-data/Data_M4/Starter/Program.cs

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -23,20 +23,20 @@ static void Main(string[] args)
2323
bankAccount.AddTransaction(200m, "Deposit");
2424
bankAccount.AddTransaction(-50m, "ATM Withdrawal");
2525

26-
// Task 5: Step 3 - Add an anonymous-type transaction report (Select(...) into new { ... }).
27-
28-
29-
// Task 5: Step 5 - Add an anonymous-type daily totals report (GroupBy(...) + Select(...)).
26+
Console.WriteLine(bankAccount.DisplayAccountInfo());
3027

28+
// Task 2: Step 6 - Consider the DisplayTransactions() method call.
29+
bankAccount.DisplayTransactions();
3130

32-
// Task 5: Step 7 - Add an anonymous-type "top debits" report (Where/OrderBy/Take).
31+
// Task 5: Step 1 - Add an anonymous-type transaction report (Select(...) into new { ... }).
3332

3433

3534
// Task 6: Step 3 - Update Program to call bankAccount.GetDailyTotals() (named record) for API-friendly reporting.
35+
// Task 5: Step 2 - Add an anonymous-type daily totals report (GroupBy(...) + Select(...)).
3636

3737

38-
Console.WriteLine(bankAccount.DisplayAccountInfo());
39-
bankAccount.DisplayTransactions();
38+
// Task 5: Step 3 - Add an anonymous-type "top debits" report (Where/OrderBy/Take).
39+
4040

4141
// Task 4: Step 8 - Add Fee transactions to the account.
4242

Instructions/Labs/l2p2-lp4-m3-exercise-implement-enum-struct-record.md

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -424,5 +424,3 @@ Use the following steps to complete this task:
424424
## Clean up
425425

426426
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. Additionally, building a portfolio of projects can be a great way to demonstrate your skills to potential employers.
427-
428-
If you no longer need the project files, you can delete the folder to free up space on your computer. However, it's recommended to keep a copy of the completed project for future reference or as part of your coding portfolio.

Instructions/Labs/l2p2-lp4-m4-exercise-implement-generic-anonymous-types.md

Lines changed: 49 additions & 71 deletions
Original file line numberDiff line numberDiff line change
@@ -41,8 +41,13 @@ You've developed an initial version of the app that includes the following files
4141
This exercise includes the following tasks:
4242

4343
1. Review the current version of your project.
44+
1. Refactor transactions to a read-only generic API.
45+
1. Add a Bank class that manages accounts using generics.
46+
1. Build a reusable generic Ledger\<TEntry\> with constraints.
47+
1. Create reports using anonymous types (LINQ projections).
48+
1. Decide the boundary - anonymous types vs named result types.
4449

45-
## Review the current version of your project
50+
## Task 1: Review the current version of your project
4651

4752
In this task, you download the existing version of your project and review the code.
4853

@@ -95,11 +100,9 @@ Use the following steps to complete this section of the exercise:
95100
96101
## Task 2: Refactor transactions to a read-only generic API
97102
98-
### What to learn
99-
100103
Returning mutable collections like `List<T>` exposes internal state that callers can modify, breaking encapsulation. Generic read-only interfaces like `IReadOnlyList<T>` provide a safe way to expose collections without allowing mutation.
101104
102-
In this task, you make transactions queryable without exposing mutable internal state.
105+
In this task, you use generic read-only interfaces to make transactions queryable without exposing mutable internal state.
103106
104107
Use the following steps to complete this task:
105108
@@ -136,21 +139,13 @@ Use the following steps to complete this task:
136139
Before:
137140
138141
```csharp
139-
public void AddTransaction(decimal amount, string description)
140-
{
141-
Balance += amount;
142-
Transactions.Add(new Transaction(amount, DateTime.Now, description));
143-
}
142+
Transactions.Add(new Transaction(amount, DateTime.Now, description));
144143
```
145144
146145
After:
147146
148147
```csharp
149-
public void AddTransaction(decimal amount, string description)
150-
{
151-
Balance += amount;
152-
_transactions.Add(new Transaction(amount, DateTime.Now, description));
153-
}
148+
_transactions.Add(new Transaction(amount, DateTime.Now, description));
154149
```
155150
156151
Notice that the method now adds to the private backing list, not the public property.
@@ -162,26 +157,18 @@ Use the following steps to complete this task:
162157
Before (iterate the backing list directly):
163158
164159
```csharp
165-
public void DisplayTransactions()
160+
foreach (Transaction transaction in Transactions)
166161
{
167-
Console.WriteLine("Transactions:");
168-
foreach (Transaction transaction in _transactions)
169-
{
170-
Console.WriteLine(transaction);
171-
}
162+
Console.WriteLine(transaction);
172163
}
173164
```
174165
175166
After (iterate the read-only view):
176167
177168
```csharp
178-
public void DisplayTransactions()
169+
foreach (var transaction in Transactions)
179170
{
180-
Console.WriteLine("Transactions:");
181-
foreach (var transaction in Transactions)
182-
{
183-
Console.WriteLine(transaction);
184-
}
171+
Console.WriteLine(transaction);
185172
}
186173
```
187174
@@ -199,7 +186,9 @@ Use the following steps to complete this task:
199186
200187
1. Open the `Program.cs` file.
201188
202-
1. Locate the `DisplayTransactions()` method call.
189+
1. Locate the code comment that begins with **Task 2: Step 6**.
190+
191+
1. Take a minute to consider the `DisplayTransactions()` method call.
203192
204193
You updated the `DisplayTransactions()` method to read transactions using the read-only API, so no changes are needed here. However, if you wanted to print transactions directly from `Program.cs`, you could replace the method call with something similar to the following code:
205194
@@ -274,9 +263,9 @@ Use the following steps to complete this task:
274263
private readonly Dictionary<BankAccountNumber, BankAccount> _accounts = new();
275264
```
276265
277-
The 'generics' part of this line is the `Dictionary<BankAccountNumber, BankAccount>`:
266+
The 'generics' part of this line is the `Dictionary<BankAccountNumber, BankAccount>`.
278267
279-
`Dictionary<TKey, TValue>` is a generic .NET collection type. It takes two type parameters:
268+
You may recall that `Dictionary<TKey, TValue>` is a generic .NET collection type. It takes two type parameters:
280269
- TKey = BankAccountNumber (the type you look up by)
281270
- TValue = BankAccount (the type you get back)
282271
@@ -329,9 +318,9 @@ Use the following steps to complete this task:
329318
Add this below the code above:
330319
331320
```csharp
332-
var selected = bank.GetAccount(accountNumber);
333-
selected.AddTransaction(200m, "Deposit");
334-
selected.AddTransaction(-50m, "ATM Withdrawal");
321+
var selected = bank.GetAccount(accountNumber3);
322+
selected.AddTransaction(321m, "Deposit");
323+
selected.AddTransaction(-123m, "ATM Withdrawal");
335324
Console.WriteLine(selected.DisplayAccountInfo());
336325
```
337326
@@ -341,15 +330,13 @@ Use the following steps to complete this task:
341330
342331
```plaintext
343332
Welcome to the Bank App!
344-
Account Holder: Tim Shao, Account Number: 000012345678, Type: Checking, Balance: $650.00
333+
Account Holder: Ni Kang, Account Number: 000123456789, Type: Savings, Balance: $1,350.00
345334
Account type description: A standard checking account.
335+
Account Holder: Tim Shao, Account Number: 000012345678, Type: Checking, Balance: $500.00
346336
Account Holder: Tim Shao, Account Number: 000012345678, Type: Checking, Balance: $650.00
347-
Account Holder: Tim Shao, Account Number: 000012345678, Type: Checking, Balance: $800.00
348337
Transactions:
349338
1/12/2026: Deposit - $200.00
350339
1/12/2026: ATM Withdrawal - ($50.00)
351-
1/12/2026: Deposit - $200.00
352-
1/12/2026: ATM Withdrawal - ($50.00)
353340
Are customers equal? True
354341
Original Account Number: 000123456789
355342
```
@@ -403,6 +390,8 @@ Use the following steps to complete this task:
403390
}
404391
```
405392
393+
1. Scroll to the bottom of the file.
394+
406395
1. Locate the code comment that begins with **Task 4: Step 3**.
407396
408397
1. Create a new generic class `Ledger<TEntry>` constrained to `ILedgerEntry`:
@@ -424,15 +413,15 @@ Use the following steps to complete this task:
424413
}
425414
```
426415
427-
1. Take a minute to consider the generic class declaration you just created.
416+
1. Take a minute to consider the class declaration code line that you just created.
428417
429418
```csharp
430419
public sealed class Ledger<TEntry> where TEntry : ILedgerEntry
431420
```
432421
433-
In this code, `TEntry` can be any type that implements `ILedgerEntry`, ensuring type safety while allowing flexibility.
422+
In this generic class declaration, `TEntry` can be any type that implements `ILedgerEntry`, ensuring type safety while allowing flexibility.
434423
435-
1. Locate the code comment that begins with **Task 4: Step 4**.
424+
1. Scroll back up to locate the code comment that begins with **Task 4: Step 4**.
436425
437426
1. Refactor `BankAccount` to replace its transaction list with `Ledger<Transaction>`.
438427
@@ -496,9 +485,7 @@ Use the following steps to complete this task:
496485
497486
1. Open Program.cs, and then locate the code comment that begins with **Task 4: Step 8**.
498487
499-
1. Demonstrate in `Program.cs` that `Ledger<Fee>` works without rewriting ledger logic.
500-
501-
Add this (example) somewhere in `Main`:
488+
1. To demonstrate that `Ledger<Fee>` works without rewriting ledger logic, enter the following code:
502489
503490
```csharp
504491
var feeLedger = new Ledger<Fee>();
@@ -512,15 +499,13 @@ Use the following steps to complete this task:
512499
513500
```plaintext
514501
Welcome to the Bank App!
515-
Account Holder: Tim Shao, Account Number: 000012345678, Type: Checking, Balance: $650.00
502+
Account Holder: Ni Kang, Account Number: 000123456789, Type: Savings, Balance: $1,350.00
516503
Account type description: A standard checking account.
504+
Account Holder: Tim Shao, Account Number: 000012345678, Type: Checking, Balance: $500.00
517505
Account Holder: Tim Shao, Account Number: 000012345678, Type: Checking, Balance: $650.00
518-
Account Holder: Tim Shao, Account Number: 000012345678, Type: Checking, Balance: $800.00
519506
Transactions:
520507
1/12/2026: Deposit - $200.00
521508
1/12/2026: ATM Withdrawal - ($50.00)
522-
1/12/2026: Deposit - $200.00
523-
1/12/2026: ATM Withdrawal - ($50.00)
524509
Fee ledger total: ($2.50)
525510
Are customers equal? True
526511
Original Account Number: 000123456789
@@ -601,26 +586,21 @@ Use the following steps to complete this task:
601586
602587
```plaintext
603588
Welcome to the Bank App!
604-
Account Holder: Tim Shao, Account Number: 000012345678, Type: Checking, Balance: $650.00
589+
Account Holder: Ni Kang, Account Number: 000123456789, Type: Savings, Balance: $1,398.00
605590
Account type description: A standard checking account.
591+
Account Holder: Tim Shao, Account Number: 000012345678, Type: Checking, Balance: $500.00
606592
Account Holder: Tim Shao, Account Number: 000012345678, Type: Checking, Balance: $650.00
607-
Fee ledger total: ($2.50)
593+
Transactions:
594+
1/12/2026: Deposit - $200.00
595+
1/12/2026: ATM Withdrawal - ($50.00)
608596
Transaction report:
609597
1/12/2026 | Credit | $200.00 | Deposit
610598
1/12/2026 | Debit | ($50.00) | ATM Withdrawal
611-
1/12/2026 | Credit | $200.00 | Deposit
612-
1/12/2026 | Debit | ($50.00) | ATM Withdrawal
613599
Daily totals:
614-
1/12/2026: $300.00 (4 tx)
600+
1/12/2026: $150.00 (2 tx)
615601
Top debits:
616602
1/12/2026 | ($50.00) | ATM Withdrawal
617-
1/12/2026 | ($50.00) | ATM Withdrawal
618-
Account Holder: Tim Shao, Account Number: 000012345678, Type: Checking, Balance: $800.00
619-
Transactions:
620-
1/12/2026: Deposit - $200.00
621-
1/12/2026: ATM Withdrawal - ($50.00)
622-
1/12/2026: Deposit - $200.00
623-
1/12/2026: ATM Withdrawal - ($50.00)
603+
Fee ledger total: ($2.50)
624604
Are customers equal? True
625605
Original Account Number: 000123456789
626606
```
@@ -645,7 +625,7 @@ Use the following steps to complete this task:
645625
public record DailyTotal(DateOnly Day, decimal Total, int Count);
646626
```
647627
648-
1. Locate the code comment that begins with **Task 6: Step 1**.
628+
1. Locate the code comment that begins with **Task 6: Step 2**.
649629
650630
1. To create a daily totals method that returns `IEnumerable<DailyTotal>`, enter the following code:
651631
@@ -704,27 +684,25 @@ Use the following steps to complete this task:
704684
705685
```plaintext
706686
Welcome to the Bank App!
707-
Account Holder: Tim Shao, Account Number: 000012345678, Type: Checking, Balance: $650.00
687+
Account Holder: Ni Kang, Account Number: 000123456789, Type: Savings, Balance: $1,398.00
708688
Account type description: A standard checking account.
689+
Account Holder: Tim Shao, Account Number: 000012345678, Type: Checking, Balance: $500.00
709690
Account Holder: Tim Shao, Account Number: 000012345678, Type: Checking, Balance: $650.00
710-
Fee ledger total: ($2.50)
691+
Transactions:
692+
1/12/2026: Deposit - $200.00
693+
1/12/2026: ATM Withdrawal - ($50.00)
711694
Transaction report:
712695
1/12/2026 | Credit | $200.00 | Deposit
713696
1/12/2026 | Debit | ($50.00) | ATM Withdrawal
714-
1/12/2026 | Credit | $200.00 | Deposit
715-
1/12/2026 | Debit | ($50.00) | ATM Withdrawal
716697
Daily totals:
717-
1/12/2026: $300.00 (4 tx)
698+
1/12/2026: $150.00 (2 tx)
718699
Top debits:
719700
1/12/2026 | ($50.00) | ATM Withdrawal
720-
1/12/2026 | ($50.00) | ATM Withdrawal
721-
Account Holder: Tim Shao, Account Number: 000012345678, Type: Checking, Balance: $800.00
722-
Transactions:
723-
1/12/2026: Deposit - $200.00
724-
1/12/2026: ATM Withdrawal - ($50.00)
725-
1/12/2026: Deposit - $200.00
726-
1/12/2026: ATM Withdrawal - ($50.00)
701+
Fee ledger total: ($2.50)
727702
Are customers equal? True
728703
Original Account Number: 000123456789
729704
```
730705
706+
## Clean up
707+
708+
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. Additionally, building a portfolio of projects can be a great way to demonstrate your skills to potential employers.

0 commit comments

Comments
 (0)