Skip to content

Commit 8b2f55e

Browse files
committed
exercise 1 updates
1 parent c4baa66 commit 8b2f55e

File tree

2 files changed

+24
-14
lines changed

2 files changed

+24
-14
lines changed

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

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,11 @@ static BankCustomer()
1515
nextCustomerId = random.Next(10000000, 20000000);
1616
}
1717

18+
public BankCustomer()
19+
{
20+
this.customerId = (nextCustomerId++).ToString("D10");
21+
}
22+
1823
public BankCustomer(string firstName, string lastName)
1924
{
2025
fName = firstName;

Instructions/Labs/l2p2-lp1-m1-exercise-create-classes-and-objects-in-csharp.md

Lines changed: 19 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -183,6 +183,8 @@ Use the following steps to complete this task:
183183
184184
## Update the Program.cs file to create instances of the BankCustomer class
185185
186+
The `new` operator is used to create objects based on a class constructor. Each object has its own data fields, and the values assigned to the fields can be different for each object.
187+
186188
In this task, you update the Program.cs file to create instances of the `BankCustomer` class and run the app to verify that the instances are created successfully.
187189
188190
Use the following steps to complete this task:
@@ -294,6 +296,8 @@ Use the following steps to complete this task:
294296
295297
## Add public fields and updated constructors to the BankCustomer class
296298
299+
Public fields are accessible from outside the class. They can be read from and written to by any code that has access to the object. Public fields are often used to expose object data to other classes.
300+
297301
In this task, you add public fields to the `BankCustomer` class, update the instance constructors to use additional parameters, and then update the Program.cs file to access each object's public data.
298302
299303
Use the following steps to complete this task:
@@ -359,11 +363,11 @@ Use the following steps to complete this task:
359363
360364
```
361365
362-
The constructor initializes the `fName`, `lName`, and `accountId` fields with the values of the `firstName`, `lastName`, and `customerIdNumber` parameters, respectively.
366+
The constructor initializes the `fName`, `lName`, and `customerId` fields with the values of the `firstName`, `lastName`, and `customerIdNumber` parameters, respectively.
363367
364368
1. Switch to the Program.cs file
365369
366-
1. To create local variables for `firstName`, `lastName`, and `accountNumber` just below the `namespace` declaration, enter the following code:
370+
1. To create local variables for `firstName`, `lastName`, and `customerIdNumber` just below the `namespace` declaration, enter the following code:
367371
368372
```csharp
369373
@@ -430,7 +434,6 @@ Use the following steps to complete this task:
430434
Console.WriteLine($"BankCustomer 2: {customer2.fName} {customer2.lName} {customer2.customerId}");
431435
Console.WriteLine($"BankCustomer 3: {customer3.fName} {customer3.lName} {customer3.customerId}");
432436
433-
434437
```
435438
436439
BankCustomer.cs file:
@@ -443,7 +446,7 @@ Use the following steps to complete this task:
443446
444447
public class BankCustomer
445448
{
446-
// add public fields for fName, lName, and accountID
449+
// add public fields for fName, lName, and customerId
447450
public string fName = "Tim";
448451
public string lName = "Shao";
449452
public string customerId = "1010101010";
@@ -480,10 +483,12 @@ Use the following steps to complete this task:
480483
481484
```
482485
483-
Notice that customer1 and customer2 share the same ID number. The public fields are initialized with default values. Although the constructors update the fields with the values passed as parameters, there are clearly some issues to address.
486+
Notice that the first two customers share the same ID number. Public fields in the BankCustomer class are initialized using default values. Although the constructors update some fields with the values passed as parameters, there are issues that must be addressed.
484487
485488
## Update the BankCustomer class using static members to ensure unique customer IDs
486489
490+
Static fields are initialized before an instance of the class is created. They're accessed using the class name, not an instance of the class, and they're shared among all instances of the class. Static constructors are called when a class is loaded into memory. A static constructor is called only once, regardless of how many instances of the class are created.
491+
487492
In this task, you update the `BankCustomer` class using a static field and static constructor to ensure unique `customerId` values are assigned to each new customer object.
488493
489494
Use the following steps to complete this task:
@@ -496,7 +501,7 @@ Use the following steps to complete this task:
496501
497502
public class BankCustomer
498503
{
499-
// add public fields for fName, lName, and accountID
504+
// add public fields for fName, lName, and customerId
500505
public string fName = "Tim";
501506
public string lName = "Shao";
502507
public string customerId = "1010101010";
@@ -555,9 +560,9 @@ Use the following steps to complete this task:
555560
556561
```
557562
558-
The static constructor is called when the `BankCustomer` class is loaded into memory. The static constructor initializes the `nextCustomerId` field with a random value between 10,000,000 and 20,000,000.
563+
The static constructor is called when the `BankCustomer` class is loaded into memory, and before any instances of the class are created. The static constructor initializes the `nextCustomerId` field with a random eight digit integer.
559564
560-
1. To update the parameterless constructor to assign the `customerId` field using the `nextCustomerId` field, add the following code to the `BankCustomer` class definition:
565+
1. To assign a unique value to `customerId` inside your parameterless constructor, update the constructor to match the following code:
561566
562567
```csharp
563568
@@ -568,12 +573,12 @@ Use the following steps to complete this task:
568573
569574
```
570575
571-
The constructor initializes the `customerId` field using the `nextCustomerId` field. Notice that `nextCustomerId` is incremented by 1 before the `customerId` field is assigned a value.
576+
The updated constructor initializes the `customerId` field using the already initialized `nextCustomerId` field. Notice that `nextCustomerId` is incremented by 1 before the `customerId` field is assigned a value.
572577
573578
> [!NOTE]
574-
> The `this` keyword is used to refer to the current instance of the class. It's used to access fields, properties, and methods of the current instance.
579+
> The `this` keyword is used to refer to the current instance of the class. It's used to access fields, properties, and methods of the current instance. The `this` keyword is not available in a static constructor.
575580
576-
1. To update the constructor that accepts the first and last name fields, add the following code to the `BankCustomer` class definition:
581+
1. To assign a unique value to `customerId` inside your constructor that accepts `firstName` and `lastName` parameters, update the constructor to match the following code:
577582
578583
```csharp
579584
@@ -586,11 +591,11 @@ Use the following steps to complete this task:
586591
587592
```
588593
589-
Once again, the constructor initializes the `customerId` field using the incremented `nextCustomerId` field value.
594+
Once again, the constructor initializes the `customerId` field using an incremented `nextCustomerId` value.
590595
591596
1. Delete the constructor accepts the `firstName`, `lastName`, and `customerIdNumber` parameters.
592597
593-
The static constructor ensures that each new instance of the `BankCustomer` class is assigned a unique customer ID. You already have a constructor that accepts the `firstName` and `lastName` parameters, so a constructor that accepts all three parameters is no longer needed.
598+
Ensuring unique customer IDs includes removing the opportunity to create a new customer with an externally provided ID. Also, your parameterless and static constructors ensure that every new instance of the `BankCustomer` class is assigned a unique customer ID.
594599
595600
1. Switch to the Program.cs file.
596601
@@ -681,7 +686,7 @@ Use the following steps to complete this task:
681686
682687
In this task, you create an `BankAccount` class that initializes a combination of public and static fields. You also create constructors that initialize the fields with default values and values passed as parameters.
683688
684-
You need a `BankAccount` class that includes the following fields:
689+
You want a `BankAccount` class that includes the following fields:
685690
686691
- `accountNumber` - A public field that stores the account number.
687692
- `balance` - A public field that stores the account balance.

0 commit comments

Comments
 (0)