lunch-rest/menu.cpp

82 lines
1.9 KiB
C++
Raw Normal View History

2020-09-14 22:55:03 +00:00
#include "menu.hpp"
2020-09-14 23:30:34 +00:00
#include <sstream>
2020-09-14 22:55:03 +00:00
void LunchRest::Menu::addMeal(bool soup, const std::string &name, const std::string &desc, int price) {
_meals.emplace_back(soup, name, desc, price);
}
void LunchRest::Menu::addMeal(const LunchRest::Meal &meal) {
_meals.push_back(meal);
}
bool LunchRest::Menu::hasSoup() const {
bool ret = false;
for(auto &x : _meals) {
ret |= x.isSoup();
}
return ret;
}
LunchRest::Meal LunchRest::Menu::getSoup() const {
for(auto &x : _meals) {
if(x.isSoup())
return x;
}
return Meal(true, "", "", 0);
}
std::vector<LunchRest::Meal> LunchRest::Menu::getNonSoupMeals() {
std::vector<Meal> ret{};
for(auto &x : _meals) {
if(!x.isSoup())
ret.push_back(x);
}
return ret;
}
const std::vector<LunchRest::Meal> &LunchRest::Menu::getMeals() const {
return _meals;
}
unsigned long int LunchRest::Menu::getSoupIndex() const {
for(unsigned long int i = 0; i < _meals.size(); i++) {
if(_meals[i].isSoup())
return i;
}
return -1;
}
2020-09-15 14:05:16 +00:00
void LunchRest::Menu::setInvalidMenu(bool invalid) {
_valid = !invalid;
2020-09-14 22:55:03 +00:00
}
bool LunchRest::Menu::isValid() const {
return _valid;
}
2020-09-14 23:30:34 +00:00
std::string LunchRest::Menu::jsonify() const {
if(!isValid())
2020-09-15 19:28:41 +00:00
return "{\"day\": \"" + getDay() + "\", \"meals\": []}";
2020-09-14 23:30:34 +00:00
std::stringstream ss{};
2020-09-15 19:28:41 +00:00
ss << "{\"day\": \"" << getDay() << "\", \"meals\": [";
2020-09-14 23:30:34 +00:00
auto soupInd = getSoupIndex();
if(soupInd != (unsigned long int)-1)
ss << getMeals()[soupInd].jsonify() << ",";
2020-09-14 23:30:34 +00:00
for(unsigned long int i = 0; i < getMeals().size(); i++) {
if(i != soupInd)
ss << getMeals()[i].jsonify() << ",";
}
if(getMeals().size() > 0)
ss.seekp(-1, ss.cur);
2020-09-15 19:28:41 +00:00
ss << "]}";
2020-09-14 23:30:34 +00:00
return ss.str();
}
2020-09-15 19:28:41 +00:00
void LunchRest::Menu::setDay(const std::string &day) {
_day = day;
}
const std::string &LunchRest::Menu::getDay() const {
return _day;
}