From fe6f10b64fca82ad6a6b7f8c0a5a5145ed24a213 Mon Sep 17 00:00:00 2001 From: = Date: Mon, 11 May 2026 12:54:56 +0200 Subject: [PATCH] Feat: Updated playerstatuscontroller and tests --- .../controller/PlayerStatusController.java | 38 ++++----------- .../g40/mappe/model/PlayerStatusTest.java | 46 +++++++++++++++---- 2 files changed, 46 insertions(+), 38 deletions(-) diff --git a/src/main/java/edu/ntnu/idi/idatt2003/g40/mappe/controller/PlayerStatusController.java b/src/main/java/edu/ntnu/idi/idatt2003/g40/mappe/controller/PlayerStatusController.java index aa45ab6..b69af32 100644 --- a/src/main/java/edu/ntnu/idi/idatt2003/g40/mappe/controller/PlayerStatusController.java +++ b/src/main/java/edu/ntnu/idi/idatt2003/g40/mappe/controller/PlayerStatusController.java @@ -3,8 +3,6 @@ import edu.ntnu.idi.idatt2003.g40.mappe.model.Player; import edu.ntnu.idi.idatt2003.g40.mappe.model.PlayerStatus; -import java.math.BigDecimal; - /** * Controls and calculates the players' status. * @@ -20,38 +18,22 @@ private PlayerStatusController() { * *

Calculates what status the player should get * based on players' net worth compared to starting - * money, and current week.

+ * money.

* * @param player the player to give a status to. * * @return a PlayerStatus * */ public static PlayerStatus getStatus(final Player player) { - Integer startingMoneyInt = player.getStartingMoney().intValue(); - - BigDecimal difference = - player.getNetWorth().subtract(player.getStartingMoney()); - - return switch ((Integer) difference.intValue()) { - case Integer val when val >= percentAmount(startingMoneyInt, 900) -> PlayerStatus.OMNIPOTENT; - case Integer val when val >= percentAmount(startingMoneyInt, 400) -> PlayerStatus.SEER; - case Integer val when val >= percentAmount(startingMoneyInt, 200) -> PlayerStatus.PRO; - case Integer val when val >= percentAmount(startingMoneyInt, 100) -> PlayerStatus.TRYHARD; - case Integer val when val >= percentAmount(startingMoneyInt, 50) -> PlayerStatus.GOOD; - case Integer val when val >= percentAmount(startingMoneyInt, 20) -> PlayerStatus.NOVICE; - default -> PlayerStatus.NOOB; - }; - } + float differenceInPercent = ((player.getNetWorth().floatValue() + / player.getStartingMoney().floatValue()) - 1) * 100; - /** - * Helper method for increasing a value by x percent. - * - * @param value the value to calculate from - * @param x the percent amount - * - * @return the increased value. - * */ - private static Integer percentAmount(final Integer value, final Integer x) { - return (value * x) / 100; + for (int i = PlayerStatus.values().length; i > 0; i--) { + PlayerStatus curStatus = PlayerStatus.values()[i - 1]; + if (differenceInPercent >= curStatus.getPercentIncreaseNeeded()) { + return curStatus; + } + } + return PlayerStatus.NOOB; } } diff --git a/src/test/java/edu/ntnu/idi/idatt2003/g40/mappe/model/PlayerStatusTest.java b/src/test/java/edu/ntnu/idi/idatt2003/g40/mappe/model/PlayerStatusTest.java index a9a44f5..1765d46 100644 --- a/src/test/java/edu/ntnu/idi/idatt2003/g40/mappe/model/PlayerStatusTest.java +++ b/src/test/java/edu/ntnu/idi/idatt2003/g40/mappe/model/PlayerStatusTest.java @@ -1,23 +1,49 @@ package edu.ntnu.idi.idatt2003.g40.mappe.model; -import edu.ntnu.idi.idatt2003.g40.mappe.controller.PlayerStatusController; -import org.junit.jupiter.api.Test; +import static org.junit.jupiter.api.Assertions.assertEquals; import java.math.BigDecimal; - -import static org.junit.jupiter.api.Assertions.*; +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Test; class PlayerStatusTest { + /** + * Player object used for testing. + * */ + private Player testPlayer; + + @BeforeEach + void setUp() { + testPlayer = new Player("Player", new BigDecimal(1000)); + } + @Test - void getStatusReturnsProperStatuses() { - Player testPlayer = new Player("Player", new BigDecimal(1000)); - PlayerStatus gottenStatus = PlayerStatusController.getStatus(testPlayer); - assertEquals(PlayerStatus.NOOB, gottenStatus); + void getStatusAtStartReturnsWorstStatus() { + assertEquals(PlayerStatus.NOOB, testPlayer.getStatus()); + } + @Test + void getStatusAfterDoubleIncreaseReturnsBetterStatus() { testPlayer.addMoney(new BigDecimal(1000)); - PlayerStatus gottenStatus2 = PlayerStatusController.getStatus(testPlayer); + assertEquals(PlayerStatus.TRYHARD, testPlayer.getStatus()); + } + + @Test + void gettingStatusWhenNegativeDifferenceReturnsWorstStatus() { + testPlayer.addMoney(new BigDecimal(-1000)); + assertEquals(PlayerStatus.NOOB, testPlayer.getStatus()); + } + + @Test + void multipleChangesInValueCauseCorrectStatus() { + testPlayer.addMoney(new BigDecimal(2000)); + assertEquals(PlayerStatus.PRO, testPlayer.getStatus()); + + testPlayer.addMoney(new BigDecimal(-1000)); + assertEquals(PlayerStatus.TRYHARD, testPlayer.getStatus()); - assertEquals(PlayerStatus.TRYHARD, gottenStatus2); + testPlayer.addMoney(new BigDecimal(-500)); + assertEquals(PlayerStatus.GOOD, testPlayer.getStatus()); } } \ No newline at end of file