lunch-rest/menu.cpp
2020-09-15 01:30:34 +02:00

70 lines
1.6 KiB
C++

#include "menu.hpp"
#include <sstream>
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;
}
void LunchRest::Menu::setInvalidMenu() {
_valid = false;
}
bool LunchRest::Menu::isValid() const {
return _valid;
}
std::string LunchRest::Menu::jsonify() const {
std::stringstream ss{};
ss << "[";
auto soupInd = getSoupIndex();
ss << getMeals()[soupInd].jsonify() << ",";
for(unsigned long int i = 0; i < getMeals().size(); i++) {
if(i != soupInd)
ss << getMeals()[i].jsonify() << ",";
}
ss.seekp(-1, ss.cur);
ss << "]";
return ss.str();
}