-
Notifications
You must be signed in to change notification settings - Fork 4
/
JollyBanker.cpp
187 lines (173 loc) · 5.53 KB
/
JollyBanker.cpp
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
//Project 5: Jolly Banker
//Author: Brent Stone
//CSS 342
#include "stdafx.h"
#include "JollyBanker.h"
#include "Transaction.h"
#include "Account.h"
#include <fstream>
#include <iostream>
#include <sstream>
#include <queue>
JollyBanker::JollyBanker()
{
}
JollyBanker::~JollyBanker()
{
}
//The ReadTransaction fucntion, reads the input file and creates the
//appropriate transaction and places them into the queue.
bool JollyBanker::ReadTransactions()
{
ifstream in("BankTransIn.txt");
string readLine;
if (!in)
{
cout << "Cannot Open!" << endl;
}
while (!in.eof())
{
getline(in, readLine);
if (readLine.empty())
{
break;
}
istringstream parseLine(readLine);
char transactionType;
parseLine >> transactionType;
if (transactionType == 'O' || transactionType == 'o')
{
string lastName, firstName;
int accountID;
parseLine >> firstName >> lastName >> accountID;
Transaction addToQueue(transactionType, firstName, lastName, accountID);
transactionList.push(addToQueue);
}
else if (transactionType == 'W' || transactionType == 'w' || transactionType == 'D' || transactionType == 'd')
{
int accountID, fundID, amount;
parseLine >> accountID >> amount;
fundID = accountID % 10;
accountID = accountID / 10;
Transaction addToQueue(transactionType, accountID, fundID, amount);
transactionList.push(addToQueue);
}
else if (transactionType == 'T'|| transactionType == 't')
{
int accountID, fundID, amount, trasnferAccountID, transferFundID;
parseLine >> accountID >> amount >> trasnferAccountID;
fundID = accountID % 10;
accountID = accountID / 10;
transferFundID = trasnferAccountID % 10;
trasnferAccountID = trasnferAccountID / 10;
Transaction addToQueue(transactionType, accountID, fundID, amount, trasnferAccountID, transferFundID);
transactionList.push(addToQueue);
}
else if(transactionType == 'H' || transactionType == 'h')
{
int accountID, fundID;
int integerTest;
int length = 1;
parseLine >> accountID;
Transaction addToQueue(transactionType, accountID);
transactionList.push(addToQueue);
}
else
{
cout << "ERROR!" << endl;
}
}
return false;
}
//THe ExeTransaction pops off transactions from the queue. The first character of the transaction
//dictates the action taken on the it. Each section of the if/then statements generates an
//Account pointer which is retrived from the Binary Seach Tree and manipulates the data.
void JollyBanker::ExeTransactions()
{
while (!transactionList.empty())
{
Transaction frontTrans = transactionList.front();
if (frontTrans.getTransactionType() == 'O' || frontTrans.getTransactionType() == 'o')
{
Account * account = new Account(frontTrans.getFirstName(), frontTrans.getLastName(), frontTrans.getAccountID());
accountList.Insert(account);
}
else if (frontTrans.getTransactionType() == 'D' || frontTrans.getTransactionType() == 'd')
{
int accountNumber = frontTrans.getAccountID();
int fundNumber = frontTrans.getFundID();
int amountToAdd = frontTrans.getAmount();
Account *account;
if (accountList.Retrieve(accountNumber, account))
{
account->AddToAccount(fundNumber, amountToAdd);
account->RecordTrans(frontTrans, fundNumber);
}
}
else if (frontTrans.getTransactionType() == 'W' || frontTrans.getTransactionType() == 'w')
{
int accountNumber = frontTrans.getAccountID();
int fundNumber = frontTrans.getFundID();
int amountToSubtract = frontTrans.getAmount();
Account *account;
if (accountList.Retrieve(accountNumber, account))
{
account->MinusFunds(fundNumber, amountToSubtract, frontTrans);
}
}
else if (frontTrans.getTransactionType() == 'T' || frontTrans.getTransactionType() == 'T')
{
int accountNumber = frontTrans.getAccountID();
int transferAcctNum = frontTrans.getTransferAccountID();
int fundNumber = frontTrans.getFundID();
int transferFundNum = frontTrans.getTransferFundID();
int amountToSub = frontTrans.getAmount();
Account *to, *from;
if(accountList.Retrieve(accountNumber, to) && accountList.Retrieve(transferAcctNum, from))
{
if (to->MinusFunds(fundNumber, amountToSub, frontTrans))
{
from->AddToAccount(transferFundNum, amountToSub);
from->RecordTrans(frontTrans, transferFundNum);
}
else
{
cerr << "ERROR: Not Enought Funds to Transer " << amountToSub << " " << "from " << from->getAccountID() << fundNumber << " to " << to->getAccountID() << transferFundNum << endl;
Transaction addToHistory('T', accountNumber, fundNumber, amountToSub, transferAcctNum, transferFundNum, "(Failed)");
from->RecordTrans(addToHistory, transferFundNum);
}
}
}
else if (frontTrans.getTransactionType() == 'H' || frontTrans.getTransactionType() == 'h')
{
if(frontTrans.getAccountID() >= 10000 && frontTrans.getAccountID() <= 99999)//5 digits = account number + fund
{
Account *account;
int fundNumPrt = frontTrans.getAccountID() % 10;
int acctNumSearch = frontTrans.getAccountID() / 10;
if (accountList.Retrieve(acctNumSearch, account))
{
account->PrintFundHistory(fundNumPrt);
cout << endl;
}
}
else if(frontTrans.getAccountID() >= 1000 && frontTrans.getAccountID() <= 9999)//4 digits = account number
{
Account *account;
int acctNumSearch = frontTrans.getAccountID();
if (accountList.Retrieve(acctNumSearch, account))
{
account->PrintHistory();
cout << endl;
}
}
}
transactionList.pop();
}
}
void JollyBanker::Display()
{
cout << endl;
cout << "Processing Done. Final Balances" << endl;
accountList.Display();
}