test: Added some tests
This commit is contained in:
@@ -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);
|
||||
}
|
||||
|
||||
@@ -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();
|
||||
|
||||
|
||||
}
|
||||
}
|
||||
@@ -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.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;
|
||||
|
||||
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