aboutsummaryrefslogtreecommitdiff
path: root/week07/Exercise1.cpp
diff options
context:
space:
mode:
Diffstat (limited to 'week07/Exercise1.cpp')
-rw-r--r--week07/Exercise1.cpp87
1 files changed, 87 insertions, 0 deletions
diff --git a/week07/Exercise1.cpp b/week07/Exercise1.cpp
new file mode 100644
index 0000000..ec2a47a
--- /dev/null
+++ b/week07/Exercise1.cpp
@@ -0,0 +1,87 @@
+#include "Exercise1.h"
+#include <cstring>
+
+/* Private */
+
+void Recipe::resize() {
+ allocated *= 2;
+ Ingredient* moreIngredients = new Ingredient[allocated];
+ for (int i = 0; i < lastIndex; i++) {
+ moreIngredients[i] = ingredients[i];
+ }
+ delete[] ingredients;
+ ingredients = moreIngredients;
+}
+
+void Recipe::free() {
+ delete[] ingredients;
+}
+void Recipe::copyFrom(const Recipe& other) {
+ this->lastIndex = other.lastIndex;
+ this->allocated = other.allocated;
+ this->ingredients = new Ingredient[allocated];
+ for (int i = 0; i < lastIndex; i++) {
+ this->ingredients[i] = other.ingredients[i];
+ }
+}
+
+/* Public */
+
+Recipe::Recipe() {
+ ingredients = nullptr;
+ lastIndex = allocated = 0;
+}
+Recipe::~Recipe() {
+ free();
+}
+Recipe::Recipe(const Recipe& other) {
+ copyFrom(other);
+}
+Recipe& Recipe::operator=(const Recipe& other) {
+ if (this != &other) {
+ free();
+ copyFrom(other);
+ }
+ return *this;
+}
+Recipe::Recipe(Recipe&& other) {
+ this->ingredients = other.ingredients;
+ this->lastIndex = other.lastIndex;
+ this->allocated = other.allocated;
+
+ other.ingredients = nullptr;
+}
+Recipe& Recipe::operator=(Recipe&& other) {
+ if (this != &other) {
+ free();
+
+ this->ingredients = other.ingredients;
+ this->lastIndex = other.lastIndex;
+ this->allocated = other.allocated;
+
+ other.ingredients = nullptr;
+ }
+ return *this;
+}
+
+void Recipe::AddIngredient(const Ingredient& newIng) {
+ if (lastIndex == allocated) {
+ resize();
+ }
+ ingredients[lastIndex++] = newIng;
+}
+void Recipe::RemoveIngredient(const char* name) {
+ int index = 0;
+ while (index < lastIndex && strcmp(ingredients[index].name, name) != 0) {
+ index++;
+ }
+ if (index == lastIndex) {
+ return;
+ }
+
+ while (index < lastIndex) {
+ ingredients[index] = ingredients[index+1];
+ index++;
+ }
+ lastIndex--;
+}