- Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathwineProcess.cpp
More file actions
Latest commit
386 lines (276 loc) · 7.51 KB
/
Copy pathwineProcess.cpp
File metadata and controls
386 lines (276 loc) · 7.51 KB
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
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
#include"wineProcess.h"
#include"mainwindow.h"
#include"ui_mainwindow.h"
#include<sstream>
#include<string>
#include<cctype>
#include<iostream>
#include<fstream>
#include<iomanip>
#include<algorithm>
usingnamespacestd;
/**
default constructor for a Wine object. Sets all values to 0.
*/
Wine::Wine()
{
wineName = "";
vintage = 0;
rating = 0;
price = 0;
upc = 0;
location = "";
}
/**
overloaded constructor for a Wine object. Sets wine price to the passed price.
@param setPrice a double used to set the price of a Wine object
*/
Wine::Wine(double setPrice)
{
wineName = "";
vintage = 0;
rating = 0;
price = setPrice;
upc = 0;
location = "";
}
/**
overloaded constructor. Sets all wine parameters.
@param setPrice a double used to set the price of a Wine object
*/
Wine::Wine(string setName,int setVintage,int setRating, double setPrice,
int setWineUpc, string setWineLocation)
{
wineName = setName;
vintage = setVintage;
rating = setRating;
price = setPrice;
upc = setWineUpc;
location = setWineLocation;
}
voidWine::setWineName(string name)
{
wineName = name;
}
string Wine::getWineName()
{
return wineName;
}
voidWine::setWineVintage(int wineVintage)
{
vintage = wineVintage;
}
intWine::getWineVintage()
{
return vintage;
}
voidWine::setWineRating(int wineRating)
{
rating = wineRating;
}
intWine::getWineRating()
{
return rating;
}
voidWine::setWinePrice(double winePrice)
{
price = winePrice;
}
doubleWine::getWinePrice()
{
return price;
}
voidWine::setWineUpc(int wineUpc)
{
upc = wineUpc;
}
intWine::getWineUpc()
{
return upc;
}
voidWine::setWineLocation(string wineLocation)
{
location = wineLocation;
}
string Wine::getWineLocation()
{
return location;
}
/**
Converts a string to a floating-point number, e.g. "3.14" -> 3.14.
@param s a string representing a floating-point number
@return the equivalent floating-point number
*/
doubledataProcessing::string_to_double(string s)
{
istringstream stream;
stream.str(s);
double x = 0;
stream >> x;
return x;
}
/**
Converts a double to a string, e.g. 3.14 -> "3.14".
@param input is a double to be processed
@return string equivalent of the floating-point number
*/
string dataProcessing::double_to_string(double input)
{
stringstream out;
out << input;
return out.str();
}
/**
Processes the lines Delimited by ';' and including 4 fields
@param fileLine is the string to be processed
@param wineName is the name to record from the string
@param vintage is the year to record from the string
@param rating is the rating to record from the string
@param price is the price to record from the string
*/
voiddataProcessing::readProper_line(string fileLine, Wine& wine)
{
int i = 0;
int j = 0;
while (fileLine[i] != ';') { i++; };
wine.setWineName(fileLine.substr(j, i-j));
j=i+2;
i++;
while (fileLine[i] != ';') { i++; };
wine.setWineVintage(string_to_double(fileLine.substr(j, i-j)));
j=i+2;
i++;
while (fileLine[i] != ';') { i++; };
wine.setWineRating(string_to_double(fileLine.substr(j, i-j)));
j=i+2;
i++;
while (fileLine[i] != ';') { i++; };
wine.setWinePrice(string_to_double(fileLine.substr(j, i-j)));
j=i+2;
i++;
while (fileLine[i] != ';') { i++; };
wine.setWineUpc(string_to_double(fileLine.substr(j, i-j)));
j=i+2;
i++;
while (fileLine[i] != ';') { i++; };
wine.setWineLocation(fileLine.substr(j, i-j ));
}
voiddataProcessing::read_line(string fileLine, Wine& wine)
{
int i = 0;
while (fileLine[i] != ';') { i++; }//parsing to get the first field
int j = i + 1;//parsing to get the second field marked
while (isalnum(fileLine[j]) || isspace(fileLine[j])) { j++; }
int k = j + 1;//third field
//because the second field is sometimes missing, work around:
if(string_to_double(fileLine.substr(i + 1, j - 1)) < 1000)
{
wine.setWineVintage(666);//just a random number to substitute into missing field
k = i + 1;
}
else{wine.setWineVintage(string_to_double(fileLine.substr(i + 1, j - 1))); }
while (isalnum(fileLine[k]) || isspace(fileLine[k])) { k++; }
int l = k + 1; //marking the fourth filed
while (isalnum(fileLine[l])) { l++; }
//cutting out the values and pasting them into the object
wine.setWineName(fileLine.substr(0, i - 2));
wine.setWineRating(string_to_double(fileLine.substr(j + 1, k - 1)));
wine.setWinePrice(string_to_double(fileLine.substr(k + 1, l - 1)));
wine.setWineLocation("Canadia");
}
/**
Processes the file by cutting it up and feeding separate lines to the
read_line processor
@param wineList[] structure for wine descriptions/rating/price
*/
voiddataProcessing::processListFile(vector<Wine> &wineList)
{
int lineNum = 0;
ifstream inList;
inList.open("winelist.txt");//open file
string fileLine;//will store cut out lines from a file in this
while(getline(inList, fileLine))//read file, send onto processing
{
read_line(fileLine, wineList[lineNum]);
wineList[lineNum].setWineUpc(100000+lineNum);
lineNum ++;
}
inList.close();
}
/**
Processes the properly saved file by cutting it up and feeding separate lines to the
read_line processor
@param wineList[] structure for wine descriptions/rating/price
*/
voiddataProcessing::processProperListFile(vector<Wine> &wineList)
{
unsignedint lineNum = 0;
ifstream inList;
inList.open("winelistsave.txt");//open file
string fileLine;//will store cut out lines from a file in this
while(getline(inList, fileLine))//read file, send onto processing
{
if(wineList.size() > (lineNum)){
readProper_line(fileLine, wineList[lineNum]);
lineNum++;
}else{
//readProper_line(fileLine, wineList[lineNum]);
//lineNum++;
}
}
inList.close();
}
/**
Processes the properly saved file by cutting it up and feeding separate lines to the
read_line processor
@param wineList[] structure for wine descriptions/rating/price
*/
unsignedintdataProcessing::checkFileSize()
{
unsignedint lineNum = 0;
ifstream inList;
inList.open("winelistsave.txt");//open file
string fileLine;//will store cut out lines from a file in this
while(getline(inList, fileLine))//read file, send onto processing
{
lineNum++;
}
inList.close();
return lineNum;
}
/**
Sorts Wine objects
@param wineList[] vector of Wine objects to be sorted and
@param sortedWines[] to store the sorted vector
*/
voidSorter::sortWines(vector<Wine> wineList, vector<Wine> &sortedWines)
{
bool sorted = false;
Wine buffer;
while(!sorted)
{
for(unsignedint i=0; i < (wineList.size()-1); i++)
{
if(wineList[i+1] > wineList[i])
{
buffer = wineList[i];
wineList[i]=wineList[i+1];
wineList[i+1]=buffer;
i=0;
}
else{sorted = true;};
}
}
for(unsignedint i=0; i < wineList.size(); i++)
{sortedWines[i] = wineList[i];
};
}
/**
Sorts Wine objects according to price
@param wineList[] vector of Wine objects to be sorted and
@param sortedWines[] to store the sorted vector
*/
voidSorter::sortWinesByPrice(vector<Wine> &sortWines)
{
sort(sortWines.begin(), sortWines.end());
}