Browse Source

added buyItem with test

remotes/origin/navigation
KRUGSON 2 years ago
parent
commit
a18aca76d7
  1. 19
      src/c/player.c
  2. 3
      src/c/player.h
  3. 16
      test/c/test_player.c

19
src/c/player.c

@ -71,6 +71,25 @@ Player removeMoneyFromPlayer(Player actualPlayer, int money)
return actualPlayer; 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 // show
void showInventory(Player actualPlayer) void showInventory(Player actualPlayer)
{ {

3
src/c/player.h

@ -20,6 +20,9 @@ Player setTotal(Player actualPlayer, int value);
Player addMoneyToPlayer(Player actualPlayer, int money); Player addMoneyToPlayer(Player actualPlayer, int money);
Player removeMoneyFromPlayer(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 //show
void showInventory(Player actualPlayer); void showInventory(Player actualPlayer);

16
test/c/test_player.c

@ -111,7 +111,23 @@ void test_removeMoneyFromPlayer(void)
TEST_ASSERT_EQUAL_INT(checkSum, actualPlayer.wallet); 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 #endif // TEST
Loading…
Cancel
Save