test: Added some tests
This commit is contained in:
2
.idea/misc.xml
generated
2
.idea/misc.xml
generated
@@ -8,7 +8,7 @@
|
|||||||
</list>
|
</list>
|
||||||
</option>
|
</option>
|
||||||
</component>
|
</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" />
|
<output url="file://$PROJECT_DIR$/out" />
|
||||||
</component>
|
</component>
|
||||||
</project>
|
</project>
|
||||||
7
pom.xml
7
pom.xml
@@ -21,6 +21,13 @@
|
|||||||
<version>1.18.38</version>
|
<version>1.18.38</version>
|
||||||
<scope>provided</scope>
|
<scope>provided</scope>
|
||||||
</dependency>
|
</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>
|
</dependencies>
|
||||||
|
|
||||||
</project>
|
</project>
|
||||||
@@ -49,11 +49,18 @@ public class Game {
|
|||||||
|
|
||||||
public void build(Item item) {
|
public void build(Item item) {
|
||||||
if (!player.getInventory().contains(item)) {
|
if (!player.getInventory().contains(item)) {
|
||||||
System.out.println(" "); // TODO
|
System.out.println(" never "); // TODO
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
if (item.canBuild(this)) {
|
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);
|
buildings.add(item);
|
||||||
player.getInventory().remove(item);
|
player.getInventory().remove(item);
|
||||||
}
|
}
|
||||||
@@ -75,18 +82,24 @@ public class Game {
|
|||||||
public void harvest(Item item) {
|
public void harvest(Item item) {
|
||||||
switch (item) {
|
switch (item) {
|
||||||
case WHEAT_FARMLAND -> {
|
case WHEAT_FARMLAND -> {
|
||||||
buildings.remove(Item.WHEAT_SEEDS);
|
for (int i = 0; i < waterType.getFarmlandAndVineyardCount() ; i++) {
|
||||||
player.getInventory().add(Item.WHEAT);
|
buildings.remove(Item.WHEAT_SEEDS);
|
||||||
|
player.getInventory().add(Item.WHEAT);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
case HOPS_FARMLAND -> {
|
case HOPS_FARMLAND -> {
|
||||||
buildings.remove(Item.HOPS_SEEDS);
|
for (int i = 0; i < waterType.getFarmlandAndVineyardCount() ; i++) {
|
||||||
player.getInventory().add(Item.HOPS);
|
buildings.remove(Item.HOPS_SEEDS);
|
||||||
|
player.getInventory().add(Item.HOPS);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
case VINEYARD -> {
|
case VINEYARD -> {
|
||||||
buildings.remove(Item.GRAPEVINE_SEEDS);
|
for (int i = 0; i < waterType.getFarmlandAndVineyardCount() ; i++) {
|
||||||
player.getInventory().add(Item.GRAPEVINE);
|
buildings.remove(Item.GRAPEVINE_SEEDS);
|
||||||
|
player.getInventory().add(Item.GRAPEVINE);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -102,24 +115,28 @@ public class Game {
|
|||||||
long coalFactoryProducedMoney = 0;
|
long coalFactoryProducedMoney = 0;
|
||||||
long cheeseFactoryProducedMoney = 0;
|
long cheeseFactoryProducedMoney = 0;
|
||||||
|
|
||||||
|
if (!buildings.contains(item)) {
|
||||||
|
System.out.println(" cant produce"); //TODO
|
||||||
|
return;
|
||||||
|
}
|
||||||
switch (item) {
|
switch (item) {
|
||||||
case BAKERY -> {
|
case BAKERY -> {
|
||||||
long wheatSum = player.getInventory().stream().filter(item1 -> item1 == Item.WHEAT).count();
|
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);
|
bakeryProducedMoney = (wheatSum * item.getPrice()/2);
|
||||||
player.setCoins(player.getCoins() + bakeryProducedMoney);
|
player.setCoins(player.getCoins() + bakeryProducedMoney);
|
||||||
}
|
}
|
||||||
|
|
||||||
case PUB -> {
|
case PUB -> {
|
||||||
long hopsSum = player.getInventory().stream().filter(item1 -> item1 == Item.HOPS).count();
|
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);
|
pubProducedMoney = (hopsSum * item.getPrice()/2);
|
||||||
player.setCoins(player.getCoins() + pubProducedMoney);
|
player.setCoins(player.getCoins() + pubProducedMoney);
|
||||||
}
|
}
|
||||||
|
|
||||||
case WINERY -> {
|
case WINERY -> {
|
||||||
long grapevineSum = player.getInventory().stream().filter(item1 -> item1 == Item.GRAPEVINE).count();
|
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);
|
wineryProducedMoney = (grapevineSum * item.getPrice()/2);
|
||||||
player.setCoins(player.getCoins() + wineryProducedMoney);
|
player.setCoins(player.getCoins() + wineryProducedMoney);
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -6,7 +6,8 @@ public class Main {
|
|||||||
public static void main(String[] args) {
|
public static void main(String[] args) {
|
||||||
Game game = new Game();
|
Game game = new Game();
|
||||||
game.generateStats();
|
game.generateStats();
|
||||||
System.out.println(game.getPlayer().getCoins());
|
System.out.println("coins: " + game.getPlayer().getCoins());
|
||||||
|
System.out.println();
|
||||||
|
|
||||||
try {
|
try {
|
||||||
game.getMarket().buyItem(game, Item.CHURCH);
|
game.getMarket().buyItem(game, Item.CHURCH);
|
||||||
@@ -14,9 +15,33 @@ public class Main {
|
|||||||
} catch (ItemNotAvailableException e) {
|
} catch (ItemNotAvailableException e) {
|
||||||
throw new RuntimeException(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 {
|
try {
|
||||||
game.getMarket().buyItem(game, Item.WHEAT_FARMLAND);
|
game.getMarket().buyItem(game, Item.WHEAT_FARMLAND);
|
||||||
@@ -24,8 +49,59 @@ public class Main {
|
|||||||
} catch (ItemNotAvailableException e) {
|
} catch (ItemNotAvailableException e) {
|
||||||
throw new RuntimeException(e);
|
throw new RuntimeException(e);
|
||||||
}
|
}
|
||||||
System.out.println(game.getPlayer().getInventory());
|
System.out.println("coins: " + game.getPlayer().getCoins());
|
||||||
System.out.println(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();
|
||||||
|
|
||||||
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -0,0 +1,7 @@
|
|||||||
|
package cz.jull.exceptions;
|
||||||
|
|
||||||
|
public class NotSufficientsCoinsException extends RuntimeException {
|
||||||
|
public NotSufficientsCoinsException(String message) {
|
||||||
|
super(message);
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -4,6 +4,7 @@ import cz.jull.Game;
|
|||||||
import cz.jull.Item;
|
import cz.jull.Item;
|
||||||
import cz.jull.Player;
|
import cz.jull.Player;
|
||||||
import cz.jull.exceptions.ItemNotAvailableException;
|
import cz.jull.exceptions.ItemNotAvailableException;
|
||||||
|
import cz.jull.exceptions.NotSufficientsCoinsException;
|
||||||
import cz.jull.surroundings.PathType;
|
import cz.jull.surroundings.PathType;
|
||||||
import cz.jull.surroundings.SoilType;
|
import cz.jull.surroundings.SoilType;
|
||||||
import cz.jull.surroundings.WaterType;
|
import cz.jull.surroundings.WaterType;
|
||||||
@@ -22,7 +23,11 @@ public class Market {
|
|||||||
player.getInventory().remove(item);
|
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)) {
|
if (!item.getCanBeBoughtFunction().apply(game)) {
|
||||||
System.out.println(" "); //TODO
|
System.out.println(" "); //TODO
|
||||||
return;
|
return;
|
||||||
|
|||||||
14
src/test/java/cz/jull/NotSufficientCoinsTest.java
Normal file
14
src/test/java/cz/jull/NotSufficientCoinsTest.java
Normal 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));
|
||||||
|
}
|
||||||
|
}
|
||||||
Reference in New Issue
Block a user