Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Append new recipe #725

Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
62 changes: 62 additions & 0 deletions homework/append-new-recipe/AppendNewRecipe.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
#include "AppendNewRecipe.hpp"

std::ofstream recipes("recipes.txt", std::ios::out | std::ios::app);

std::stringstream FormatRecipit(std::vector<std::string> steps,
const std::list<std::string>& ingredients,
const std::deque<std::pair<size_t, char>>& amount) {
std::string result{};
std::stringstream ss{};
ss << "Skladniki:\n";
auto items = FormatIngredients(ingredients, amount);
for (auto const item : items) {
ss << item << ",\n";
}
ss << "\nKroki:\n";
for (size_t i = 0; i < steps.size(); ++i) {
ss << i + 1 << ") " << steps[i] << ".\n";
}
ss << "___________________________________\n";
return ss;
}
bool AppendNewRecipe(std::vector<std::string> steps,
const std::list<std::string>& ingredients,
const std::deque<std::pair<size_t, char>>& amount) {
if (recipes.is_open()) {
recipes << FormatRecipit(steps, ingredients, amount).str();
recipes.close();
return true;
} else {
return false;
}
}

std::vector<std::string> FormatIngredients(const std::list<std::string>& ingredients,
const std::deque<std::pair<size_t, char>>& amount) {
std::vector<std::string> res{};
auto itr = amount.begin();
for (auto x : ingredients) {
std::stringstream ss{};
std::string row{};
ss << itr->first;
row = ss.str();
switch (itr->second) {
case 'g': {
row += " gram";
break;
}
case 's': {
row += " szklanka(i)";
break;
}
case 'm': {
row += " mililitrow";
break;
}
}
row += " " + x;
++itr;
res.push_back(row);
}
return res;
}
21 changes: 21 additions & 0 deletions homework/append-new-recipe/AppendNewRecipe.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
#pragma once
#include <deque>
#include <fstream>
#include <iostream>
#include <list>
#include <sstream>
#include <string>
#include <vector>

extern std::ofstream recipes;

std::vector<std::string> FormatIngredients(const std::list<std::string>& ingredients,
const std::deque<std::pair<size_t, char>>& amount);

bool AppendNewRecipe(std::vector<std::string> steps,
const std::list<std::string>& ingredients,
const std::deque<std::pair<size_t, char>>& amount);

std::stringstream FormatRecipit(std::vector<std::string> steps,
const std::list<std::string>& ingredients,
const std::deque<std::pair<size_t, char>>& amount);
21 changes: 21 additions & 0 deletions homework/append-new-recipe/main.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
#include <fstream>
#include <iostream>
#include "AppendNewRecipe.hpp"

int main() {
recipes << "";
recipes.close();
std::list<std::string> ingredients{"cukru", "maki", "rumu"};
std::deque<std::pair<size_t, char>> amount{
{20, 'g'},
{1, 's'},
{40, 'm'}};
std::vector<std::string> result = FormatIngredients(ingredients, amount);
std::vector<std::string> steps{"Wsypac do miski 20 gram cukru",
"Dorzucic 1 szklanke maki",
"Dokladnie wymieszac",
"Nalac 40ml rumu do kieliszka",
"Wypic kieliszek",
"Wysypac zawartosc miski"};
FormatRecipit(steps, ingredients, amount);
}
1 change: 0 additions & 1 deletion homework/append-new-recipe/test.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,6 @@ TEST_F(Test, ShouldFormatWholeRecipe) {
{20, 'g'},
{1, 's'},
{40, 'm'}};

EXPECT_EQ(FormatRecipit(steps, ingredients, amount).str(), kExpected);
}

Expand Down
Loading