-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathNetwork.cpp
182 lines (171 loc) · 5.6 KB
/
Network.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
#include <vector>
#include <sstream>
#include "Network.h"
#include "Peer.h"
#include "Cash.h"
#include "CreditCard.h"
#include "Check.h"
#include "Transaction.h"
Network::Network()
{
numberOfPeers = 0;
peer1 = new Peer(1, "Lyle School of Engineering");
peer2 = new Peer(2, "Cox School of Business");
peer3 = new Peer(3, "Dedman School of Law");
}
void Network::processTransactions(std::ifstream &inputFile)
{
string line, transactionType;
int index = 0;
vector<string> fullTransaction, transactionInfo;
while(getline(inputFile, line, '\n')) //parses each transaction to be on its own
{
fullTransaction.push_back(line);
}
for(int i = 0; i < fullTransaction.size(); i++) //parses each transaction into individual pieces
{
stringstream parseTransactions(fullTransaction.at(i));
while(getline(parseTransactions, line, ' '))
{
transactionInfo.push_back(line);
}
}
for(int i = 0; i < fullTransaction.size(); i++)
{
transactionType = transactionInfo.at(index); //determines the type of transaction and creates that transaction pointer then adds it to the specific peer
if(transactionType == "Credit")
{
CreditCard *toAdd = new CreditCard(rand(), stoi(transactionInfo.at(index + 1)), stoi(transactionInfo.at(index + 2)),
stod(transactionInfo.at(index + 3)), transactionInfo.at(index + 4),
transactionInfo.at(index + 5), transactionInfo.at(index + 6));
copyCreditTransactions(index, transactionInfo, toAdd);
index += 7;
}
else if(transactionType == "Cash")
{
Cash *toAdd = new Cash(rand(), stoi(transactionInfo.at(index + 1)), stoi(transactionInfo.at(index + 2)),
stod(transactionInfo.at(index + 3)), stod(transactionInfo.at(index + 4)),
stod(transactionInfo.at(index + 5)));
copyCashTransactions(index, transactionInfo, toAdd);
index += 6;
}
else if(transactionType == "Check")
{
Check *toAdd = new Check(rand(), stoi(transactionInfo.at(index + 1)), stoi(transactionInfo.at(index + 2)),
stod(transactionInfo.at(index + 3)), transactionInfo.at(index + 4),
transactionInfo.at(index + 5));
copyCheckTransactions(index, transactionInfo, toAdd);
index += 6;
}
}
peers.insert(peer1); //insert each peer into the peers linked list
peers.insert(peer2);
peers.insert(peer3);
}
void Network::copyCreditTransactions(int index, vector<string> transactionInfo, CreditCard *toAdd)
{
CreditCard *copyToAdd;
copyToAdd = new CreditCard(*toAdd);
//finds which peer to add the transaction to
if(stoi(transactionInfo.at(index + 1)) == 1)
{
peer1->addTransaction(toAdd);
}
else if(stoi(transactionInfo.at(index + 1)) == 2)
{
peer2->addTransaction(toAdd);
}
else if(stoi(transactionInfo.at(index + 1)) == 3)
{
peer3->addTransaction(toAdd);
}
//finds which peer to add the copy of the transaction to
if(stoi(transactionInfo.at(index + 2)) == 1)
{
peer1->addTransaction(copyToAdd);
}
else if(stoi(transactionInfo.at(index + 2)) == 2)
{
peer2->addTransaction(copyToAdd);
}
else if(stoi(transactionInfo.at(index + 2)) == 3)
{
peer3->addTransaction(copyToAdd);
}
}
void Network::copyCashTransactions(int index, vector<string> transactionInfo, Cash *toAdd)
{
Cash *copyToAdd;
copyToAdd = new Cash(*toAdd);
//finds which peer to add the transaction to
if(stoi(transactionInfo.at(index + 1)) == 1)
{
peer1->addTransaction(toAdd);
}
else if(stoi(transactionInfo.at(index + 1)) == 2)
{
peer2->addTransaction(toAdd);
}
else if(stoi(transactionInfo.at(index + 1)) == 3)
{
peer3->addTransaction(toAdd);
}
//finds which peer to add the copy of the transaction to
if(stoi(transactionInfo.at(index + 2)) == 1)
{
peer1->addTransaction(copyToAdd);
}
else if(stoi(transactionInfo.at(index + 2)) == 2)
{
peer2->addTransaction(copyToAdd);
}
else if(stoi(transactionInfo.at(index + 2)) == 3)
{
peer3->addTransaction(copyToAdd);
}
}
void Network::copyCheckTransactions(int index, vector<string> transactionInfo, Check *toAdd)
{
Check *copyToAdd;
copyToAdd = new Check(*toAdd);
//finds which peer to add the transaction to
if(stoi(transactionInfo.at(index + 1)) == 1)
{
peer1->addTransaction(toAdd);
}
else if(stoi(transactionInfo.at(index + 1)) == 2)
{
peer2->addTransaction(toAdd);
}
else if(stoi(transactionInfo.at(index + 1)) == 3)
{
peer3->addTransaction(toAdd);
}
//finds which peer to add the copy of the transaction to
if(stoi(transactionInfo.at(index + 2)) == 1)
{
peer1->addTransaction(copyToAdd);
}
else if(stoi(transactionInfo.at(index + 2)) == 2)
{
peer2->addTransaction(copyToAdd);
}
else if(stoi(transactionInfo.at(index + 2)) == 3)
{
peer3->addTransaction(copyToAdd);
}
}
void Network::display()
{
for(int i = 0; i < peers.getSize(); i++)
{
cout << "\n-- Peer " << i + 1 << " Ledger --" << endl;
peers.at(i)->display();
}
}
void Network::save(ofstream &peer1File, ofstream &peer2File, ofstream &peer3File)
{
peer1->save(peer1File);
peer2->save(peer2File);
peer3->save(peer3File);
}