forked from jacobjhicks/BuildingClassCIT241
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathCustomerListType.cpp
245 lines (215 loc) · 5.18 KB
/
CustomerListType.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
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
#include "CustomerListType.h"
CustomerListType::CustomerListType()
{
vector<string> input;
ifstream inDat("CustData.txt");
if (!inDat)
{
cout << "CustData.txt Not Found" << endl;
}
else
{
while (!inDat.eof())
{
string a;
getline(inDat, a, '\n');
input.push_back(a);
}
}
inDat.close();
CustomerType CustHolder;
Order OrderHolder;
for (auto b = input.begin(); b != input.end(); ++b)
{
string j;
istringstream iss{ *b };
getline(iss, j, '|');
if (j == "C")
{
customers.push_back(CustHolder);
string f, g, h;
getline(iss, f, '|');
getline(iss, g, '|');
getline(iss, h, '|');
CustomerType hold(f, g, h);
CustHolder = hold;
}
if (j == "O")
{
string f, g, h;
if (OrderHolder.getOrderID() == "")
{
}
else
{
CustHolder.addOrder(OrderHolder);
}
getline(iss, f, '|');
getline(iss, g, '|');
getline(iss, h, '|');
Date y(g);
Date z(h);
Order hold(f, y, z);
OrderHolder = hold;
}
if (j == "I")
{
string f, g, i, k;
int y;
double z;
getline(iss, f, '|');
getline(iss, g, '|');
getline(iss, i, '|');
getline(iss, k, '|');
try
{
y = stoi(i);
z = stod(k);
}
catch (exception)
{
system("CLS");
cerr << "Error: Exception " << current_exception << " thrown." << endl
<< "Invalid input for OrderItem object." << endl
<< "Exiting Program" << endl;
system("pause");
exit(1);
}
orderItem hold(f, g, y, z);
OrderHolder.addOrderItem(hold);
}
}
CustHolder.addOrder(OrderHolder);
customers.push_back(CustHolder);
}
CustomerListType::~CustomerListType()
{
}
void CustomerListType::addCustomer(const CustomerType customer)
{
customers.push_back(customer);
}
// Not needed?
//list<CustomerType>::iterator CustomerListType::findCustomerObject(const string & custId)
//{
// list<CustomerType>::iterator found = find_if(customers.begin(), customers.end(), [&custId](CustomerType customer) {
// return customer.getId() == custId;
// });
//
// if (found != customers.end())
// return found;
// throw exception("Customer not found.");
//}
/**
* Returns the CustomerType object with the given ID from the list of customers.
* Throws an error if the custId is not found
*/
CustomerType & CustomerListType::getCustomer(const string & custId)
{
auto found = find_if(customers.begin(), customers.end(), [&custId](CustomerType customer) {
return customer.getId() == custId;
});
if (found != customers.end())
return *found;
throw exception("Customer not found.");
}
/**
* Returns true if a customer with the given ID is found.
*/
bool CustomerListType::findCustomer(const string & custId)
{
auto found = find_if(customers.begin(), customers.end(), [&custId](CustomerType customer) {
return customer.getId() == custId;
});
return found != customers.end();
}
void CustomerListType::removeCustomer( CustomerType customer)
{
//customers.remove(*customer);
list<CustomerType>::iterator rmIt;
for (rmIt = customers.begin(); rmIt != customers.end(); ++rmIt)
{
if (customer == *rmIt)
{
customers.erase(rmIt);
break;
}
}
}
string CustomerListType::getOrders(CustomerType customer)
{
list<CustomerType>::iterator rmIt;
string out;
for (rmIt = customers.begin(); rmIt != customers.end(); ++rmIt)
{
if (customer == *rmIt)
{
out = rmIt->printOrders();
break;
}
}
return out;
}
void CustomerListType::addOrder(CustomerType customer, const Order & order)
{
list<CustomerType>::iterator rmIt;
for (rmIt = customers.begin(); rmIt != customers.end(); ++rmIt)
{
if (customer == *rmIt)
{
rmIt->addOrder(order);
break;
}
}
}
/**
* Returns an iterator to the first customer.
*/
list<CustomerType>::iterator CustomerListType::begin()
{
return customers.begin();
}
/**
* Returns an iterator just past the last customer
*/
list<CustomerType>::iterator CustomerListType::end()
{
return customers.end();
}
/**
* Returns a string object detailing each customer.
* Example:
* "CustId Name Email "
* "--------------------------------------------------------------------------------"
* "OK123 Full Name [email protected] "
* "... "
*/
string CustomerListType::print()
{
stringstream out;
out << left << setw(12) << "CustId";
out << left << setw(34) << "Name";
out << left << setw(34) << "Email";
out << endl;
out << left << setfill('-') << setw(80) << "" << endl;
for_each(customers.begin(), customers.end(), [&out](CustomerType customer) {
out << customer << endl;
});
return out.str();
}
/**
* Prints the details of each customer.
* Example:
* "CustId Name Email "
* "--------------------------------------------------------------------------------"
* "OK123 Full Name [email protected] "
* "... "
*/
ostream & operator<<(ostream & out, CustomerListType & rightConstmerList)
{
for (CustomerType customer : rightConstmerList.customers)
{
out << customer << endl;
}
return out;
}