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
|
#include <iostream>
#include <fstream>
#include <string>
#include <vector>
using namespace std;
class Wine
{
private:
string name;
int year, score;
double price;
public:
Wine (string aName = "??? UNKNOWN ???", int aYear = 666, int aScore = 0, double aPrice = 0)
{
name = aName;
year = aYear;
score = aScore;
price = aPrice;
}
void setName (string aName)
{ name = aName;}
string getName()
{ return name; }
void setYear (int aYear)
{ year = aYear;}
int getYear()
{ return year; }
void setScore (int aScore)
{ score = aScore; }
int getScore ()
{ return score; }
void setPrice (double aPrice)
{ price = aPrice; }
double getPrice ()
{ return price; }
};
bool compare_Score (Wine i, Wine j)
{ return i.getScore () < j.getScore (); }
bool compare_Price (Wine lhs, Wine rhs)
{ return lhs.getPrice () < rhs.getPrice (); }
size_t tokenize(string &, string &, string);
void findWines (string search)
{
string line, lineArray[30];
ifstream inFile;
bool found = false;
int results = 0;
inFile.open("winelist copy.txt");
size_t pos;
while(inFile.good())
{
getline (inFile,line);
pos = line.find (search);
if (pos != string::npos)
{
lineArray[results] = line;
results++;
found = true;
}
}
inFile.close();
if (!found)
cout << search << " not found. " << endl;
else if (found)
{
cout << results << " result(s) of " << search << " found. " << endl;
for (int j = 0; j < results; j++)
{
cout << "\nResult #" << j + 1 << ": " << endl;
cout << lineArray [j] << endl;
}
cout << endl;
}
}
int main ()
{
string line, token, wineType;
ifstream myfile ("winelist copy.txt");
const int no_properties = 6;
string array[no_properties];
int count = 0, input;
vector<Wine> wine_list;
Wine temp, rough_red("Rough red", 2017), temp_default;
wine_list.push_back(rough_red);
wine_list.push_back(temp_default);
while (input != 6)
{
while( getline(myfile, line) and line.length() > 0 )
{
count = 0;
while( tokenize(token, line, ";") != -1 )
{
array[count] = token;
count++;
}
array[count] = line;
temp.setName(array[0]);
temp.setYear(stoi(array[2]));
wine_list.push_back(temp);
}
cout << "Would you like to: "
<< "\n1. Search for a range of wine scores "
<< "\n2. Search for a range of wine prices "
<< "\n3. Display all wines sorted by score "
<< "\n4. Display all wines sorted by price "
<< "\n5. Search for a wine type "
<< "\n6. End Program" << endl;
cin >> input;
if (input == 1)
{
//Ahhh! Spent so long trying to do this >:(
;
}
else if ( input == 2)
{
//Same problem here >:(
;
}
else if (input == 3)
{
sort(wine_list.begin(), wine_list.end(), compare_Score);
cout << "WINE LIST SORTED ON SCORE:\n";
for(auto i: wine_list)
cout << "* " << i.getName() << '-' << i.getScore() << '\n';
}
else if (input == 4)
{
cout << "\nWINE LIST SORTED ON PRICE:\n";
sort (wine_list.begin(), wine_list.end(), compare_Price);
for (auto i: wine_list)
cout << "* " << i.getName() << '-' << i.getPrice() << endl;
}
else if (input == 5)
{
cout << "What wine would you like to look for? " << endl;
cin >> wineType;
findWines(wineType);
}
}
myfile.close();
cout << "PROGRAM END\n";
return 0;
}
size_t tokenize(string &token, string &aLine, string delimiter)
{
size_t pos = aLine.find_first_of(delimiter);
if (pos != string::npos)
{
token = aLine.substr(0, pos);
aLine = aLine.erase(0, pos + 1);
}
return pos;
}
|