-
Notifications
You must be signed in to change notification settings - Fork 101
Expand file tree
/
Copy pathProgram.cs
More file actions
63 lines (53 loc) · 2.62 KB
/
Program.cs
File metadata and controls
63 lines (53 loc) · 2.62 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
using System;
namespace FactoryPattern
{
/// <summary>
/// Client application demonstrating the Factory Pattern usage
/// Notice how the client code is completely decoupled from concrete implementations
/// </summary>
public class BankingApplication
{
public static void Main(string[] args)
{
Console.WriteLine("=== Banking System - Factory Pattern Demo ===\n");
try
{
// Create different types of accounts using the factory
IAccount savingsAccount = AccountFactory.CreateAccount(AccountType.Savings);
IAccount checkingAccount = AccountFactory.CreateAccount(AccountType.Checking);
IAccount businessAccount = AccountFactory.CreateAccount(AccountType.BusinessChecking);
// Alternative: Create account using string input (useful for user interfaces)
IAccount userAccount = AccountFactory.CreateAccount("Savings");
// Use the accounts without knowing their concrete types
DisplayAccountInfo(savingsAccount);
DisplayAccountInfo(checkingAccount);
DisplayAccountInfo(businessAccount);
// Perform operations
Console.WriteLine("\n=== Account Operations ===");
Console.WriteLine(savingsAccount.Deposit(1000));
Console.WriteLine(savingsAccount.Withdraw(250));
Console.WriteLine(checkingAccount.Deposit(500));
Console.WriteLine(checkingAccount.Withdraw(600)); // This might overdraft
Console.WriteLine(businessAccount.Deposit(2000));
Console.WriteLine(businessAccount.Withdraw(500)); // This will include fees
}
catch (ArgumentException ex)
{
Console.WriteLine($"Error: {ex.Message}");
}
Console.WriteLine("\nPress any key to exit...");
Console.ReadKey();
}
/// <summary>
/// Helper method to display account information
/// Demonstrates working with objects through their interface
/// </summary>
/// <param name="account">Account instance to display information for</param>
private static void DisplayAccountInfo(IAccount account)
{
Console.WriteLine($"Account Type: {account.GetAccountType()}");
Console.WriteLine($"Interest Rate: {account.GetInterestRate()}%");
Console.WriteLine();
}
}
}