Skip to content

Commit 4d64165

Browse files
committed
LP4 M3 updated instructions and starter/solution code
1 parent 418b9cd commit 4d64165

File tree

8 files changed

+386
-565
lines changed

8 files changed

+386
-565
lines changed
Lines changed: 80 additions & 76 deletions
Original file line numberDiff line numberDiff line change
@@ -1,99 +1,103 @@
11
using System;
22
using System.Collections.Generic;
3+
using System.Linq;
34

4-
namespace BankApp
5+
namespace BankApp;
6+
7+
// TASK 2: Define Enum, Struct, and Record types
8+
9+
// TASK 2: Step 1 - Define the BankAccountType enum with Checking, Savings, and Business values
10+
11+
public enum BankAccountType
512
{
6-
// TASK 1: Define the AccountType enum
7-
// TASK 1: Step 1 - Define the AccountType enum
8-
public enum AccountType
9-
{
10-
Checking,
11-
Savings,
12-
Business
13-
}
13+
Checking,
14+
Savings,
15+
Business
16+
}
1417

15-
public static class AccountTypeExtensions
18+
// TASK 2: Step 2 - Define an extension method to provide descriptions for each BankAccountType value
19+
public static class BankAccountTypeExtensions
20+
{
21+
// TASK 1: Step 2 - Add an extension method to provide descriptions for each AccountType.
22+
public static string GetDescription(this BankAccountType accountType)
1623
{
17-
// TASK 1: Step 2 - Add an extension method to provide descriptions for each AccountType.
18-
public static string GetDescription(this AccountType accountType)
24+
return accountType switch
1925
{
20-
return accountType switch
21-
{
22-
AccountType.Checking => "A standard checking account.",
23-
AccountType.Savings => "A savings account with interest.",
24-
AccountType.Business => "A business account for companies.",
25-
_ => "Unknown account type."
26-
};
27-
}
26+
BankAccountType.Checking => "A standard checking account.",
27+
BankAccountType.Savings => "A savings account with interest.",
28+
BankAccountType.Business => "A business account for companies.",
29+
_ => "Unknown account type."
30+
};
2831
}
32+
}
2933

30-
// TASK 2: Define the Transaction struct
31-
// TASK 2: Step 1 - Define the Transaction struct
32-
public readonly struct Transaction
34+
// TASK 2: Step 3 - Define the BankAccountNumber struct
35+
public readonly struct BankAccountNumber
36+
{
37+
public string Value { get; }
38+
public BankAccountNumber(string value)
3339
{
34-
public double Amount { get; }
35-
public DateTime Date { get; }
36-
public string Description { get; }
40+
// Simple format: 12 digits (demo-friendly)
41+
if (value is null || value.Length != 12 || !value.All(char.IsDigit))
42+
throw new ArgumentException("Account numbers must be 12 digits.");
43+
Value = value;
44+
}
45+
public override string ToString() => Value;
46+
}
3747

38-
public Transaction(double amount, DateTime date, string description)
39-
{
40-
Amount = amount;
41-
Date = date;
42-
Description = description;
43-
}
48+
// TASK 2: Step 4 - Define the AccountHolderDetails record
49+
public record AccountHolderDetails(string Name, string CustomerId, string Address);
4450

45-
public override string ToString()
46-
{
47-
return $"{Date.ToShortDateString()}: {Description} - {Amount:C}";
48-
}
51+
// TASK 2: Step 5 - Define the Transaction record
52+
public record Transaction(decimal Amount, DateTime Date, string Description)
53+
{
54+
public override string ToString()
55+
{
56+
return $"{Date.ToShortDateString()}: {Description} - {Amount:C}";
4957
}
58+
}
5059

51-
// TASK 3: Define the Customer record
52-
// TASK 3: Step 1 - Define the Customer record
53-
public record Customer(string Name, string CustomerId, string Address);
5460

55-
// TASK 4: Implement the BankAccount class
56-
// TASK 4: Step 1 - Add properties for AccountNumber, Balance, AccountHolder, and Type.
57-
public class BankAccount
58-
{
59-
public int AccountNumber { get; }
60-
public AccountType Type { get; }
61-
public Customer AccountHolder { get; }
62-
public double Balance { get; private set; }
61+
public class BankAccount
62+
{
63+
// TASK 3: Implement the BankAccount class
64+
// TASK 3: Step 1 - Add properties for BankAccountNumber, BankAccountType, Balance, AccountHolderDetails, and a Transactions list.
65+
public BankAccountNumber AccountNumber { get; }
66+
public BankAccountType AccountType { get; }
67+
public decimal Balance { get; private set; }
68+
public AccountHolderDetails AccountHolder { get; }
69+
private List<Transaction> Transactions { get; } = new();
6370

64-
// TASK 4: Step 2 - Add a constructor to initialize the properties.
65-
public BankAccount(int accountNumber, AccountType type, Customer accountHolder, double initialBalance = 0)
66-
{
67-
AccountNumber = accountNumber;
68-
Type = type;
69-
AccountHolder = accountHolder;
70-
Balance = initialBalance;
71-
}
7271

73-
// TASK 4: Step 3 - Add a method to deposit money into the account.
74-
public void AddTransaction(double amount, string description)
75-
{
76-
Balance += amount;
77-
Transactions.Add(new Transaction(amount, DateTime.Now, description));
78-
}
72+
// TASK 3: Step 2 - Add a constructor to initialize the properties.
73+
public BankAccount(BankAccountNumber accountNumber, BankAccountType accountType, AccountHolderDetails accountHolder, decimal initialBalance = 0)
74+
{
75+
AccountNumber = accountNumber;
76+
AccountType = accountType;
77+
AccountHolder = accountHolder;
78+
Balance = initialBalance;
79+
}
7980

80-
// TASK 4: Step 4 - Add a method to withdraw money from the account.
81-
public string DisplayAccountInfo()
82-
{
83-
return $"Account Holder: {AccountHolder.Name}, Account Number: {AccountNumber}, Type: {Type}, Balance: {Balance:C}";
84-
}
81+
// TASK 3: Step 3 - Add a method for deposits/withdrawals that updates the balance and records the transaction.
82+
public void AddTransaction(decimal amount, string description)
83+
{
84+
Balance += amount;
85+
Transactions.Add(new Transaction(amount, DateTime.Now, description));
86+
}
8587

86-
// TASK 4: Step 5 - Add a method to display account information.
87-
private List<Transaction> Transactions { get; } = new();
88+
// TASK 3: Step 4 - Add a method to display account information.
89+
public string DisplayAccountInfo()
90+
{
91+
return $"Account Holder: {AccountHolder.Name}, Account Number: {AccountNumber}, Type: {AccountType}, Balance: {Balance:C}";
92+
}
8893

89-
// TASK 4: Step 6 - Add a list to track transactions.
90-
public void DisplayTransactions()
94+
// TASK 3: Step 5 - Add a method to display transactions.
95+
public void DisplayTransactions()
96+
{
97+
Console.WriteLine("Transactions:");
98+
foreach (var transaction in Transactions)
9199
{
92-
Console.WriteLine("Transactions:");
93-
foreach (var transaction in Transactions)
94-
{
95-
Console.WriteLine(transaction);
96-
}
100+
Console.WriteLine(transaction);
97101
}
98102
}
99-
}
103+
}

DownloadableCodeProjects/LP4_manage-app-data/Data_M3/Solution/Solution.csproj renamed to DownloadableCodeProjects/LP4_manage-app-data/Data_M3/Solution/Data_M3.csproj

File renamed without changes.
Lines changed: 36 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -1,50 +1,52 @@
11
using System;
22

3-
namespace BankApp
3+
namespace BankApp;
4+
5+
class Program
46
{
5-
class Program
7+
static void Main(string[] args)
68
{
7-
static void Main(string[] args)
8-
{
9-
Console.WriteLine("Welcome to the Bank App!");
9+
Console.WriteLine("Welcome to the Bank App!");
10+
11+
// TASK 4: Display basic bank account information
12+
13+
// TASK 4: Step 1 - Create the AccountHolderDetails and BankAccountNumber for a new bank account.
14+
AccountHolderDetails accountHolderDetails = new("Tim Shao", "123456789", "123 Elm Street");
15+
BankAccountNumber accountNumber = new BankAccountNumber("000012345678");
1016

11-
// TASK 5: Display Basic Bank Account Information
12-
// TASK 5: Step 1 - Create a BankAccount object.
13-
// TASK 5: Step 2 - Call the DisplayAccountInfo method to output account details.
17+
// TASK 4: Step 2 - Create a Checking account with $500 using accountHolderDetails and accountNumber.
18+
BankAccount bankAccount = new(accountNumber, BankAccountType.Checking, accountHolderDetails, 500m);
1419

15-
// Create a BankAccount
16-
Customer customer1 = new("Tim Shao", "C123456789", "123 Elm Street");
17-
BankAccount account = new(12345678, AccountType.Checking, customer1, 500);
18-
Console.WriteLine(account.DisplayAccountInfo());
20+
// TASK 4: Step 3 - Display the account type description and account details.
21+
Console.WriteLine("\nTASK 4: Display basic bank account information");
22+
Console.WriteLine($"Account type description: {bankAccount.AccountType.GetDescription()}");
23+
Console.WriteLine(bankAccount.DisplayAccountInfo());
1924

20-
// TASK 6: Perform Transactions
21-
// TASK 6: Step 1 - Call the AddTransaction method to add a deposit to the account.
22-
// TASK 6: Step 2 - Call the AddTransaction method to add a withdrawal to the account.
25+
// TASK 5: Demonstrate successful transactions
26+
// TASK 5: Step 1 - Use the AddTransaction method for deposits and withdrawals.
27+
bankAccount.AddTransaction(200m, "Deposit");
28+
bankAccount.AddTransaction(-50m, "ATM Withdrawal");
2329

24-
// Add Transactions
25-
account.AddTransaction(200, "Deposit");
26-
account.AddTransaction(-50, "ATM Withdrawal");
30+
// TASK 5: Step 2 - Display Account Info and Transactions
31+
Console.WriteLine("\nTASK 5: Demonstrate using the Transaction record");
32+
Console.WriteLine(bankAccount.DisplayAccountInfo());
33+
bankAccount.DisplayTransactions();
2734

28-
// TASK 7: Display Transaction History
29-
// TASK 7: Step 1 - Call the DisplayTransactions method to output the list of transactions.
35+
// TASK 6: Demonstrate record comparison and the immutability of readonly structs
36+
// TASK 6: Step 1 - Create a second AccountHolderDetails object with identical properties.
37+
AccountHolderDetails customer2 = new("Tim Shao", "123456789", "123 Elm Street");
3038

31-
// Display Account Info and Transactions
32-
Console.WriteLine(account.DisplayAccountInfo());
33-
account.DisplayTransactions();
3439

35-
// TASK 8: Demonstrate Record Comparison
36-
// TASK 8: Step 1 - Create two Customer objects with identical properties.
37-
// TASK 8: Step 2 - Compare the two Customer objects using the == operator.
40+
// TASK 6: Step 2 - Compare the two Customer objects using the == operator.
41+
Console.WriteLine("\nTASK 6: Demonstrate record comparison and struct immutability");
42+
Console.WriteLine($"Are customers equal? {accountHolderDetails == customer2}");
3843

39-
Customer customer2 = new("Tim Shao", "C123456789", "123 Elm Street");
40-
Console.WriteLine($"Are customers equal? {customer1 == customer2}");
44+
// TASK 6: Step 3 - create an instance of the readonly struct BankAccountNumber.
45+
BankAccountNumber accountNumber2 = new BankAccountNumber("000123456789");
46+
Console.WriteLine($"Original Account Number: {accountNumber2}");
4147

42-
// TASK 9: Demonstrate Immutability of Structs
43-
// TASK 9: Step 1 - Create a Transaction object.
44-
// TASK 9: Step 2 - Display the transaction details using the ToString method.
48+
// TASK 6: Step 4 - Attempt to change the Value property of the BankAccountNumber struct.
49+
// accountNumber2.Value = "000987654321"; // Uncommenting this line will cause an error
4550

46-
Transaction transaction = new(100, DateTime.Now, "Test Transaction");
47-
Console.WriteLine($"Immutable Transaction: {transaction}");
48-
}
4951
}
5052
}
Lines changed: 40 additions & 81 deletions
Original file line numberDiff line numberDiff line change
@@ -1,84 +1,43 @@
11
using System;
2+
using System.Collections.Generic;
3+
using System.Linq;
24

3-
namespace BankApp
5+
namespace BankApp;
6+
7+
// TASK 2: Define Enum, Struct, and Record types
8+
9+
// TASK 2: Step 1 - Define the BankAccountType enum with Checking, Savings, and Business values
10+
11+
12+
// TASK 2: Step 2 - Define an extension method to provide descriptions for each BankAccountType value
13+
14+
15+
// TASK 2: Step 3 - Define the BankAccountNumber struct
16+
17+
18+
// TASK 2: Step 4 - Define the AccountHolderDetails record
19+
20+
21+
// TASK 2: Step 5 - Define the Transaction record
22+
23+
24+
25+
public class BankAccount
426
{
5-
// TASK 1: Define the AccountType enum
6-
// TASK 1: Step 1 - Define the AccountType enum
7-
// - Add an enum named AccountType with values like Checking, Savings, and Business.
8-
9-
public enum AccountType
10-
{
11-
Checking,
12-
Savings,
13-
Business
14-
}
15-
16-
// TASK 2: Define the Transaction struct
17-
// TASK 2: Step 1 - Define the Transaction struct
18-
// - Add a struct named Transaction with properties for Amount, Date, and Description.
19-
// - Add a constructor to initialize the properties.
20-
// - Override the ToString method to format transaction details.
21-
22-
public struct Transaction
23-
{
24-
public double Amount { get; set; }
25-
public DateTime Date { get; set; }
26-
public string Description { get; set; }
27-
}
28-
29-
// TASK 3: Define the Customer record
30-
// TASK 3: Step 1 - Define the Customer record
31-
// - Add a record named Customer with properties for Name, CustomerId, and Address.
32-
33-
public record Customer
34-
{
35-
public string Name { get; init; }
36-
public int CustomerId { get; init; }
37-
public string Address { get; init; }
38-
}
39-
40-
public class BankAccount
41-
{
42-
// TASK 4: Implement the BankAccount class
43-
// TASK 4: Step 1 - Add properties for AccountNumber, Balance, AccountHolder, and Type.
44-
// TASK 4: Step 2 - Add a constructor to initialize the properties.
45-
// TASK 4: Step 3 - Add a method to deposit money into the account.
46-
// TASK 4: Step 4 - Add a method to withdraw money from the account.
47-
// TASK 4: Step 5 - Add a method to display account information.
48-
// TASK 4: Step 6 - Add a list to track transactions.
49-
// TASK 4: Step 7 - Add a method to add transactions to the list.
50-
// TASK 4: Step 8 - Add a method to display all transactions.
51-
52-
public int AccountNumber { get; }
53-
public double Balance { get; private set; }
54-
55-
public BankAccount(int accountNumber, double initialBalance = 0)
56-
{
57-
AccountNumber = accountNumber;
58-
Balance = initialBalance;
59-
}
60-
61-
public void Deposit(double amount)
62-
{
63-
if (amount > 0)
64-
{
65-
Balance += amount;
66-
}
67-
}
68-
69-
public bool Withdraw(double amount)
70-
{
71-
if (amount > 0 && Balance >= amount)
72-
{
73-
Balance -= amount;
74-
return true;
75-
}
76-
return false;
77-
}
78-
79-
public string DisplayAccountInfo()
80-
{
81-
return $"Account Number: {AccountNumber}, Balance: {Balance:C}";
82-
}
83-
}
84-
}
27+
// TASK 3: Implement the BankAccount class
28+
// TASK 3: Step 1 - Add properties for BankAccountNumber, BankAccountType, Balance, AccountHolderDetails, and a Transactions list.
29+
30+
31+
32+
// TASK 3: Step 2 - Add a constructor to initialize the properties.
33+
34+
35+
// TASK 3: Step 3 - Add a method for deposits/withdrawals that updates the balance and records the transaction.
36+
37+
38+
// TASK 3: Step 4 - Add a method to display account information.
39+
40+
41+
// TASK 3: Step 5 - Add a method to display transactions.
42+
43+
}

DownloadableCodeProjects/LP4_manage-app-data/Data_M3/Starter/Solution.csproj renamed to DownloadableCodeProjects/LP4_manage-app-data/Data_M3/Starter/Data_M3.csproj

File renamed without changes.

0 commit comments

Comments
 (0)