-
Notifications
You must be signed in to change notification settings - Fork 0
/
Ingredient.cpp
68 lines (58 loc) · 1.84 KB
/
Ingredient.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
#include "include.h"
using namespace std;
bool Ingredient::operator ==(const IComparable& theOther){
bool r = false;
if (name == ((Ingredient&)theOther).getName() &&
quantity <= ((Ingredient&)theOther).getQuantity() &&
quantityType == ((Ingredient&)theOther).getQuantityType())
{
r = true;
}
return r;
}
Ingredient Ingredient::operator =(const Ingredient& theOther){
if (this != &theOther){ // allow to avoid error with auto assignement
name = ((Ingredient&)theOther).getName();
quantity = ((Ingredient&)theOther).getQuantity();
quantityType = ((Ingredient&)theOther).getQuantityType();
}
return *this;
}
istream &operator>>(istream &in, ostream& out){
vector<Ingredient> ingredients;
string line,delimiter = ";";
while (getline(in, line)) {
vector<string> v;
split(line, ';', v);
//allow to convert string to int
string::size_type sz; // alias of size_t
int quantity = stoi(v[1], &sz);
ingredients.push_back(Ingredient(v[0], quantity, v[2]));
}
int nbLines = ingredients.size();
for (int i = 0; i < nbLines; i++)
{
out << "Name : " << ingredients.back().getName()
<< " - Quantity : " << ingredients.back().getQuantity()
<< " - Quantity Type : " << ingredients.back().getQuantityType() << endl;
ingredients.pop_back();//remove the last case
}
return in;
}
string &operator>>(string &line, Ingredient& ingredient){
string delimiter = ";";
int quantity, nbElement = 0;
vector<string> v;
split(line, ';', v);
//allow to convert string to int
string::size_type sz; // alias of size_t
quantity = stoi(v[1], &sz);
ingredient.setName(v[0]);
ingredient.setQuantity(quantity);
ingredient.setQuantityType(v[2]);
return line;
}
ostream &operator<<(ostream &out, const Ingredient& theOther){
out << theOther.getName() << ";" << theOther.getQuantity() << ";" << theOther.getQuantityType() << ";\n";
return out;
}