-
Notifications
You must be signed in to change notification settings - Fork 0
/
Food.h
163 lines (130 loc) · 3.61 KB
/
Food.h
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
#pragma once
#include <iostream>
#include <string>
using namespace std;
class Food {
// Fields
string category_;
string description_;
double carbohydrates_;
double fiber_;
double protein_;
double sugar_;
double sodium_;
// Fields to compare the macronutrients
bool compare_carbohydrates = false;
bool compare_fiber = false;
bool compare_protein = false;
bool compare_sugar = false;
bool compare_sodium = false;
public:
// Constructor to initialize the fields
Food(string category, string description, double carbohydrates, double fiber, double protein, double sugar, double sodium);
// Accessors
string category() const;
string description() const;
double carbohydrates() const;
double fiber() const;
double protein() const;
double sugar() const;
double sodium() const;
// Get the field value of the food object based on the field number
double getMacro(int field) const;
// Set the field comparison to true of the field number
void fieldComparisonSetTrue(int field);
// Set all the field comparison to false
void fieldComparisonSetFalse();
// Define the less-than operator to compare based on the macronutrient
bool operator<(const Food& other) const;
};
// Constructor to initialize the fields
Food::Food(string category, string description, double carbohydrates, double fiber, double protein, double sugar, double sodium) {
category_ = category;
description_ = description;
carbohydrates_ = carbohydrates;
fiber_ = fiber;
protein_ = protein;
sugar_ = sugar;
sodium_ = sodium;
}
// Accessors
string Food::category() const {
return category_;
}
string Food::description() const {
return description_;
}
double Food::carbohydrates() const {
return carbohydrates_;
}
double Food::fiber() const {
return fiber_;
}
double Food::protein() const {
return protein_;
}
double Food::sugar() const {
return sugar_;
}
double Food::sodium() const {
return sodium_;
}
// Define a function to get the field value
double Food::getMacro(int field) const {
switch (field) {
case 1:
return carbohydrates_;
case 2:
return fiber_;
case 3:
return protein_;
case 4:
return sugar_;
case 5:
return sodium_;
}
return 0;
}
// Set the field comparison to true of the field number
void Food::fieldComparisonSetTrue(int field) {
switch (field) {
case 1:
compare_carbohydrates = true;
break;
case 2:
compare_fiber = true;
break;
case 3:
compare_protein = true;
break;
case 4:
compare_sugar = true;
break;
case 5:
compare_sodium = true;
break;
}
}
// Set all the field comparison to false
void Food::fieldComparisonSetFalse() {
compare_carbohydrates = false;
compare_fiber = false;
compare_protein = false;
compare_sugar = false;
compare_sodium = false;
}
// Define the less-than operator to compare based on the macronutrient
bool Food::operator<(const Food& other) const {
if (compare_carbohydrates) {
return carbohydrates_ < other.carbohydrates_;
} else if (compare_fiber) {
return fiber_ < other.fiber_;
} else if (compare_protein) {
return protein_ < other.protein_;
} else if (compare_sugar) {
return sugar_ < other.sugar_;
} else if (compare_sodium) {
return sodium_ < other.sodium_;
}
return carbohydrates_ < other.carbohydrates_;
}