From d8c2a249303c79d4e9246c585853a352934254a0 Mon Sep 17 00:00:00 2001 From: KRUGSON Date: Sun, 5 Feb 2023 03:35:36 +0100 Subject: [PATCH] added shop function --- src/c/shop.c | 49 +++++++++++++++++++++++++++++++++++++++++++++++++ src/c/shop.h | 8 ++++++++ 2 files changed, 57 insertions(+) create mode 100644 src/c/shop.c create mode 100644 src/c/shop.h diff --git a/src/c/shop.c b/src/c/shop.c new file mode 100644 index 0000000..4f871aa --- /dev/null +++ b/src/c/shop.c @@ -0,0 +1,49 @@ +#include +#include +#include +#include + +#include "shop.h" + +int openShop(Item *availableItems) +{ + int userInput; + fflush(stdout); + bool shopIsOpen = 1; + while (shopIsOpen == 1) + { + printf("*** SHOP-Items *** \n\n"); + + // printf("%-5s %-30s %5s\n", "Index", "Name", "Price"); + for (int i = 0; i < 6; i++) + { + printf("%-5d %-40s %5d$\n", availableItems[i].id, availableItems[i].itemName, availableItems[i].price); + } + + printf("\n-> to buy items type '[index of item]' \n-> write '0' to quit the shop'\n\n"); + + while (scanf("%d", &userInput) != 1) + { + printf("Invalid [index]. Please try again: "); + fflush(stdout); + } + + if (userInput > 0) + { + return userInput; + // BUY ITEM added later -> addItemToInventory(userInput); + } + else if (userInput == 0) + { + shopIsOpen = 0; + printf("Enjoy your items, have a great day!\n"); + return 0; + } + else + { + printf("Invalid [index]. Please try again: "); + } + } + + return 0; +} \ No newline at end of file diff --git a/src/c/shop.h b/src/c/shop.h new file mode 100644 index 0000000..24a390b --- /dev/null +++ b/src/c/shop.h @@ -0,0 +1,8 @@ +#ifndef SHOP_H +#define SHOP_H + +#include "items.h" + +int openShop(Item *availableItems); + +#endif \ No newline at end of file