-
Notifications
You must be signed in to change notification settings - Fork 0
/
pairmeal.cpp
141 lines (127 loc) · 3.79 KB
/
pairmeal.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
// pairmeal.cpp
#include <string>
#include <vector>
#include <iostream>
#include <stdexcept>
#include "fooddatabase.h"
#include "meal.h"
#include "pairmeal.h"
#include "halfmeal.h"
#include "bruschetta.h"
#include "sandwich.h"
#include "salad.h"
#include "burger.h"
#include "soup.h"
using std::string;
using std::vector;
using std::pair;
using std::cout;
using std::endl;
typedef vector< pair< string, size_t> >::const_iterator vpiter;
typedef vector<string>::const_iterator vsiter;
Pairmeal::Pairmeal(const FoodDatabase& fdb, const vector< pair<string,size_t> >& used, const vector< pair<string, size_t > >& haveleft, size_t ID, size_t freq) : Meal (ID, freq )
{
// If we have baguette, automatically make bruschetta or sandwich
// If instead have hamburger, automatically make burger
// In either case, pair it with soup or salad
if ( findIfItemInVec( "baguette", haveleft ) || findIfItemInVec("veggie burger", haveleft) || findIfItemInVec("salmon burger", haveleft) || findIfItemInVec("hamburger",haveleft) )
{
if ( findIfItemInVec( "baguette", haveleft ) )
{
size_t randfirst = rand() % 2;
if (randfirst == 0)
{
m_first = new Bruschetta(fdb, used, haveleft, freq);
}
else
{
m_first = new Sandwich(fdb, used, haveleft, freq);
}
}
else
{
m_first = new Burger(fdb, used, haveleft, freq);
}
size_t randsecond = rand() % 2;
m_second = getNewHalfmeal( randsecond, fdb, used, haveleft, freq );
}
// otherwise pick a halfmeal type randomly
// if we get something with bread, pick soup or salad to go with it
// else, pick anything to go with it except itself
else
{
size_t randfirst = rand() % 5;
m_first = getNewHalfmeal( randfirst, fdb, used, haveleft, freq );
if (randfirst == 2 || randfirst == 3 || randfirst == 4)
{
size_t randsecond = rand() % 2;
m_second = getNewHalfmeal( randsecond, fdb, used, haveleft, freq );
}
else
{
size_t randsecond = rand() % 4;
while (randsecond == randfirst)
{
randsecond = rand() % 4;
}
m_second = getNewHalfmeal( randsecond, fdb, used, haveleft, freq );
}
}
// build m_foodinmeal
vector<string> firstFoods = m_first->getFoodNames();
vector<string> secondFoods = m_second->getFoodNames();
for ( vector<string>::const_iterator it = firstFoods.begin(); it != firstFoods.end(); ++it)
{
m_foodInMeal.push_back( *it );
}
for ( vector<string>::const_iterator it2 = secondFoods.begin(); it2 != secondFoods.end(); ++it2)
{
// in future version, add a check that item not in first halfmeal
m_foodInMeal.push_back( *it2 );
}
}
Pairmeal::~Pairmeal()
{
delete m_first;
delete m_second;
}
void Pairmeal::printMeal() const
{
cout << "Meal " << m_ID << endl;
cout << "\tFrequency: " << m_freq << endl;
cout << "\t";
m_first->printHalfmeal();
cout << "\t";
m_second->printHalfmeal();
cout << endl;
}
Halfmeal* Pairmeal::getNewHalfmeal( size_t num, const FoodDatabase& fdb, const vector< pair<string,size_t> >& used, const vector< pair<string, size_t > >& haveleft, size_t freq ) const
{
Halfmeal* halfptr;
switch ( num )
{
case 0:
halfptr = new Salad(fdb, used, haveleft, freq);
return halfptr;
case 1:
halfptr = new Soup(fdb, used, haveleft, freq);
return halfptr;
case 2:
halfptr = new Bruschetta(fdb, used, haveleft, freq);
return halfptr;
case 3:
halfptr = new Sandwich(fdb, used, haveleft, freq);
return halfptr;
case 4:
halfptr = new Burger(fdb, used, haveleft, freq);
return halfptr;
default:
throw std::logic_error ("Change to out of bounds exception");
}
return halfptr;
}
double Pairmeal::getMealCost() const
{
double cost = m_first->getCost() + m_second->getCost();
return cost;
}