Skip to content

Commit 013a411

Browse files
committed
updated exercise 3 instructions and solution code
1 parent 58915b7 commit 013a411

6 files changed

Lines changed: 36 additions & 36 deletions

File tree

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

Lines changed: 3 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -6,9 +6,10 @@ public class BankAccount
66
{
77
private static int s_nextAccountNumber;
88
public static double InterestRate;
9+
910
public int AccountNumber { get; }
1011
public string CustomerId { get; }
11-
public double Balance { get; private set; } = 0;
12+
public double Balance { get; internal set; } = 0;
1213
public string AccountType { get; set; } = "Checking";
1314

1415
static BankAccount()
@@ -26,7 +27,7 @@ public BankAccount(string customerIdNumber, double balance = 0, string accountTy
2627
this.AccountType = accountType;
2728
}
2829

29-
// Add a copy constructor here
30+
// Copy constructor for BankAccount
3031
public BankAccount(BankAccount existingAccount)
3132
{
3233
this.AccountNumber = s_nextAccountNumber++;
@@ -35,12 +36,6 @@ public BankAccount(BankAccount existingAccount)
3536
this.AccountType = existingAccount.AccountType;
3637
}
3738

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-
4439
// Method to display account information
4540
public string DisplayAccountInfo()
4641
{

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

Lines changed: 17 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,18 @@ public partial class BankCustomer
99
private string _lastName = "Shao";
1010
public readonly string CustomerId;
1111

12+
public string FirstName
13+
{
14+
get { return _firstName; }
15+
set { _firstName = value; }
16+
}
17+
18+
public string LastName
19+
{
20+
get { return _lastName; }
21+
set { _lastName = value; }
22+
}
23+
1224
static BankCustomer()
1325
{
1426
Random random = new Random();
@@ -22,23 +34,14 @@ public BankCustomer(string firstName, string lastName)
2234
this.CustomerId = (s_nextCustomerId++).ToString("D10");
2335
}
2436

25-
// Copy constructor with unique customerId
37+
// Copy constructor for BankCustomer
2638
public BankCustomer(BankCustomer existingCustomer)
2739
{
28-
this.CustomerId = (s_nextCustomerId++).ToString("D10");
40+
2941
this.FirstName = existingCustomer.FirstName;
3042
this.LastName = existingCustomer.LastName;
31-
}
32-
33-
public string FirstName
34-
{
35-
get { return _firstName; }
36-
set { _firstName = value; }
37-
}
43+
//this.CustomerId = existingCustomer.CustomerId;
44+
this.CustomerId = (s_nextCustomerId++).ToString("D10");
3845

39-
public string LastName
40-
{
41-
get { return _lastName; }
42-
set { _lastName = value; }
4346
}
44-
}
47+
}

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ namespace Classes_M3;
55
public partial class BankCustomer
66
{
77
// Method to return the full name of the customer
8-
public string FullName()
8+
public string ReturnFullName()
99
{
1010
return $"{FirstName} {LastName}";
1111
}
@@ -20,6 +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()}";
23+
return $"Customer ID: {CustomerId}, Name: {ReturnFullName()}";
2424
}
2525
}

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ public static bool IsValidCustomerId(this BankCustomer customer)
1313
// Extension method to greet the customer
1414
public static string GreetCustomer(this BankCustomer customer)
1515
{
16-
return $"Hello, {customer.FirstName} {customer.LastName}!";
16+
return $"Hello, {customer.ReturnFullName()}!";
1717
}
1818
}
1919

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.SetBalance(account.Balance + amount);
12+
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.SetBalance(account.Balance - amount);
21+
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.SetBalance(account.Balance + account.Balance * BankAccount.InterestRate);
41+
account.Balance += account.Balance * BankAccount.InterestRate;
4242
}
4343
}

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

Lines changed: 10 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -426,7 +426,7 @@ Use the following steps to complete this section of the exercise:
426426
427427
1. Notice that the Visual Studio Code environment uses red squiggly lines to indicate errors in your code.
428428
429-
There are two issues that you need to address:
429+
There are two primary issues that you need to address:
430430
431431
- The methods in a static class must be static, so you need to update the method signatures to include the `static` keyword.
432432
- The properties and fields defined by the `BankAccount` class are not directly accessible in the `Transactions` class. You need to update the method signatures to accept a `BankAccount` parameter and use the `BankAccount` instance to access properties within the static methods.
@@ -446,9 +446,9 @@ Use the following steps to complete this section of the exercise:
446446
447447
```
448448
449-
The `Deposit` method came from the `BankAccount` class, where it could access the `Balance` property directly. However, the `Balance` property is no longer available in the `Transactions` class. To update the `Deposit` method, you need to update the method signature to accept a `BankAccount` parameter, and then use the `BankAccount` instance to access the `Balance` property.
449+
The `Deposit` method came from the `BankAccount` class, where it could access the `Balance` property directly. However, the `Balance` property is no longer available in the `Transactions` class. To update the `Deposit` method, you need to update the method signature to accept a `BankAccount` parameter, and then use the `BankAccount` instance to access the `Balance` property. Also, you need to add `static` to the method signature to make it a static method.
450450
451-
1. To add a `BankAccount` parameter to the signature and then reference the account object within the method, update the `Deposit` method to match the following code snippet:
451+
1. To add a `static` modifier and `BankAccount` parameter to the signature, and then reference the account object within the method, update the `Deposit` method to match the following code snippet:
452452
453453
```csharp
454454
@@ -463,9 +463,11 @@ Use the following steps to complete this section of the exercise:
463463
464464
```
465465
466-
Notice that the `Deposit` method now accepts a `BankAccount` parameter named `account`, and that it uses the `account` object to access the `Balance` property. All good so far.
466+
Notice that the `Deposit` method now accepts a `BankAccount` parameter named `account`, and that it uses the `account` object to access the `Balance` property.
467467
468-
However, when the code attempts to update the `account.Balance` property using the `amount` parameter, it finds that the `Balance` property isn't accessible. This is because the setter is private. To update the `Balance` property in the static method, you need to change access control of the setter to `internal`. This will allow the `Transactions` class to modify the `Balance` property while keeping the property encapsulated within the `BankAccount` class.
468+
However, when the code attempts to update the `account.Balance` property using the `amount` parameter, it finds that the set accessor is inaccessible. You can hover the mouse pointer over the `Balance` property to see the error message.
469+
470+
The `Balance` property is inaccessible in `Transactions` because the setter is defined as `private` in `BankAccount`. To update the `Balance` property in your static methods, you need to change access control of the setter to `internal` in the `BankAccount` class. This update will allow the `Transactions` class to modify the `Balance` property while keeping the property encapsulated within the `BankAccount` class.
469471
470472
1. Open the BankAccount.cs file.
471473
@@ -479,7 +481,7 @@ Use the following steps to complete this section of the exercise:
479481
480482
1. Switch back to the Transactions.cs file.
481483
482-
1. Notice that the `Deposit` method now has access to the `Balance` property of the `BankAccount` instance.
484+
1. Notice that the `Deposit` method is now able to set the `account.Balance` property.
483485
484486
1. Repeat the process used to update `Deposit` to correct the issues in the remaining `Transactions` class methods.
485487
@@ -635,7 +637,7 @@ Use the following steps to complete this section of the exercise:
635637
636638
1. Run the app to ensure that your static class updates didn't introduce any bugs.
637639
638-
By moving the transactional methods to a static class, you can organize your code more effectively and improve the readability and maintainability of your code. The BankAccount class should still work as expected, and you should see the same output in the terminal window.
640+
By moving the transactional methods to a static class, your code is more efficiently organized and the maintainability of your code is improved. The BankAccount class should still work as expected, and you should see the same output in the terminal window.
639641
640642
Your app should produce output that's similar to the following example:
641643
@@ -849,7 +851,7 @@ Use the following steps to complete this section of the exercise:
849851
this.FirstName = existingCustomer.FirstName;
850852
this.LastName = existingCustomer.LastName;
851853
//this.CustomerId = existingCustomer.CustomerId;
852-
this.CustomerId = (nextCustomerId++).ToString("D10");
854+
this.CustomerId = (s_nextCustomerId++).ToString("D10");
853855
854856
}
855857

0 commit comments

Comments
 (0)