33
44namespace Data_M2 ;
55
6+ // TASK 2: Create Bank Class
7+ // Purpose: Manage customers and transaction reports.
8+
69public class Bank
710{
8- // Fields
9- private readonly Guid _bankId ;
10- private readonly List < BankCustomer > _customers ;
11-
12- // Properties
13- public Guid BankId => _bankId ;
14- public IReadOnlyList < BankCustomer > Customers => _customers . AsReadOnly ( ) ;
15-
16- // Constructors
17- public Bank ( )
18- {
19- _bankId = Guid . NewGuid ( ) ;
20- _customers = new List < BankCustomer > ( ) ;
21- }
22-
23- // Methods
24- internal IEnumerable < BankCustomer > GetAllCustomers ( )
25- {
26- return new List < BankCustomer > ( _customers ) ;
27- }
28-
29- internal IEnumerable < BankCustomer > GetCustomersByName ( string firstName , string lastName )
30- {
31- List < BankCustomer > matchingCustomers = new List < BankCustomer > ( ) ;
32- foreach ( var customer in _customers )
33- {
34- if ( customer . FirstName . Equals ( firstName , StringComparison . OrdinalIgnoreCase ) &&
35- customer . LastName . Equals ( lastName , StringComparison . OrdinalIgnoreCase ) )
36- {
37- matchingCustomers . Add ( customer ) ;
38- }
39- }
40- return matchingCustomers ;
41- }
11+ // TASK 2: Step 1 - Add Name and List<BankCustomer> properties
12+ public string Name { get ; set ; } // Removed 'required' keyword
13+ public List < BankCustomer > Customers { get ; private set ; }
4214
43- // get customer based on Customer ID
44- internal BankCustomer ? GetCustomerById ( string customerId )
45- {
46- foreach ( var customer in _customers )
47- {
48- if ( customer . CustomerId . Equals ( customerId , StringComparison . OrdinalIgnoreCase ) )
49- {
50- return customer ;
51- }
52- }
53- return null ;
54- }
15+ // TASK 2: Step 2 - Add a Dictionary<string, List<Transaction>> for transaction reports
16+ public Dictionary < string , List < Transaction > > TransactionReports { get ; private set ; }
5517
56- internal int GetNumberOfTransactions ( )
18+ // Constructor to initialize properties
19+ public Bank ( string name )
5720 {
58- int totalTransactions = 0 ;
59- foreach ( BankCustomer customer in _customers )
60- {
61- foreach ( BankAccount account in customer . Accounts )
62- {
63- foreach ( Transaction transaction in account . Transactions )
64- {
65- totalTransactions ++ ;
66- }
67- }
68- }
69- return totalTransactions ;
21+ Name = name ; // Initialize the Name property
22+ Customers = new List < BankCustomer > ( ) ;
23+ TransactionReports = new Dictionary < string , List < Transaction > > ( ) ;
7024 }
7125
72- internal double GetTotalMoneyInVault ( )
26+ // TASK 2: Step 3 - Implement AddCustomer method
27+ public void AddCustomer ( BankCustomer customer )
7328 {
74- double totalBankCash = 0 ;
75- foreach ( BankCustomer customer in _customers )
29+ if ( customer != null && ! Customers . Contains ( customer ) )
7630 {
77- foreach ( BankAccount account in customer . Accounts )
78- {
79- totalBankCash += account . Balance ;
80- }
31+ Customers . Add ( customer ) ;
8132 }
82- return totalBankCash ;
8333 }
8434
85- internal double GetDailyDeposits ( DateOnly date )
86- {
87- double totalDailyDeposits = 0 ;
88- foreach ( BankCustomer customer in _customers )
89- {
90- foreach ( BankAccount account in customer . Accounts )
91- {
92- foreach ( Transaction transaction in account . Transactions )
93- {
94- if ( transaction . TransactionDate == date && transaction . TransactionType == "Deposit" )
95- {
96- totalDailyDeposits += transaction . TransactionAmount ;
97- }
98- }
99- }
100- }
101- return totalDailyDeposits ;
102- }
35+ // TASK 10: Add Dictionary for Reports
36+ // Purpose: Manage transaction data for reports in the Bank class.
10337
104- internal double GetDailyWithdrawals ( DateOnly date )
38+ // TASK 10: Step 1 - Add a method to add transactions to the dictionary
39+ public void AddTransactionToReport ( string key , Transaction transaction )
10540 {
106- double totalDailyWithdrawals = 0 ;
107- foreach ( BankCustomer customer in _customers )
41+ if ( ! TransactionReports . ContainsKey ( key ) )
10842 {
109- foreach ( BankAccount account in customer . Accounts )
110- {
111- foreach ( Transaction transaction in account . Transactions )
112- {
113- if ( transaction . TransactionDate == date && transaction . TransactionType == "Withdraw" )
114- {
115- totalDailyWithdrawals += transaction . TransactionAmount ;
116- }
117- }
118- }
43+ TransactionReports [ key ] = new List < Transaction > ( ) ;
11944 }
120- return totalDailyWithdrawals ;
121- }
122-
123- internal void AddCustomer ( BankCustomer customer )
124- {
125- _customers . Add ( customer ) ;
45+ TransactionReports [ key ] . Add ( transaction ) ;
12646 }
12747
128- internal void RemoveCustomer ( BankCustomer customer )
48+ // TASK 10: Step 2 - Add a method to retrieve transactions for a specific key
49+ public List < Transaction > GetTransactionsForKey ( string key )
12950 {
130- _customers . Remove ( customer ) ;
51+ return TransactionReports . ContainsKey ( key ) ? TransactionReports [ key ] : new List < Transaction > ( ) ;
13152 }
13253}
0 commit comments