Website Summary Files Commits Branches @commit @diff @stats
path: root / Week03 / Exercise01 / Shop.cpp
diff options
context:
space:
mode:
Commit5624cd9b63bec13fc464a3d7820d703abd8de113 (patch)
Author
 Syndamia <kamen@syndamia.com>
2026-03-08 19:54:32 +0200
Committer
 Syndamia <kamen@syndamia.com>
2026-03-08 19:54:32 +0200
Files8d9915e71a7b9e757752fe3b379138804e51a8b7 /Week03/Exercise01/Shop.cpp
oop-2025-solutions-5624cd9b63bec13fc464a3d7820d703abd8de113.tar
oop-2025-solutions-5624cd9b63bec13fc464a3d7820d703abd8de113.tar.gz
oop-2025-solutions-5624cd9b63bec13fc464a3d7820d703abd8de113.zip
Parentf18c44909c604a01bb0265d006a84223ee88d603 (diff)
feat(Week03): Add solution to first exercise
1 files changed, 213 insertions, 0 deletions
-rw-r--r--Week03/Exercise01/Shop.cpp213
diff --git a/Week03/Exercise01/Shop.cpp b/Week03/Exercise01/Shop.cpp
new file mode 100644
index 0000000..ce92d25
--- /dev/null
+++ b/Week03/Exercise01/Shop.cpp
@@ -0,0 +1,213 @@
+#include <iostream>
+#include <cstring>
+#include "Shop.h"
+
+void readUsername(char* str, unsigned length) {
+ std::cout << "Username: ";
+ std::cin.getline(str, length);
+}
+
+void readPassword(char* str, unsigned length) {
+ std::cout << "Password: ";
+ std::cin.getline(str, length);
+}
+
+void readName(char* str, unsigned length) {
+ std::cout << "Name: ";
+ std::cin.getline(str, length);
+}
+
+void Shop::runCommand(char command) {
+ switch (command) {
+ case 'l':
+ if (loggedInUser == nullptr) {
+ User temp;
+ readUsername(temp.name, sizeof(temp.name) / sizeof(*temp.name));
+
+ User* registeredUser = users.find(temp.name);
+
+ if (registeredUser == nullptr) {
+ std::cout << "User '" << temp.name << "' does not exist!" << std::endl;
+ return;
+ }
+
+ readPassword(temp.password, sizeof(temp.password) / sizeof(*temp.password));
+
+ if (strcmp(registeredUser->password, temp.password) != 0) {
+ std::cout << "Incorrect password!" << std::endl;
+ return;
+ }
+
+ loggedInUser = registeredUser;
+ std::cout << "Successfull login!" << std::endl;
+ }
+ break;
+
+ case 'r':
+ if (loggedInUser == nullptr) {
+ User temp;
+ temp.isAdmin = false;
+ readUsername(temp.name, sizeof(temp.name) / sizeof(*temp.name));
+
+ User* registeredUser = users.find(temp.name);
+
+ if (registeredUser != nullptr) {
+ std::cout << "User '" << temp.name << "' is already registered!" << std::endl;
+ return;
+ }
+
+ readPassword(temp.password, sizeof(temp.password) / sizeof(*temp.password));
+
+ users.add(temp);
+ std::cout << "Successfully registered user '" << temp.name << "'!" << std::endl;
+ }
+ break;
+
+ case 'p':
+ if (products.count > 0) {
+ for (unsigned i = 0; i < products.count; ++i)
+ std::cout << products.products[i].name << ' '
+ << products.products[i].count << ' '
+ << products.products[i].price << std::endl;
+ }
+ else {
+ std::cout << "No products!" << std::endl;
+ }
+ break;
+
+ case 'e':
+ if (loggedInUser != nullptr) {
+ loggedInUser = nullptr;
+ std::cout << "Logged out!" << std::endl;
+ }
+ break;
+
+ case 'b':
+ if (loggedInUser != nullptr) {
+ unsigned N;
+
+ std::cout << "Number of products: ";
+ std::cin >> N;
+ std::cin.ignore();
+
+ Product** shoppingCartProducts = new Product*[N];
+ unsigned* shoppingCartCounts = new unsigned[N];
+ unsigned cartItems = 0;
+
+ for (unsigned i = 0; i < N; ++i) {
+ Product temp;
+
+ readName(temp.name, sizeof(temp.name) / sizeof(*temp.name));
+
+ Product* storedProduct = products.find(temp.name);
+ if (storedProduct == nullptr) {
+ std::cout << "Invalid product name!" << std::endl;
+ delete[] shoppingCartProducts;
+ delete[] shoppingCartCounts;
+ return;
+ }
+
+ std::cout << "Quantity: ";
+ std::cin >> temp.count;
+ std::cin.ignore();
+
+ if (temp.count > storedProduct->count) {
+ std::cout << "Invalid quantity!" << std::endl;
+ delete[] shoppingCartProducts;
+ delete[] shoppingCartCounts;
+ return;
+ }
+
+ shoppingCartProducts[cartItems] = storedProduct;
+ shoppingCartCounts[cartItems++] = temp.count;
+ }
+
+ double total = 0.0;
+ for (unsigned i = 0; i < N; ++i) {
+ total += shoppingCartProducts[i]->price * shoppingCartCounts[i];
+ shoppingCartProducts[i]->count -= shoppingCartCounts[i];
+ }
+
+ std::cout << "Purchase successful! Total cost: " << total << std::endl;
+
+ delete[] shoppingCartProducts;
+ delete[] shoppingCartCounts;
+ }
+ break;
+
+ case 'a':
+ if (loggedInUser != nullptr) {
+ if (!loggedInUser->isAdmin) {
+ std::cout << "No permissions!" << std::endl;
+ return;
+ }
+
+ User temp;
+
+ readUsername(temp.name, sizeof(temp.name) / sizeof(*temp.name));
+
+ User* registeredUser = users.find(temp.name);
+
+ if (registeredUser == nullptr) {
+ std::cout << "User '" << temp.name << "' does not exist!" << std::endl;
+ return;
+ }
+
+ registeredUser->isAdmin = !registeredUser->isAdmin;
+ if (registeredUser->isAdmin) {
+ std::cout << "Made user '" << registeredUser->name << "' into an administrator." << std::endl;
+ }
+ else {
+ std::cout << "Made administrator '" << registeredUser->name << "' into a user." << std::endl;
+ }
+ }
+ break;
+
+ case 's':
+ if (loggedInUser != nullptr) {
+ if (!loggedInUser->isAdmin) {
+ std::cout << "No permissions!" << std::endl;
+ return;
+ }
+
+ Product temp;
+
+ readName(temp.name, sizeof(temp.name) / sizeof(*temp.name));
+
+ std::cout << "Quantity: ";
+ std::cin >> temp.count;
+
+ std::cout << "Price: ";
+ std::cin >> temp.price;
+ std::cin.ignore();
+
+ Product* storedProduct = products.find(temp.name);
+
+ if (storedProduct == nullptr) {
+ products.add(temp);
+ }
+ else {
+ storedProduct->count += temp.count;
+ storedProduct->price = temp.price;
+ }
+ }
+ break;
+
+ default:
+ std::cout << "Invalid command '" << command << "'!" << std::endl;
+ }
+}
+
+Shop newShop() {
+ Shop s;
+ s.users = newUserArray(64);
+ s.products = newProductArray(64);
+ s.loggedInUser = nullptr;
+ return s;
+}
+
+void deleteShop(Shop& shop) {
+ deleteUserArray(shop.users);
+ deleteProductArray(shop.products);
+ shop.loggedInUser = nullptr;
+}