test: Added some tests

This commit is contained in:
2025-05-09 21:04:18 +02:00
parent 65fd2f11e6
commit a5599d80fc
7 changed files with 143 additions and 17 deletions

2
.idea/misc.xml generated
View File

@@ -8,7 +8,7 @@
</list>
</option>
</component>
<component name="ProjectRootManager" version="2" languageLevel="JDK_24" default="true" project-jdk-name="openjdk-24" project-jdk-type="JavaSDK">
<component name="ProjectRootManager" version="2" languageLevel="JDK_24" default="true" project-jdk-name="24" project-jdk-type="JavaSDK">
<output url="file://$PROJECT_DIR$/out" />
</component>
</project>

View File

@@ -21,6 +21,13 @@
<version>1.18.38</version>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>org.junit.jupiter</groupId>
<artifactId>junit-jupiter-api</artifactId>
<version>5.8.2</version> <!-- Choose the latest version -->
<scope>test</scope>
</dependency>
</dependencies>
</project>

View File

@@ -49,11 +49,18 @@ public class Game {
public void build(Item item) {
if (!player.getInventory().contains(item)) {
System.out.println(" "); // TODO
System.out.println(" never "); // TODO
return;
}
if (item.canBuild(this)) {
if (List.of(Item.WHEAT_SEEDS, Item.HOPS_SEEDS, Item.GRAPEVINE_SEEDS, Item.WHEAT_FARMLAND, Item.HOPS_FARMLAND, Item.VINEYARD).contains(item)) {
for (int i = 0; i < waterType.getFarmlandAndVineyardCount() ; i++) {
buildings.add(item);
player.getInventory().remove(item);
}
return;
}
buildings.add(item);
player.getInventory().remove(item);
}
@@ -75,18 +82,24 @@ public class Game {
public void harvest(Item item) {
switch (item) {
case WHEAT_FARMLAND -> {
buildings.remove(Item.WHEAT_SEEDS);
player.getInventory().add(Item.WHEAT);
for (int i = 0; i < waterType.getFarmlandAndVineyardCount() ; i++) {
buildings.remove(Item.WHEAT_SEEDS);
player.getInventory().add(Item.WHEAT);
}
}
case HOPS_FARMLAND -> {
buildings.remove(Item.HOPS_SEEDS);
player.getInventory().add(Item.HOPS);
for (int i = 0; i < waterType.getFarmlandAndVineyardCount() ; i++) {
buildings.remove(Item.HOPS_SEEDS);
player.getInventory().add(Item.HOPS);
}
}
case VINEYARD -> {
buildings.remove(Item.GRAPEVINE_SEEDS);
player.getInventory().add(Item.GRAPEVINE);
for (int i = 0; i < waterType.getFarmlandAndVineyardCount() ; i++) {
buildings.remove(Item.GRAPEVINE_SEEDS);
player.getInventory().add(Item.GRAPEVINE);
}
}
}
}
@@ -102,24 +115,28 @@ public class Game {
long coalFactoryProducedMoney = 0;
long cheeseFactoryProducedMoney = 0;
if (!buildings.contains(item)) {
System.out.println(" cant produce"); //TODO
return;
}
switch (item) {
case BAKERY -> {
long wheatSum = player.getInventory().stream().filter(item1 -> item1 == Item.WHEAT).count();
player.getInventory().remove(Item.WHEAT);
player.getInventory().removeIf(i -> i == Item.WHEAT);
bakeryProducedMoney = (wheatSum * item.getPrice()/2);
player.setCoins(player.getCoins() + bakeryProducedMoney);
}
case PUB -> {
long hopsSum = player.getInventory().stream().filter(item1 -> item1 == Item.HOPS).count();
player.getInventory().remove(Item.HOPS);
player.getInventory().removeIf(i -> i == Item.HOPS);
pubProducedMoney = (hopsSum * item.getPrice()/2);
player.setCoins(player.getCoins() + pubProducedMoney);
}
case WINERY -> {
long grapevineSum = player.getInventory().stream().filter(item1 -> item1 == Item.GRAPEVINE).count();
player.getInventory().remove(Item.GRAPEVINE);
player.getInventory().removeIf(i -> i == Item.GRAPEVINE);
wineryProducedMoney = (grapevineSum * item.getPrice()/2);
player.setCoins(player.getCoins() + wineryProducedMoney);
}

View File

@@ -6,7 +6,8 @@ public class Main {
public static void main(String[] args) {
Game game = new Game();
game.generateStats();
System.out.println(game.getPlayer().getCoins());
System.out.println("coins: " + game.getPlayer().getCoins());
System.out.println();
try {
game.getMarket().buyItem(game, Item.CHURCH);
@@ -14,9 +15,33 @@ public class Main {
} catch (ItemNotAvailableException e) {
throw new RuntimeException(e);
}
System.out.println(game.getPlayer().getCoins());
System.out.println("coins: " + game.getPlayer().getCoins());
System.out.println("inventory: " + game.getPlayer().getInventory());
System.out.println();
System.out.println(game.getPlayer().getInventory());
game.build(Item.CHURCH);
System.out.println("coins: " + game.getPlayer().getCoins());
System.out.println("inventory: " + game.getPlayer().getInventory());
System.out.println("buildings: " + game.getBuildings());
System.out.println();
try {
game.getMarket().buyItem(game, Item.VILLAGER_HOUSE);
} catch (ItemNotAvailableException e) {
throw new RuntimeException(e);
}
System.out.println("coins: " + game.getPlayer().getCoins());
System.out.println("inventory: " + game.getPlayer().getInventory());
System.out.println();
game.build(Item.VILLAGER_HOUSE);
System.out.println("coins: " + game.getPlayer().getCoins());
System.out.println("inventory: " + game.getPlayer().getInventory());
System.out.println("buildings: " + game.getBuildings());
System.out.println();
try {
game.getMarket().buyItem(game, Item.WHEAT_FARMLAND);
@@ -24,8 +49,59 @@ public class Main {
} catch (ItemNotAvailableException e) {
throw new RuntimeException(e);
}
System.out.println(game.getPlayer().getInventory());
System.out.println(game.getPlayer().getCoins());
System.out.println("coins: " + game.getPlayer().getCoins());
System.out.println("inventory: " + game.getPlayer().getInventory());
System.out.println();
game.build(Item.WHEAT_FARMLAND);
System.out.println("coins: " + game.getPlayer().getCoins());
System.out.println("inventory: " + game.getPlayer().getInventory());
System.out.println("buildings: " + game.getBuildings());
System.out.println();
try {
game.getMarket().buyItem(game, Item.WHEAT_SEEDS);
} catch (ItemNotAvailableException e) {
throw new RuntimeException(e);
}
System.out.println("coins: " + game.getPlayer().getCoins());
System.out.println("inventory: " + game.getPlayer().getInventory());
System.out.println();
game.build(Item.WHEAT_SEEDS);
System.out.println("coins: " + game.getPlayer().getCoins());
System.out.println("inventory: " + game.getPlayer().getInventory());
System.out.println("buildings: " + game.getBuildings());
System.out.println();
game.harvest(Item.WHEAT_FARMLAND);
System.out.println("coins: " + game.getPlayer().getCoins());
System.out.println("inventory: " + game.getPlayer().getInventory());
System.out.println("buildings: " + game.getBuildings());
System.out.println();
try {
game.getMarket().buyItem(game, Item.BAKERY);
} catch (ItemNotAvailableException e) {
System.out.println(" cant buy bakery");
}
System.out.println("coins: " + game.getPlayer().getCoins());
System.out.println("inventory: " + game.getPlayer().getInventory());
System.out.println("buildings: " + game.getBuildings());
System.out.println();
game.produce(Item.BAKERY);
System.out.println("coins: " + game.getPlayer().getCoins());
System.out.println("inventory: " + game.getPlayer().getInventory());
System.out.println("buildings: " + game.getBuildings());
System.out.println();
}
}

View File

@@ -0,0 +1,7 @@
package cz.jull.exceptions;
public class NotSufficientsCoinsException extends RuntimeException {
public NotSufficientsCoinsException(String message) {
super(message);
}
}

View File

@@ -4,6 +4,7 @@ import cz.jull.Game;
import cz.jull.Item;
import cz.jull.Player;
import cz.jull.exceptions.ItemNotAvailableException;
import cz.jull.exceptions.NotSufficientsCoinsException;
import cz.jull.surroundings.PathType;
import cz.jull.surroundings.SoilType;
import cz.jull.surroundings.WaterType;
@@ -22,7 +23,11 @@ public class Market {
player.getInventory().remove(item);
}
public void buyItem(Game game, Item item) throws ItemNotAvailableException {
public void buyItem(Game game, Item item) throws ItemNotAvailableException, NotSufficientsCoinsException {
if (game.getPlayer().getCoins() < item.getPrice()) {
throw new NotSufficientsCoinsException("Not enough coins");
}
if (!item.getCanBeBoughtFunction().apply(game)) {
System.out.println(" "); //TODO
return;

View File

@@ -0,0 +1,14 @@
package cz.jull;
import cz.jull.exceptions.NotSufficientsCoinsException;
import org.junit.jupiter.api.Test;
import static org.junit.jupiter.api.Assertions.*;
public class NotSufficientCoinsTest {
@Test
public void notSufficientCoins() {
Game game = new Game();
game.getPlayer().setCoins(1);
assertThrows(NotSufficientsCoinsException.class, () -> game.getMarket().buyItem(game, Item.AXE));
}
}