-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathserving.cpp
62 lines (51 loc) · 2.14 KB
/
serving.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
#include "serving.h"
#include <iostream>
#include <stdexcept>
namespace Mice {
Serving::Serving(Container container) : _container{container} { }
Serving::Serving(std::istream& ist) {
// The header must have been stripped from the incoming stream at this point
std::string header1, header2;
while (true) {
std::getline(ist, header1); // header
std::getline(ist, header2);
if (header1 != "#") throw std::runtime_error("missing # during input");
if (header2 == "END SERVING") break; // footer
else if (header2 == "CONTAINER") _container = Mice::Container{ist};
else if (header2 == "SCOOP") _scoops.push_back(Mice::Scoop{ist});
else if (header2 == "TOPPING") _toppings.push_back(Mice::Topping{ist});
else throw std::runtime_error("invalid item type in Serving");
}
}
void Serving::save(std::ostream& ost) {
ost << "#" << std::endl << "SERVING" << std::endl; // header
_container.save(ost);
for (Scoop& s : _scoops) s.save(ost);
for (Topping& t : _toppings) t.save(ost);
ost << "#" << std::endl << "END SERVING" << std::endl; // footer
}
Container Serving::container() const {return _container;}
std::vector<Scoop> Serving::scoops() const {return _scoops;}
std::vector<Topping> Serving::toppings() const {return _toppings;}
void Serving::add_scoop(Scoop scoop) {_scoops.push_back(scoop);}
void Serving::add_topping(Topping topping) {_toppings.push_back(topping);}
double Serving::cost() const {
double total = _container.cost();
for (Scoop scoop : _scoops) total += scoop.cost();
for (Topping topping : _toppings) total += topping.cost();
return total;
}
double Serving::price() const {
double total = _container.price();
for (Scoop scoop : _scoops) total += scoop.price();
for (Topping topping : _toppings) total += topping.price();
return total;
}
}
// OPERATOR OVERLOADING for class Serving
std::ostream& operator<<(std::ostream& os, const Mice::Serving& serving) {
os << serving.container();
for (Mice::Scoop s : serving.scoops()) os << std::endl << s;
for (Mice::Topping t : serving.toppings()) os << std::endl << t;
return os;
}