-
Notifications
You must be signed in to change notification settings - Fork 0
/
BankManager.cs
303 lines (266 loc) · 10.2 KB
/
BankManager.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
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Collections;
using System.Threading.Tasks;
namespace _422BankApplicationSharp
{
class BankManager
{
//constructors
/// <summary>
/// Zero argument constructor, defaults number of accounts to 1024
/// </summary>
public BankManager()
{
mAccounts = new List<Account>();
mAccountsAvailable = new List<bool>();
for (int i = 0; i < 1024; i++)
{
mAccountsAvailable.Add(true);
}
mNumberAccounts = 1024;
}
/// <summary>
/// Bankmanager constructor that takes a single argument for the maximum number of accounts
/// </summary>
/// <param name="maxNumberAccounts"></param>
public BankManager(int maxNumberAccounts)
{
mAccounts = new List<Account>();
mAccountsAvailable = new List<bool>();
for (int i = 0; i < maxNumberAccounts; i++)
{
mAccountsAvailable.Add(true);
}
mNumberAccounts = maxNumberAccounts;
}
//Methods
/// <summary>
/// Displays the main menu for the manager class
/// </summary>
public void displayMenu()
{
Console.WriteLine(" 1. Create Account");
Console.WriteLine(" 2. Delete Account");
Console.WriteLine(" 3. Update Account");
Console.WriteLine(" 4. Display Account");
Console.WriteLine(" 5. Exit");
}
/// <summary>
/// Gets a menu option from the user and returns it as an integer
/// </summary>
/// <returns>Menue option as int</returns>
public int getMenuOption()
{
int option = 0;
option = Convert.ToChar(Console.ReadLine());
Console.Clear();
return option;
}
/// <summary>
/// Creates an account, prompting the user for all relevent information
/// </summary>
/// <returns>Returns true on success, false on failure</returns>
public bool createAccount()
{
int accountNumber = 0;
double balance = 0.0;
string name;
string dateCreated;
bool success = true;
Console.WriteLine("Enter account number: ");
string temp = Console.ReadLine();
accountNumber = Convert.ToInt16(temp);
Console.WriteLine("Enter name: ");
name = Console.ReadLine();
Console.WriteLine("Enter Balance: ");
temp = Console.ReadLine();
balance = Convert.ToDouble(temp);
Console.WriteLine("Enter Date: ");
dateCreated = Console.ReadLine();
int counter = 0;
while ((counter < mNumberAccounts) && (mAccountsAvailable.ElementAt(counter) != true))
{
//search for an empty spot in the list
counter ++;
}
if (mNumberAccounts<=counter)
{
Console.WriteLine("WARNING: Could not create account!");
success = false;
}
else
{
try
{
mAccounts.RemoveAt(counter);
}
catch { }
mAccounts.Insert(counter,(new Account(balance,accountNumber,name,dateCreated)));
mAccountsAvailable.RemoveAt(counter);
mAccountsAvailable.Insert(counter,false);
}
return success;
}
/// <summary>
/// Deletes an account. Uses findAccount to prompt the user for an account number to delete
/// </summary>
/// <returns>Returns true on Success, false on failure</returns>
public bool deleteAccount()
{
bool success = false;
int accountNumber;
int counter = 0;
counter = findAccount();
if ((counter < mNumberAccounts) && (mAccountsAvailable.ElementAt(counter) == false))
{
success = true;
mAccountsAvailable.RemoveAt(counter);
mAccountsAvailable.Insert(counter, true);
Console.WriteLine("Account Deleted!");
}
return success;
}
/// <summary>
/// Allows the user to update an account, with some constraints, they are prompted to either change the account name, debit the
/// account, or credit it.
/// </summary>
/// <returns>Returns true on success, false on failure</returns>
public bool updateAccount()
{
bool success = false;
string name = "", dateCreated = "";
double balance = 0.0, deposit = 0.0;
int accountNumber = 0, counter = 0, option = 0;
counter = findAccount();
if ((counter < mNumberAccounts) && (mAccountsAvailable.ElementAt(counter) == false))
{
success = true;
Console.WriteLine("1. Update Name");
Console.WriteLine("2. Withdraw Money");
Console.WriteLine("3. Deposit Money");
option = Convert.ToChar(Console.ReadLine());
switch (option - 48)
{
case 1: Console.WriteLine("Enter Name: ");
name = Console.ReadLine();
mAccounts.ElementAt(counter).MName = name;
break;
case 2: Console.WriteLine("Enter amount to withdraw: ");
balance = Convert.ToDouble(Console.ReadLine());
mAccounts.ElementAt(counter).debit(balance);
break;
case 3: Console.WriteLine("Enter amount to deposit: ");
balance = Convert.ToDouble(Console.ReadLine());
mAccounts.ElementAt(counter).credit(balance);
break;
default: Console.WriteLine("ERROR: Invalid option");
break;
}
}
return success;
}
/// <summary>
/// Displays the account specified by the user, if it exists
/// </summary>
public void displayAccount()
{
int counter = 0;
counter = findAccount();
if ((counter < mNumberAccounts) && (mAccountsAvailable.ElementAt(counter) == false))
{
mAccounts.ElementAt(counter).printBalance();
}
else
{
Console.WriteLine("WARNING: Account does not exist");
}
}
/// <summary>
/// Prompts the user for an account number, and then finds the index of the matching account, if it exists
/// </summary>
/// <returns>The index of the specified account on success, the number of elements in the array on failure</returns>
public int findAccount()
{
int accountnumber = 0;
int counter = 0;
Console.WriteLine("Enter Account Number");
accountnumber = Convert.ToInt16(Console.ReadLine());
for (counter = 0; ((counter!=mAccounts.Count)&&(counter < mNumberAccounts) && (mAccounts.ElementAt(counter).MAccountNumber != accountnumber)); counter++)
{
}
return counter;
}
/// <summary>
/// Runs the bank application as the user does not select quit.
/// </summary>
public void runBankApplication()
{
int option = 0;
bool status = true, success = true;
Console.WriteLine("***Welcome to Alex and Young's User Friendly Banking system ***");
do
{
displayMenu();
option = getMenuOption();
switch (option - 48)
{
case 1: success = createAccount();
if (success == false)
{
Console.WriteLine("WARNING: Could not create account!");
}
break;
case 2: success = deleteAccount();
if (success == false)
{
Console.WriteLine("WARNING: Could not delete account!");
}
break;
case 3:
success = updateAccount();
if (success == false)
{
Console.WriteLine("WARNING: Could not update account!");
}
break;
case 4:
displayAccount();
break;
case 5:
status = false;
break;
default:
Console.WriteLine("ERROR: Invalid Choice");
break;
}
} while (status != false);
}
//private variables
//The list of accounts
List<Account> mAccounts; //The arrays have been replaced with dymically sized lists, because why not use the features of C# if we are converting to it.
//getters and setters for the above
internal List<Account> MAccounts
{
get { return mAccounts; }
set { mAccounts = value; }
}
//A list designating whether open slots exist in the list for new accounts
List<bool> mAccountsAvailable;
//Getters and setters for the above
public List<bool> MAccountsAvailable
{
get { return mAccountsAvailable; }
set { mAccountsAvailable = value; }
}
//The maximum number of accounts available.
int mNumberAccounts;
public int MNumberAccounts
{
get { return mNumberAccounts; }
set { mNumberAccounts = value; }
}
}
}