Skip to content

Commit f40c016

Browse files
committed
adding some initial files to learning path 3
1 parent ff0aab9 commit f40c016

17 files changed

Lines changed: 918 additions & 0 deletions

File tree

Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
# This workflow is triggered on two events: workflow_dispatch and push.
2+
#
3+
# - The workflow_dispatch event allows you to manually trigger the workflow from GitHub's UI.
4+
# - The push event triggers the workflow whenever there's a push to the master branch, but only
5+
# if the changes include files in the LearnModuleExercises/SampleApps/** directory.
6+
#
7+
# The defaults section sets the default shell for all run commands in the workflow to PowerShell (pwsh).
8+
#
9+
# The workflow consists of a single job named create_zip, which runs on the latest version of Ubuntu.
10+
#
11+
# This job has three steps:
12+
#
13+
# 1. The Checkout step uses the actions/checkout@v4 action to checkout the repository's code onto the
14+
# runner. This is a common first step in most workflows as it allows subsequent steps to operate on
15+
# the codebase.
16+
#
17+
# 2. The Create SampleApps zip step changes the current directory to ./LearnModuleExercises/SampleApps
18+
# and then creates a zip file of all the files in that directory, including those in the .vscode
19+
# subdirectory. The -r option is used to zip directories recursively and the -q option is used to run
20+
# the command quietly without printing a lot of output. The resulting zip file is saved in the
21+
# ../Downloads directory with the name SampleApps.zip.
22+
#
23+
# 3. The Commit and push step uses the Endbug/add-and-commit@v7 action to add the newly created zip file
24+
# to the repository, commit the changes with the message 'Updating Zip for API source files', and then
25+
# push the changes back to the repository. The add input is set to the path of the zip file and the push
26+
# input is set to true to enable pushing.
27+
#
28+
# This workflow is useful for automatically packaging and versioning sample applications whenever changes
29+
# are made to them.
30+
#
31+
name: CreateLP3SamplesZip
32+
on:
33+
workflow_dispatch:
34+
push:
35+
branches:
36+
- 'main'
37+
paths:
38+
- DownloadableCodeProjects/LP3_reuse-inheritance-polymorphism/**
39+
40+
defaults:
41+
run:
42+
shell: pwsh
43+
44+
jobs:
45+
create_zip:
46+
runs-on: ubuntu-latest
47+
steps:
48+
- name: Checkout
49+
uses: actions/checkout@v4
50+
- name: Create LP1 SampleApps zip
51+
run: |
52+
cd ./DownloadableCodeProjects/LP3_reuse-inheritance-polymorphism
53+
rm -f ../Downloads/LP3SampleApps.zip
54+
zip -r -q ../Downloads/LP3SampleApps.zip $(git ls-files)
55+
- name: Commit and push
56+
uses: Endbug/add-and-commit@v7
57+
with:
58+
add: '["DownloadableCodeProjects/Downloads/LP3SampleApps.zip"]'
59+
message: 'Updating Zip with sample app source files'
60+
push: true
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
placeholder
Lines changed: 77 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,77 @@
1+
using System;
2+
3+
namespace Classes_M2;
4+
5+
public class BankAccount
6+
{
7+
private static int nextAccountNumber;
8+
public static double interestRate;
9+
public int AccountNumber { get; }
10+
public string CustomerId { get; }
11+
public double Balance { get; private set; } = 0;
12+
public string AccountType { get; set; } = "Checking";
13+
14+
static BankAccount()
15+
{
16+
Random random = new Random();
17+
nextAccountNumber = random.Next(10000000, 20000000);
18+
interestRate = 0;
19+
}
20+
21+
public BankAccount(string customerIdNumber)
22+
{
23+
this.AccountNumber = nextAccountNumber++;
24+
this.CustomerId = customerIdNumber;
25+
}
26+
27+
public BankAccount(string customerIdNumber, double balance, string accountType)
28+
{
29+
this.AccountNumber = nextAccountNumber++;
30+
this.CustomerId = customerIdNumber;
31+
this.Balance = balance;
32+
this.AccountType = accountType;
33+
}
34+
35+
// Method to deposit money into the account
36+
public void Deposit(double amount)
37+
{
38+
if (amount > 0)
39+
{
40+
Balance += amount;
41+
}
42+
}
43+
44+
// Method to withdraw money from the account
45+
public bool Withdraw(double amount)
46+
{
47+
if (amount > 0 && Balance >= amount)
48+
{
49+
Balance -= amount;
50+
return true;
51+
}
52+
return false;
53+
}
54+
55+
// Method to transfer money to another account
56+
public bool Transfer(BankAccount targetAccount, double amount)
57+
{
58+
if (Withdraw(amount))
59+
{
60+
targetAccount.Deposit(amount);
61+
return true;
62+
}
63+
return false;
64+
}
65+
66+
// Method to apply interest to the account
67+
public void ApplyInterest()
68+
{
69+
Balance += Balance * interestRate;
70+
}
71+
72+
// Method to display account information
73+
public string DisplayAccountInfo()
74+
{
75+
return $"Account Number: {AccountNumber}, Type: {AccountType}, Balance: {Balance}, Interest Rate: {interestRate}, Customer ID: {CustomerId}";
76+
}
77+
}
Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
using System;
2+
3+
namespace Classes_M2;
4+
5+
public class BankCustomer
6+
{
7+
private static int nextCustomerId;
8+
private string fName = "Tim";
9+
private string lName = "Shao";
10+
public readonly string customerId;
11+
12+
static BankCustomer()
13+
{
14+
Random random = new Random();
15+
nextCustomerId = random.Next(10000000, 20000000);
16+
}
17+
18+
public BankCustomer(string firstName, string lastName)
19+
{
20+
FirstName = firstName;
21+
LastName = lastName;
22+
this.customerId = (nextCustomerId++).ToString("D10");
23+
}
24+
25+
public string FirstName
26+
{
27+
get { return fName; }
28+
set { fName = value; }
29+
}
30+
31+
public string LastName
32+
{
33+
get { return lName; }
34+
set { lName = value; }
35+
}
36+
37+
// Method to return the full name of the customer
38+
public string ReturnFullName()
39+
{
40+
return $"{FirstName} {LastName}";
41+
}
42+
43+
// Method to update the customer's name
44+
public void UpdateName(string firstName, string lastName)
45+
{
46+
FirstName = firstName;
47+
LastName = lastName;
48+
}
49+
50+
// Method to display customer information
51+
public string DisplayCustomerInfo()
52+
{
53+
return $"Customer ID: {customerId}, Name: {ReturnFullName()}";
54+
}
55+
}
Lines changed: 71 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,71 @@
1+
using Classes_M2;
2+
3+
// Step 1: Create BankCustomer objects
4+
Console.WriteLine("Creating BankCustomer objects...");
5+
string firstName = "Tim";
6+
string lastName = "Shao";
7+
8+
BankCustomer customer1 = new BankCustomer(firstName, lastName);
9+
10+
firstName = "Lisa";
11+
BankCustomer customer2 = new BankCustomer(firstName, lastName);
12+
13+
firstName = "Sandy";
14+
lastName = "Zoeng";
15+
BankCustomer customer3 = new BankCustomer(firstName, lastName);
16+
17+
Console.WriteLine($"BankCustomer 1: {customer1.FirstName} {customer1.LastName} {customer1.customerId}");
18+
Console.WriteLine($"BankCustomer 2: {customer2.FirstName} {customer2.LastName} {customer2.customerId}");
19+
Console.WriteLine($"BankCustomer 3: {customer3.FirstName} {customer3.LastName} {customer3.customerId}");
20+
21+
// Step 2: Create BankAccount objects for customers
22+
Console.WriteLine("\nCreating BankAccount objects for customers...");
23+
BankAccount account1 = new BankAccount(customer1.customerId);
24+
BankAccount account2 = new BankAccount(customer2.customerId, 1500, "Checking");
25+
BankAccount account3 = new BankAccount(customer3.customerId, 2500, "Checking");
26+
27+
Console.WriteLine($"Account 1: Account # {account1.AccountNumber}, type {account1.AccountType}, balance {account1.Balance}, rate {BankAccount.interestRate}, customer ID {account1.CustomerId}");
28+
Console.WriteLine($"Account 2: Account # {account2.AccountNumber}, type {account2.AccountType}, balance {account2.Balance}, rate {BankAccount.interestRate}, customer ID {account2.CustomerId}");
29+
Console.WriteLine($"Account 3: Account # {account3.AccountNumber}, type {account3.AccountType}, balance {account3.Balance}, rate {BankAccount.interestRate}, customer ID {account3.CustomerId}");
30+
31+
// Step 3: Demonstrate the use of BankCustomer properties
32+
Console.WriteLine("\nUpdating BankCustomer 1's name...");
33+
customer1.FirstName = "Thomas";
34+
customer1.LastName = "Margand";
35+
Console.WriteLine($"Updated BankCustomer 1: {customer1.FirstName} {customer1.LastName} {customer1.customerId}");
36+
37+
// Step 4: Demonstrate the use of BankAccount methods
38+
Console.WriteLine("\nDemonstrating BankAccount methods...");
39+
40+
// Deposit
41+
Console.WriteLine("Depositing 500 into Account 1...");
42+
account1.Deposit(500);
43+
Console.WriteLine($"Account 1 after deposit: Balance = {account1.Balance}");
44+
45+
// Withdraw
46+
Console.WriteLine("Withdrawing 200 from Account 2...");
47+
bool withdrawSuccess = account2.Withdraw(200);
48+
Console.WriteLine($"Account 2 after withdrawal: Balance = {account2.Balance}, Withdrawal successful: {withdrawSuccess}");
49+
50+
// Transfer
51+
Console.WriteLine("Transferring 300 from Account 3 to Account 1...");
52+
bool transferSuccess = account3.Transfer(account1, 300);
53+
Console.WriteLine($"Account 3 after transfer: Balance = {account3.Balance}, Transfer successful: {transferSuccess}");
54+
Console.WriteLine($"Account 1 after receiving transfer: Balance = {account1.Balance}");
55+
56+
// Apply interest
57+
Console.WriteLine("Applying interest to Account 1...");
58+
account1.ApplyInterest();
59+
Console.WriteLine($"Account 1 after applying interest: Balance = {account1.Balance}");
60+
61+
// Step 5: Demonstrate the use of extension methods
62+
Console.WriteLine("\nDemonstrating extension methods...");
63+
Console.WriteLine(customer1.GreetCustomer());
64+
Console.WriteLine($"Is customer1 ID valid? {customer1.IsValidCustomerId()}");
65+
Console.WriteLine($"Can account2 withdraw 2000? {account2.CanWithdraw(2000)}");
66+
Console.WriteLine($"Is account3 overdrawn? {account3.IsOverdrawn()}");
67+
68+
// Step 6: Display customer and account information
69+
Console.WriteLine("\nDisplaying customer and account information...");
70+
Console.WriteLine(customer1.DisplayCustomerInfo());
71+
Console.WriteLine(account1.DisplayAccountInfo());
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
<Project Sdk="Microsoft.NET.Sdk">
2+
3+
<PropertyGroup>
4+
<OutputType>Exe</OutputType>
5+
<TargetFramework>net9.0</TargetFramework>
6+
<ImplicitUsings>enable</ImplicitUsings>
7+
<Nullable>enable</Nullable>
8+
</PropertyGroup>
9+
10+
</Project>
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
placeholder
Lines changed: 77 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,77 @@
1+
using System;
2+
3+
namespace Classes_M2;
4+
5+
public class BankAccount
6+
{
7+
private static int nextAccountNumber;
8+
public static double interestRate;
9+
public int AccountNumber { get; }
10+
public string CustomerId { get; }
11+
public double Balance { get; private set; } = 0;
12+
public string AccountType { get; set; } = "Checking";
13+
14+
static BankAccount()
15+
{
16+
Random random = new Random();
17+
nextAccountNumber = random.Next(10000000, 20000000);
18+
interestRate = 0;
19+
}
20+
21+
public BankAccount(string customerIdNumber)
22+
{
23+
this.AccountNumber = nextAccountNumber++;
24+
this.CustomerId = customerIdNumber;
25+
}
26+
27+
public BankAccount(string customerIdNumber, double balance, string accountType)
28+
{
29+
this.AccountNumber = nextAccountNumber++;
30+
this.CustomerId = customerIdNumber;
31+
this.Balance = balance;
32+
this.AccountType = accountType;
33+
}
34+
35+
// Method to deposit money into the account
36+
public void Deposit(double amount)
37+
{
38+
if (amount > 0)
39+
{
40+
Balance += amount;
41+
}
42+
}
43+
44+
// Method to withdraw money from the account
45+
public bool Withdraw(double amount)
46+
{
47+
if (amount > 0 && Balance >= amount)
48+
{
49+
Balance -= amount;
50+
return true;
51+
}
52+
return false;
53+
}
54+
55+
// Method to transfer money to another account
56+
public bool Transfer(BankAccount targetAccount, double amount)
57+
{
58+
if (Withdraw(amount))
59+
{
60+
targetAccount.Deposit(amount);
61+
return true;
62+
}
63+
return false;
64+
}
65+
66+
// Method to apply interest to the account
67+
public void ApplyInterest()
68+
{
69+
Balance += Balance * interestRate;
70+
}
71+
72+
// Method to display account information
73+
public string DisplayAccountInfo()
74+
{
75+
return $"Account Number: {AccountNumber}, Type: {AccountType}, Balance: {Balance}, Interest Rate: {interestRate}, Customer ID: {CustomerId}";
76+
}
77+
}

0 commit comments

Comments
 (0)