-
Notifications
You must be signed in to change notification settings - Fork 0
/
Bank.cs
162 lines (148 loc) · 5.34 KB
/
Bank.cs
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 BankConsole
{
public class Bank
{
public string Name { get; set; }
public List<BankAccount> Accounts { get; set; } = new List<BankAccount>();
public void AddAccount()
{
var firstName = Prompt("First Name");
var lastName = Prompt("Last Name");
var street = Prompt("Street");
var apt = Prompt("Apt");
var city = Prompt("City");
var state = Prompt("State");
var zip = Prompt("ZIP");
var initialDeposit = Decimal.Parse(Prompt("Initial Deposit"));
var newAccount = new BankAccount
{
Owner = new Person
{
FirstName = firstName,
LastName = lastName,
Address = new Address
{
Street = street,
Apt = apt,
City = city,
State = state,
Zip = zip
}
}
};
newAccount.Transactions.Add(new Transaction
{
Description = "Initial Deposit",
Amount = initialDeposit
});
Accounts.Add(newAccount);
Console.WriteLine($"Account number {newAccount.AccountNumber} for {newAccount.Owner.FullName} added!");
Console.Write("Press any key to continue...");
Console.ReadLine();
}
public void ListAllAccounts()
{
foreach (BankAccount acct in Accounts)
{
Console.WriteLine($"Name: {acct.Owner.FullName}, Number: {acct.AccountNumber}, Balance: {acct.Balance}");
}
Console.Write("Press any key to continue...");
Console.ReadLine();
}
public void ListAccount()
{
Console.Write("Input Account Number or Name: ");
var input = Console.ReadLine();
try
{
var intResponse = int.Parse(input);
ListSingleAccount(intResponse);
}
catch
{
ListSingleAccount(input);
}
Console.Write("Press any key to continue...");
Console.ReadLine();
}
public void AddTransaction()
{
var payeeAcct = Prompt("Payee Account Number");
var payorAcct = Prompt("Payor Account Number");
var description = Prompt("Description");
var amount = Decimal.Parse(Prompt("Amount"));
var payee = Accounts.Find(acct => acct.AccountNumber == int.Parse(payeeAcct));
var payor = Accounts.Find(acct => acct.AccountNumber == int.Parse(payorAcct));
if (amount > payor.Balance)
{
Console.WriteLine("Payor does not have sufficient funds for transaction");
}
else
{
var payeeTrans = new Transaction
{
Description = description,
Amount = amount
};
payee.Transactions.Add(payeeTrans);
var payorTrans = new Transaction
{
Description = description,
Amount = -amount
};
payor.Transactions.Add(payorTrans);
Console.WriteLine($"{payor.Owner.FullName} paid {payee.Owner.FullName}");
Console.WriteLine($"{amount} for {description}");
Console.WriteLine("New Balances:");
Console.WriteLine("-------------------------------");
ListSingleAccount(payee.AccountNumber);
ListSingleAccount(payor.AccountNumber);
}
Console.Write("Press any key to continue...");
Console.ReadLine();
}
public void PrintMailingLabels()
{
foreach (BankAccount acct in Accounts)
{
Console.WriteLine(acct.Owner.AddressWithName);
Console.WriteLine();
}
Console.Write("Press any key to continue...");
Console.ReadLine();
}
private void ListSingleAccount(int acctNumber)
{
var acct = Accounts.Find(acct => acct.AccountNumber == acctNumber);
if (acct != null)
{
Console.WriteLine($"Name: {acct.Owner.FullName}, Number: {acct.AccountNumber}, Balance: {acct.Balance}");
}
else
{
Console.WriteLine("Account not found!!!");
}
}
private void ListSingleAccount(string name)
{
var acct = Accounts.Find(acct => acct.Owner.FullName.ToLower().Contains(name.ToLower()));
if (acct != null)
{
Console.WriteLine($"Name: {acct.Owner.FullName}, Number: {acct.AccountNumber}, Balance: {acct.Balance}");
}
else
{
Console.WriteLine("Account not found!!!");
}
}
private string Prompt(string prompt)
{
Console.Write($"{prompt}: ");
string response = Console.ReadLine();
Console.WriteLine();
return response;
}
}
}