diff options
feat(Week03): Add solution to first exercise
7 files changed, 366 insertions, 0 deletions
| -rw-r--r-- | Week03/Exercise01/Product.cpp | 33 | ||||
| -rw-r--r-- | Week03/Exercise01/Product.h | 22 | ||||
| -rw-r--r-- | Week03/Exercise01/Shop.cpp | 213 | ||||
| -rw-r--r-- | Week03/Exercise01/Shop.h | 18 | ||||
| -rw-r--r-- | Week03/Exercise01/User.cpp | 33 | ||||
| -rw-r--r-- | Week03/Exercise01/User.h | 22 | ||||
| -rw-r--r-- | Week03/Exercise01/main.cpp | 25 |
diff --git a/Week03/Exercise01/Product.cpp b/Week03/Exercise01/Product.cpp new file mode 100644 index 0000000..1bfa66e --- /dev/null +++ b/Week03/Exercise01/Product.cpp @@ -0,0 +1,33 @@ +#include <iostream> +#include <cstring> +#include "Product.h" + +void ProductArray::add(const Product& product) { + if (count >= allocated) { + std::cout << "No more products are allowed!" << std::endl; + return; + } + + products[count++] = product; +} + +Product* ProductArray::find(char* username) { + for (unsigned i = 0; i < count; ++i) + if (strcmp(username, products[i].name) == 0) + return &products[i]; + return nullptr; +} + +ProductArray newProductArray(unsigned allocatedSize) { + ProductArray pa; + pa.products = new Product[allocatedSize]; + pa.count = 0; + pa.allocated = allocatedSize; + return pa; +} + +void deleteProductArray(ProductArray& pa) { + delete[] pa.products; + pa.products = nullptr; + pa.count = pa.allocated = 0; +} diff --git a/Week03/Exercise01/Product.h b/Week03/Exercise01/Product.h new file mode 100644 index 0000000..6707bc1 --- /dev/null +++ b/Week03/Exercise01/Product.h @@ -0,0 +1,22 @@ +#ifndef _PRODUCT +#define _PRODUCT + +struct Product { + char name[256]; + unsigned count; + double price; +}; + +struct ProductArray { + Product* products; + unsigned count; + unsigned allocated; + + void add(const Product& product); + Product* find(char* name); +}; + +ProductArray newProductArray(unsigned allocatedSize); +void deleteProductArray(ProductArray& ua); + +#endif 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; +} diff --git a/Week03/Exercise01/Shop.h b/Week03/Exercise01/Shop.h new file mode 100644 index 0000000..af008bb --- /dev/null +++ b/Week03/Exercise01/Shop.h @@ -0,0 +1,18 @@ +#ifndef _SHOP +#define _SHOP + +#include "User.h" +#include "Product.h" + +struct Shop { + UserArray users; + ProductArray products; + User* loggedInUser; + + void runCommand(char command); +}; + +Shop newShop(); +void deleteShop(Shop& shop); + +#endif diff --git a/Week03/Exercise01/User.cpp b/Week03/Exercise01/User.cpp new file mode 100644 index 0000000..0dca648 --- /dev/null +++ b/Week03/Exercise01/User.cpp @@ -0,0 +1,33 @@ +#include <iostream> +#include <cstring> +#include "User.h" + +void UserArray::add(const User& user) { + if (count >= allocated) { + std::cout << "No more users are allowed!" << std::endl; + return; + } + + users[count++] = user; +} + +User* UserArray::find(char* username) { + for (unsigned i = 0; i < count; ++i) + if (strcmp(username, users[i].name) == 0) + return &users[i]; + return nullptr; +} + +UserArray newUserArray(unsigned allocatedSize) { + UserArray ua; + ua.users = new User[allocatedSize]; + ua.count = 0; + ua.allocated = allocatedSize; + return ua; +} + +void deleteUserArray(UserArray& ua) { + delete[] ua.users; + ua.users = nullptr; + ua.count = ua.allocated = 0; +} diff --git a/Week03/Exercise01/User.h b/Week03/Exercise01/User.h new file mode 100644 index 0000000..4802516 --- /dev/null +++ b/Week03/Exercise01/User.h @@ -0,0 +1,22 @@ +#ifndef _USER +#define _USER + +struct User { + char name[256]; + char password[256]; + bool isAdmin; +}; + +struct UserArray { + User* users; + unsigned count; + unsigned allocated; + + void add(const User& user); + User* find(char* username); +}; + +UserArray newUserArray(unsigned allocatedSize); +void deleteUserArray(UserArray& ua); + +#endif diff --git a/Week03/Exercise01/main.cpp b/Week03/Exercise01/main.cpp new file mode 100644 index 0000000..a018dd5 --- /dev/null +++ b/Week03/Exercise01/main.cpp @@ -0,0 +1,25 @@ +#include <iostream> +#include <cstring> +#include "Shop.h" + +int main() { + Shop shop = newShop(); + + User admin; + strcpy(admin.name, "admin"); + strcpy(admin.password, "admin"); + admin.isAdmin = true; + + shop.users.add(admin); + + char command; + while (true) { + std::cin >> command; + std::cin.ignore(); + shop.runCommand(command); + } + + deleteShop(shop); + + return 0; +} |
