From a18aca76d7403207b9c800d60d7ca482780519ba Mon Sep 17 00:00:00 2001 From: KRUGSON Date: Sun, 5 Feb 2023 03:48:32 +0100 Subject: [PATCH] added buyItem with test --- src/c/player.c | 19 +++++++++++++++++++ src/c/player.h | 3 +++ test/c/test_player.c | 16 ++++++++++++++++ 3 files changed, 38 insertions(+) diff --git a/src/c/player.c b/src/c/player.c index f59881b..fea8f91 100644 --- a/src/c/player.c +++ b/src/c/player.c @@ -71,6 +71,25 @@ Player removeMoneyFromPlayer(Player actualPlayer, int money) return actualPlayer; } +//add and remove items with currency +Player buyItem(Item *availableItems, int itemIndex, Player actualPlayer) +{ + int itemPrice = availableItems[itemIndex - 1].price; + if (actualPlayer.wallet >= itemPrice) + { + actualPlayer = addItemToInventory(availableItems, itemIndex, actualPlayer); + actualPlayer = removeMoneyFromPlayer(actualPlayer, itemPrice); + printf("You bought item for %d$! Your balance is now: %d$\n", itemPrice, actualPlayer.wallet); + return actualPlayer; + } + else + { + printf("You don't have enough money.\n\n"); + return actualPlayer; + } +} + + // show void showInventory(Player actualPlayer) { diff --git a/src/c/player.h b/src/c/player.h index 1283b70..411c2cf 100644 --- a/src/c/player.h +++ b/src/c/player.h @@ -20,6 +20,9 @@ Player setTotal(Player actualPlayer, int value); Player addMoneyToPlayer(Player actualPlayer, int money); Player removeMoneyFromPlayer(Player actualPlayer, int money); +//add and remove items with currency +Player buyItem(Item *availableItems, int itemIndex, Player actualPlayer); + //show void showInventory(Player actualPlayer); diff --git a/test/c/test_player.c b/test/c/test_player.c index d0de9ef..2bd5683 100644 --- a/test/c/test_player.c +++ b/test/c/test_player.c @@ -111,7 +111,23 @@ void test_removeMoneyFromPlayer(void) TEST_ASSERT_EQUAL_INT(checkSum, actualPlayer.wallet); } +void test_buyItem(void) +{ + /* arrange */ + // Hier die Werte eingeben/deklarieren + int itemIndex = 2; // price = 50 + int checkSum = actualPlayer.wallet - availableItems[itemIndex-1].price; + + /* act */ + // Die Funktion wird ausgeführt + actualPlayer = buyItem(availableItems, itemIndex, actualPlayer); + /* assert */ + // Vergleichen mit Inhalt von game.Map File + TEST_ASSERT_EQUAL_INT(itemIndex, actualPlayer.itemInventory[0].id); //check if item has right ID + TEST_ASSERT_EQUAL_INT(checkSum, actualPlayer.wallet); //check money after transfer + +} #endif // TEST