-
Notifications
You must be signed in to change notification settings - Fork 0
/
bank
162 lines (137 loc) · 4.57 KB
/
bank
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
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
using System;
using System.Collections.Generic;
namespace BankAccountDemo
{
public class Program
{
static void Main(string[] args)
{
var bank = new Bank();
bank.Run();
}
}
public class Bank
{
private List<Account> accounts = new List<Account>();
private bool running = true;
public void Run()
{
Console.WriteLine("Welcome to the Simple Bank System!");
while (running)
{
Console.WriteLine("\nPlease choose an option:");
Console.WriteLine("1. Create a new account");
Console.WriteLine("2. Deposit");
Console.WriteLine("3. Withdraw");
Console.WriteLine("4. Print all accounts");
Console.WriteLine("5. Exit");
switch (Console.ReadLine())
{
case "1":
CreateAccount();
break;
case "2":
MakeDeposit();
break;
case "3":
MakeWithdrawal();
break;
case "4":
PrintAccounts();
break;
case "5":
running = false;
break;
default:
Console.WriteLine("Invalid option, try again.");
break;
}
}
Console.WriteLine("Thank you for using the Simple Bank System. Goodbye!");
}
private void CreateAccount()
{
Console.Write("Enter the account holder's name: ");
string name = Console.ReadLine();
Console.Write("Enter the initial balance: ");
decimal initialBalance = decimal.Parse(Console.ReadLine());
var account = new Account(name, initialBalance);
accounts.Add(account);
Console.WriteLine($"Account for {name} created with account number {account.Number} and balance {account.Balance:C}.");
}
private void MakeDeposit()
{
var account = FindAccount();
if (account == null) return;
Console.Write("Enter the amount to deposit: ");
decimal amount = decimal.Parse(Console.ReadLine());
account.Deposit(amount);
Console.WriteLine($"Deposit successful. New balance is {account.Balance:C}.");
}
private void MakeWithdrawal()
{
var account = FindAccount();
if (account == null) return;
Console.Write("Enter the amount to withdraw: ");
decimal amount = decimal.Parse(Console.ReadLine());
bool success = account.Withdraw(amount);
if (success)
{
Console.WriteLine($"Withdrawal successful. New balance is {account.Balance:C}.");
}
else
{
Console.WriteLine("Withdrawal failed. Insufficient funds.");
}
}
private void PrintAccounts()
{
Console.WriteLine("Account List:");
foreach (var account in accounts)
{
Console.WriteLine(account);
}
}
private Account FindAccount()
{
Console.Write("Enter the account number: ");
int accountNumber = int.Parse(Console.ReadLine());
var account = accounts.Find(a => a.Number == accountNumber);
if (account == null)
{
Console.WriteLine("Account not found.");
}
return account;
}
}
public class Account
{
private static int accountNumberSeed = 1234567890;
public string Name { get; }
public decimal Balance { get; private set; }
public int Number { get; }
public Account(string name, decimal initialBalance)
{
Name = name;
Balance = initialBalance;
Number = accountNumberSeed++;
}
public void Deposit(decimal amount)
{
Balance += amount;
}
public bool Withdraw(decimal amount)
{
if (amount <= Balance)
{
Balance -= amount;
return true;
}
return false;
}
public override string ToString()
{
return $"Account {Number} - {Name}, Balance: {Balance:C}";
}
}
}