-
Notifications
You must be signed in to change notification settings - Fork 0
/
fooddatabase.cpp
124 lines (109 loc) · 2.98 KB
/
fooddatabase.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
// fooddatabase.cpp
#include <string>
#include <vector>
#include <fstream>
#include <iostream>
#include <sstream>
#include <algorithm>
#include <stdexcept>
#include <time.h>
#include "fooddatabase.h"
#include "foodgroup.h"
#include "ingredient.h"
using std::vector;
using std::string;
using std::pair;
using std::cout;
using std::endl;
FoodDatabase::FoodDatabase(const string& filename)
{
std::ifstream file ( filename.c_str() );
if ( file.is_open() )
{
string txtline;
while ( file.good() )
{
while (true)
{
std::getline(file,txtline);
if (txtline.length() != 0 || file.eof())
break;
}
FoodGroup* newGroupPtr = new FoodGroup(txtline);
string name, cost_str;
double cost;
while (true)
{
std::getline(file,txtline,'\n');
if (txtline.length() == 0 )
{
break;
}
std::istringstream subline( txtline );
getline ( subline, name, ',');
getline ( subline, cost_str, ';');
cost = atof ( cost_str.c_str() );
newGroupPtr->addFood(name,cost);
}
m_allFoods.push_back(newGroupPtr);
}
}
else
std::cerr << "Failed to open file: " << filename << std::endl;
}
FoodDatabase::~FoodDatabase()
{
for (vector<FoodGroup*>::const_iterator fg_it = m_allFoods.begin(); fg_it != m_allFoods.end(); ++fg_it)
{
delete *fg_it;
}
}
const Ingredient& FoodDatabase::getRandomIngred(const string& type, const vector<string>& unwantedList) const
{
for (vector<FoodGroup*>::const_iterator fg_it = m_allFoods.begin(); fg_it != m_allFoods.end(); ++fg_it)
{
if ( (*fg_it)->getType() == type)
{
if (unwantedList.empty())
{
return (*fg_it)->getRandomIngred();
}
else
{
return (*fg_it)->getRandomIngredButNot( unwantedList );
}
}
}
throw std::logic_error ( "Attempted to get an ingredient from a foodgroup that does not exist." );
}
const Ingredient& FoodDatabase::getSpecificIngred( const string& type, const string& Ingredname ) const
{
for (vector<FoodGroup*>::const_iterator fg_it = m_allFoods.begin(); fg_it != m_allFoods.end(); ++fg_it)
{
if ( (*fg_it)->getType() == type)
{
return (*fg_it)->getIngred( Ingredname );
}
}
throw std::logic_error ("Attempted to retrieve specific ingredient from foodgroup it is not in.");
}
bool FoodDatabase::checkIngredType( const string& type, const string& ingredName ) const
{
for (vector<FoodGroup*>::const_iterator fg_it = m_allFoods.begin(); fg_it != m_allFoods.end(); ++fg_it)
{
if ( (*fg_it)->getType() == type)
{
return (*fg_it)->contains(ingredName);
}
}
throw std::logic_error ("Invalid foodgroup specified when checking specific ingredient foodgroup.");
}
bool FoodDatabase::checkIngredExistence( const string& ingredName ) const
{
for (vector<FoodGroup*>::const_iterator fg_it = m_allFoods.begin(); fg_it != m_allFoods.end(); ++fg_it)
{
if ( (*fg_it)->contains(ingredName) )
return true;
}
return false;
}