Skip to content

Commit 3bba93e

Browse files
committed
updates for exercise 3
1 parent 63f1be1 commit 3bba93e

3 files changed

Lines changed: 24 additions & 37 deletions

File tree

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

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ 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; }
1112
public double Balance { get; private set; } = 0;
@@ -32,8 +33,6 @@ public BankAccount(string customerIdNumber, double balance, string accountType)
3233
this.AccountType = accountType;
3334
}
3435

35-
36-
3736
// Method to deposit money into the account
3837
public void Deposit(double amount)
3938
{

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

Lines changed: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,18 @@ public 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,18 +34,6 @@ public BankCustomer(string firstName, string lastName)
2234
this.CustomerId = (s_nextCustomerId++).ToString("D10");
2335
}
2436

25-
public string FirstName
26-
{
27-
get { return _firstName; }
28-
set { _firstName = value; }
29-
}
30-
31-
public string LastName
32-
{
33-
get { return _lastName; }
34-
set { _lastName = value; }
35-
}
36-
3737
// Method to return the full name of the customer
3838
public string ReturnFullName()
3939
{

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

Lines changed: 11 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -74,15 +74,15 @@ Use the following steps to complete this section of the exercise:
7474

7575
The `BankAccount` class represents a bank account with properties and methods to manage account operations.
7676

77-
Notice that the class includes static fields `s_nextAccountNumber` and `InterestRate`, which track the next account number and the interest rate applied to accounts, respectively. Each `BankAccount` instance has properties such as `AccountNumber`, `CustomerId`, `Balance`, and `AccountType`, with default values for balance and account type. The class includes constructors for initializing accounts with or without an initial balance and account type. Methods provided by the class include `Deposit` for adding funds, `Withdraw` for removing funds, `Transfer` for transferring funds between accounts, `ApplyInterest` for applying interest to the balance, and `DisplayAccountInfo` for displaying account details. The class also features a static constructor to initialize static fields.
77+
Notice that the class includes static fields `s_nextAccountNumber` and `InterestRate`, which track the next account number and the interest rate applied to accounts, respectively. In addition to the static fields, the `BankAccount` class defines the following properties: `AccountNumber`, `CustomerId`, `Balance`, and `AccountType`. The class also includes constructors for initializing accounts with or without an initial balance and account type. Methods provided by the class include `Deposit` for adding funds, `Withdraw` for removing funds, `Transfer` for transferring funds between accounts, `ApplyInterest` for applying interest to the balance, and `DisplayAccountInfo` for displaying account details. The class also features a static constructor to initialize static fields.
7878

7979
The `this` keyword is used to access the properties of the current class instance. For example, `this.AccountNumber` refers to the `AccountNumber` property of the current `BankAccount` instance.
8080

8181
1. Open the BankCustomer.cs file and take a minute to review the BankCustomer class.
8282

8383
The BankCustomer class represents a bank customer with properties and methods to manage customer information.
8484

85-
Notice that the class includes a static field `nextCustomerId` to track the next customer ID, and instance fields `FirstName` and `LastName` for storing the customer's first and last names, respectively. Each `BankCustomer` instance has a read-only `CustomerId` property, which is initialized using a static constructor that generates a random starting ID. The class provides properties `FirstName` and `LastName` for accessing and modifying the customer's name. Methods in the class include `ReturnFullName` to return the customer's full name, `UpdateName` to update the customer's name, and `DisplayCustomerInfo` to display customer details. The class also features a constructor for initializing a new customer with a first and last name.
85+
Notice that the class includes a static field `nextCustomerId` to track the next customer ID, and private backing fields `_firstName` and `_lastName` for storing the customer's first and last names, respectively. Each `BankCustomer` instance has a read-only `CustomerId` property, which is initialized using a static constructor that generates a random starting ID. The class provides properties `FirstName` and `LastName` for accessing and modifying the customer's name. Methods in the class include `ReturnFullName` to return the customer's full name, `UpdateName` to update the customer's name, and `DisplayCustomerInfo` to display customer details. The class also features a constructor for initializing a new customer with a first and last name.
8686

8787
1. Open the Extensions.cs file and take a minute to review the BankCustomerExtensions and BankAccountExtensions classes.
8888

@@ -431,7 +431,7 @@ Use the following steps to complete this section of the exercise:
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.
433433
434-
1. Take a moment to consider the `Deposit` method.
434+
1. Take a moment to consider how you'll update the `Deposit` method.
435435
436436
```csharp
437437
@@ -448,7 +448,7 @@ Use the following steps to complete this section of the exercise:
448448
449449
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.
450450
451-
1. To include 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 `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
@@ -465,35 +465,23 @@ Use the following steps to complete this section of the exercise:
465465
466466
Notice that the `Deposit` method now accepts a `BankAccount` parameter named `account`, and that it uses the `account` object to access the `Balance` property. The `account.Balance` property is updated using `amount` parameter.
467467
468-
1. Notice that `account.Balance` can't be modified from the static method because the setter is private.
468+
However, the `Balance` property can't be modified from the static method because the setter is private. To update the `Balance` property, you need to change access control of the setter to `internal`.
469469
470-
To update the `Balance` property, you need to add an `internal` method to the `BankAccount` class that enabled classes within the assembly to modify the `Balance` property. This keeps the `Balance` property encapsulated within the `BankAccount` class while allowing the `Transactions` class to update the `Balance` property.
470+
1. Open the BankAccount.cs file.
471471
472-
1. Add the following `SetBalance` method to the `BankAccount` class:
472+
1. Change the access modifier of the `Balance` property setter to `internal`.
473473
474474
```csharp
475475
476-
internal void SetBalance(double balance)
477-
{
478-
Balance = balance;
479-
}
476+
public double Balance { get; internal set; } = 0;
480477
481478
```
482479
483-
1. To use the `SetBalance` method in the `Transactions` class, update the `Deposit` method using the following code:
480+
The `internal` access modifier allows the `Transactions` class to modify the `Balance` property while keeping the property encapsulated within the `BankAccount` class.
484481
485-
```csharp
482+
1. Switch back to the Transactions.cs file.
486483
487-
// Method to deposit money into the account
488-
public static void Deposit(BankAccount account, double amount)
489-
{
490-
if (amount > 0)
491-
{
492-
account.SetBalance(account.Balance + amount);
493-
}
494-
}
495-
496-
```
484+
1. Notice that the `Deposit` method now has access to the `Balance` property of the `BankAccount` instance.
497485
498486
1. Repeat the process used to update `Deposit` to correct the issues in the remaining `Transactions` class methods.
499487

0 commit comments

Comments
 (0)