Skip to content

Commit bf502d6

Browse files
committed
LP4 M2 - updated starter code for derived accounts
1 parent fe6c6ae commit bf502d6

5 files changed

Lines changed: 65 additions & 94 deletions

File tree

DownloadableCodeProjects/LP4_manage-app-data/Data_M2/Starter/Models/CertificateOfDepositAccount.cs

Lines changed: 0 additions & 70 deletions
This file was deleted.

DownloadableCodeProjects/LP4_manage-app-data/Data_M2/Starter/Models/CheckingAccount.cs

Lines changed: 33 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -17,32 +17,53 @@ static CheckingAccount()
1717
DefaultInterestRate = 0.0;
1818
}
1919

20-
public CheckingAccount(string customerIdNumber, double balance = 200, double overdraftLimit = 500)
21-
: base(customerIdNumber, balance, "Checking")
20+
public CheckingAccount(BankCustomer owner, string customerIdNumber, double balance = 200, double overdraftLimit = 500)
21+
: base(owner, customerIdNumber, balance, "Checking")
2222
{
2323
OverdraftLimit = overdraftLimit;
24-
2524
}
2625

27-
public override bool Withdraw(double amount)
26+
public override bool Withdraw(double amount, DateOnly transactionDate, TimeOnly transactionTime, string description)
2827
{
29-
if (amount > 0 && Balance + OverdraftLimit >= amount)
28+
// try the base class Withdraw method
29+
bool result = base.Withdraw(amount, transactionDate, transactionTime, description);
30+
31+
if (result == false && !description.Contains("-(TRANSFER)"))
3032
{
31-
Balance -= amount;
33+
// if the base class Withdraw method failed and the transaction isn't an attempted transfer
34+
// - calculate the overdraft fee
35+
// - check the overdraft limit with the fee applied
36+
// - charge an overdraft fee
37+
double overdraftFee = AccountCalculations.CalculateOverdraftFee(Math.Abs(Balance), BankAccount.OverdraftRate, BankAccount.MaxOverdraftFee);
3238

33-
// Check if the account is overdrawn
34-
if (Balance < 0)
39+
if (Balance + OverdraftLimit + overdraftFee >= amount)
3540
{
36-
double overdraftFee = AccountCalculations.CalculateOverdraftFee(Math.Abs(Balance), BankAccount.OverdraftRate, BankAccount.MaxOverdraftFee);
41+
priorBalance = Balance;
42+
Balance -= amount;
43+
string transactionType = "Withdraw";
44+
45+
// Task 4: Step 8a - Create withdrawal transaction
46+
47+
48+
49+
priorBalance = Balance;
3750
Balance -= overdraftFee;
38-
Console.WriteLine($"Overdraft fee of ${overdraftFee} applied.");
51+
transactionType = "Bank Fee";
52+
string overdraftDescription = "Overdraft fee applied";
53+
54+
// Task 5: Step 2 - Create overdraft fee transaction
55+
56+
57+
58+
return true;
3959
}
4060

41-
return true;
4261
}
43-
return false;
62+
63+
return result;
4464
}
4565

66+
4667
public override string DisplayAccountInfo()
4768
{
4869
return base.DisplayAccountInfo() + $", Overdraft Limit: {OverdraftLimit}";

DownloadableCodeProjects/LP4_manage-app-data/Data_M2/Starter/Models/MoneyMarketAccount.cs

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -20,8 +20,8 @@ static MoneyMarketAccount()
2020
DefaultMinimumOpeningBalance = 2000; // Default minimum opening balance
2121
}
2222

23-
public MoneyMarketAccount(string customerIdNumber, double balance = 2000, double minimumBalance = 1000)
24-
: base(customerIdNumber, balance, "Money Market")
23+
public MoneyMarketAccount(BankCustomer owner,string customerIdNumber, double balance = 2000, double minimumBalance = 1000)
24+
: base(owner, customerIdNumber, balance, "Money Market")
2525
{
2626
if (balance < DefaultMinimumOpeningBalance)
2727
{
@@ -32,12 +32,14 @@ public MoneyMarketAccount(string customerIdNumber, double balance = 2000, double
3232
MinimumOpeningBalance = DefaultMinimumOpeningBalance; // Set the minimum opening balance to the default value
3333
}
3434

35-
public override bool Withdraw(double amount)
35+
public override bool Withdraw(double amount, DateOnly transactionDate, TimeOnly transactionTime, string description)
3636
{
3737
if (amount > 0 && Balance - amount >= MinimumBalance)
3838
{
39-
Balance -= amount;
40-
return true;
39+
// Call the base class Withdraw method
40+
bool result = base.Withdraw(amount, transactionDate, transactionTime, description);
41+
42+
return result;
4143
}
4244
return false;
4345
}

DownloadableCodeProjects/LP4_manage-app-data/Data_M2/Starter/Models/SavingsAccount.cs

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -23,8 +23,8 @@ static SavingsAccount()
2323
DefaultInterestRate = 0.02; // 2 percent interest rate
2424
}
2525

26-
public SavingsAccount(string customerIdNumber, double balance = 500, int withdrawalLimit = 6)
27-
: base(customerIdNumber, balance, "Savings")
26+
public SavingsAccount(BankCustomer owner, string customerIdNumber, double balance = 500, int withdrawalLimit = 6)
27+
: base(owner, customerIdNumber, balance, "Savings")
2828
{
2929
if (balance < DefaultMinimumOpeningBalance)
3030
{
@@ -36,13 +36,15 @@ public SavingsAccount(string customerIdNumber, double balance = 500, int withdra
3636
MinimumOpeningBalance = DefaultMinimumOpeningBalance; // Set the minimum opening balance to the default value
3737
}
3838

39-
public override bool Withdraw(double amount)
39+
public override bool Withdraw(double amount, DateOnly transactionDate, TimeOnly transactionTime, string description)
4040
{
4141
if (amount > 0 && Balance >= amount && _withdrawalsThisMonth < WithdrawalLimit)
4242
{
43-
Balance -= amount;
43+
// Call the base class Withdraw method
44+
bool result = base.Withdraw(amount, transactionDate, transactionTime, description);
45+
4446
_withdrawalsThisMonth++;
45-
return true;
47+
return result;
4648
}
4749
return false;
4850
}

Instructions/Labs/l2p2-lp4-m2-exercise-implement-collection-types.md

Lines changed: 18 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,7 @@ This exercise includes the following tasks:
4545
1. Review the prototype banking application.
4646
1. Implement the Bank class.
4747
1. Update the BankCustomer class.
48-
1. Update the BankAccount class.
48+
1. Update the BankAccount and SavingsAccount classes.
4949
1. Update the SimulateDepositWithdrawTransfer class.
5050
1. Manage customer, account, and transaction collections using bank objects.
5151
1. Use a HashSet to ensure unique transactions.
@@ -255,7 +255,7 @@ Use the following steps to complete this task:
255255

256256
The functionality implemented in this task doesn't change the output but will be used in subsequent tasks.
257257

258-
## Task 4: Update the BankAccount class
258+
## Task 4: Update the BankAccount and SavingsAccount classes
259259

260260
BankAccount objects must be able to store a collection of account transactions. This capability is essential for generating reports and for audits.
261261

@@ -386,6 +386,22 @@ Use the following steps to complete this task:
386386

387387
```
388388

389+
1. Open the CheckingAccount.cs file, and then locate the `// Task 4: Step 8` comment.
390+
391+
1. To add logic that logs the withdrawal transaction, add the following code below the comment:
392+
393+
```csharp
394+
AddTransaction(new Transaction(transactionDate, transactionTime, priorBalance, amount, AccountNumber, AccountNumber, transactionType, description));
395+
```
396+
397+
1. Locate the `// Task 4: Step 8b` comment.
398+
399+
1. To add logic that logs the overdraft fee transaction, add the following code below the comment:
400+
401+
```csharp
402+
AddTransaction(new Transaction(transactionDate, transactionTime, priorBalance, overdraftFee, AccountNumber, AccountNumber, transactionType, overdraftDescription));
403+
```
404+
389405
1. Save the BankAccount.cs file.
390406

391407
1. Build and run the application.

0 commit comments

Comments
 (0)