-
Notifications
You must be signed in to change notification settings - Fork 0
/
JollyBanker.cpp
executable file
·183 lines (176 loc) · 5 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
#include "JollyBanker.h"
#include <fstream>
#include <iostream>
#include <sstream>
JollyBanker::JollyBanker()
{
}
JollyBanker::~JollyBanker()
{
}
void JollyBanker::ReadFile(string name)
{//this will create the queue
ifstream infile(name);
string line;
while (getline(infile, line))
{
istringstream theLine(line);
char tranCode;
theLine >> tranCode;
if (tranCode == 'O')
{
string last, first;
int id;
theLine >> last >> first >> id;
Transactions temp(tranCode, last, first, id);
transactions.push(temp);
}
else if (tranCode == 'D' || tranCode=='W')
{
int id, amount;
theLine >> id >> amount;
Transactions temp(tranCode, id, amount);
transactions.push(temp);
}
else if (tranCode == 'T')
{
int id, amount, transferToID;
theLine >> id >> amount >> transferToID;
Transactions temp(tranCode, id, amount, transferToID);
transactions.push(temp);
}
else if (tranCode == 'H')
{
int id;
theLine >> id;
Transactions temp(tranCode, id);
transactions.push(temp);
}
else
{
cout << "Transaction Code not found!" << endl;
}
}
}
void JollyBanker::ProcessTransaction()
{
while (!transactions.empty())
{
Transactions temp = transactions.front();
if (temp.getTransactionType() == 'O')
{
Account *account = new Account(temp.getFirstName(), temp.getLastName(), temp.getAccountNumber());
if (!accountsList.Insert(account))
{
cerr << "ERROR: Account: " << temp.getAccountNumber() << " is already open. Transferal refused." << endl;
}
}
else if (temp.getTransactionType() == 'D')
{
int FundID = temp.getAccountNumber() % 10;
int accID = temp.getAccountNumber() / 10;
Account *acc;
accountsList.Retrieve(accID, acc);
acc->addFunds(temp.getAmount(), FundID);
acc->conductedTransaction(temp, FundID);//add transaction
}
else if (temp.getTransactionType() == 'W')
{
int FundID = temp.getAccountNumber() % 10;
int accID = temp.getAccountNumber() / 10;
Account *acc;
accountsList.Retrieve(accID, acc);
if (acc->subFunds(temp.getAmount(), FundID))
{
temp.setTransactionSuccess(true);
}
else // not enough money transaction success is set to fail
{
temp.setTransactionSuccess(false);
cerr << "ERROR: Not enough funds to withdraw " << temp.getAmount() << " from " << temp.getFirstName()<< " " << temp.getLastName()<<
" " << acc->getFundName(FundID);
}
if (!acc->getIncertCheck())
{
acc->conductedTransaction(temp, FundID);
}
}
else if (temp.getTransactionType() == 'T')
{
int fromFundID = temp.getAccountNumber() % 10;
int fromAccID = temp.getAccountNumber() / 10;
int toFundID = temp.getTransferToAccount() % 10;
int toAccID = temp.getTransferToAccount() / 10;
int amount = temp.getAmount();
Account * from, *to;
accountsList.Retrieve(fromAccID, from);
accountsList.Retrieve(toAccID, to);
if (accountsList.Retrieve(fromAccID, from))
{
if (accountsList.Retrieve(toAccID, to))
{
if (from->subFunds(amount, fromFundID))
{
to->addFunds(amount, toFundID);
to->conductedTransaction(temp, toFundID);
from->conductedTransaction(temp, fromFundID);
}
else
{
cerr << "ERROR: Unsufficent Funds to conduct transfer. From account number: " <<
from->getAccountNum() << " account: " << from->getFundName(fromFundID) << " to account number: " <<
to->getAccountNum() << " account: " << to->getFundName(toFundID) << " from the amount of: "
<< temp.getAmount() << endl;
}
}
else
{
cerr << "ERROR: Account: " << toAccID << " not found. Transderal refused" << endl;
}
}
else
{
cerr << "ERROR: Account: " << fromAccID << " not found. Transderal refused" << endl;
}
}
else if (temp.getTransactionType() == 'H')
{
if (temp.getAccountNumber() >= 10000 && temp.getAccountNumber() <= 99999)
{
int fundID = temp.getAccountNumber() % 10;
int accID = temp.getAccountNumber() / 10;
Account *account;
if (accountsList.Retrieve(accID, account))
{
cout << "Transaction History For: " << account->getFirstName() << " "
<< account->getLastName() << " " << endl;
account->printFundHistory(fundID);
}
else
{
cerr << "ERROR: Account " << accID << " not found." << endl;
}
}
else if(temp.getAccountNumber() >= 1000 && temp.getAccountNumber() <= 9999)
{
Account * account;
if (accountsList.Retrieve(temp.getAccountNumber(), account))
{
cout << "Transaction History For: " << account->getFirstName() << " "
<< account->getLastName() << " " << endl;
account->printHistoryAll();
}
else
{
cerr << "ERROR: Account " << temp.getAccountNumber() << " not found." << endl;
}
}
}
transactions.pop();
}
}
void JollyBanker::PrintResults()
{
cout << "\nProcessing Done. Final Balances" << endl;
accountsList.Display();
}