diff --git a/src/main/java/cz/jull/Game.java b/src/main/java/cz/jull/Game.java index 3971f6d..84753e5 100644 --- a/src/main/java/cz/jull/Game.java +++ b/src/main/java/cz/jull/Game.java @@ -28,13 +28,18 @@ public class Game { @Getter private final List buildings = new ArrayList<>(); + @Getter private final Market market = new Market(this); public void generateStats() { forestType = ForestType.getRandom(); + System.out.println("forest: " + forestType); soilType = SoilType.getRandom(); + System.out.println("soil: " + soilType); pathType = PathType.getRandom(); + System.out.println("path: " + pathType); waterType = WaterType.getRandom(); + System.out.println("water: " + waterType); if (waterType == WaterType.WELL) { buildings.add(Item.WELL); diff --git a/src/main/java/cz/jull/Item.java b/src/main/java/cz/jull/Item.java index 2dc23e8..4a2d9ba 100644 --- a/src/main/java/cz/jull/Item.java +++ b/src/main/java/cz/jull/Item.java @@ -19,9 +19,9 @@ public enum Item implements Buildable { HOPS_SEEDS("Hops seeds", true, 5), GRAPEVINE_SEEDS("Grapes seeds", true, 5), - VINEYARD("Vineyard", true, 100), - WHEAT_FARMLAND("Wheat farmland", true, 100), - HOPS_FARMLAND("Hops farmland", true, 100), + VINEYARD("Vineyard", true, 7), + WHEAT_FARMLAND("Wheat farmland", true, 7), + HOPS_FARMLAND("Hops farmland", true, 7), FENCE_WITH_COWS("Fence with cows", true, 100), @@ -39,7 +39,7 @@ public enum Item implements Buildable { static { - WHEAT_SEEDS.function = game -> { + WHEAT_SEEDS.canBeBuiltFunction = game -> { if (game.getBuildings().stream().filter(building -> building == Item.WHEAT_FARMLAND).count() >= game.getBuildings().stream().filter(building -> building == Item.WHEAT_SEEDS || building == Item.HOPS_SEEDS).count() + 1) { return true; } @@ -47,7 +47,7 @@ public enum Item implements Buildable { return false; }; - HOPS_SEEDS.function = game -> { + HOPS_SEEDS.canBeBuiltFunction = game -> { if (game.getBuildings().stream().filter(building -> building == Item.WHEAT_FARMLAND).count() >= game.getBuildings().stream().filter(building -> building == Item.WHEAT_SEEDS || building == Item.HOPS_SEEDS).count() + 1) { return true; } @@ -55,7 +55,7 @@ public enum Item implements Buildable { return false; }; - GRAPEVINE_SEEDS.function = game -> { + GRAPEVINE_SEEDS.canBeBuiltFunction = game -> { if (game.getBuildings().stream().filter(building -> building == Item.VINEYARD).count() >= game.getBuildings().stream().filter(building -> building == Item.GRAPEVINE_SEEDS || building == Item.HOPS_SEEDS).count() + 1) { return true; } @@ -63,19 +63,7 @@ public enum Item implements Buildable { return false; }; - VINEYARD.function = game -> { - if (game.getBuildings().stream().filter(building -> building == Item.VINEYARD).count() >= game.getWaterType().getFarmlandAndVineyardCount()) { - return true; - } - System.out.println(" "); //TODO - return false; - }; - - WHEAT_FARMLAND.function = game -> { - if (!game.getBuildings().contains(Item.VILLAGER_HOUSE)) { - System.out.println(" "); // TODO - return false; - } + WHEAT_FARMLAND.canBeBuiltFunction = game -> { if (game.getBuildings().stream().filter(building -> building == Item.WHEAT_FARMLAND).count() >= game.getWaterType().getFarmlandAndVineyardCount()) { System.out.println(" "); // TODO return false; @@ -83,7 +71,23 @@ public enum Item implements Buildable { return true; }; - FENCE_WITH_COWS.function = game -> { + VINEYARD.canBeBuiltFunction = game -> { + if (game.getBuildings().stream().filter(building -> building == Item.VINEYARD).count() >= game.getWaterType().getFarmlandAndVineyardCount()) { + return true; + } + System.out.println(" "); //TODO + return false; + }; + + HOPS_FARMLAND.canBeBuiltFunction = game -> { + if (game.getBuildings().stream().filter(building -> building == Item.HOPS_FARMLAND).count() >= game.getWaterType().getFarmlandAndVineyardCount()) { + return true; + } + System.out.println(" "); //TODO + return false; + }; + + FENCE_WITH_COWS.canBeBuiltFunction = game -> { if (game.getBuildings().stream().filter(building -> building == Item.VILLAGER_HOUSE).count() >= (game.getBuildings().stream().filter(building -> building == Item.FENCE_WITH_COWS).count() + 1) * 3) { return true; } @@ -91,7 +95,7 @@ public enum Item implements Buildable { return false; }; - CHURCH.function = game -> { + CHURCH.canBeBuiltFunction = game -> { if (!game.getBuildings().contains(Item.CHURCH)) { return true; } @@ -99,7 +103,7 @@ public enum Item implements Buildable { return false; }; - VILLAGER_HOUSE.function = game -> { + VILLAGER_HOUSE.canBeBuiltFunction = game -> { if (game.getBuildings().contains(Item.CHURCH)) { return true; } @@ -107,12 +111,7 @@ public enum Item implements Buildable { return false; }; - BAKERY.function = game -> { - if (!game.getBuildings().contains(Item.VILLAGER_HOUSE)) { - System.out.println(" "); // TODO - return false; - } - + BAKERY.canBeBuiltFunction = game -> { if (!game.getBuildings().contains(Item.WHEAT_SEEDS)) { System.out.println(" "); // TODO return false; @@ -120,12 +119,7 @@ public enum Item implements Buildable { return true; }; - PUB.function = game -> { - if (!game.getBuildings().contains(Item.VILLAGER_HOUSE)) { - System.out.println(" "); // TODO - return false; - } - + PUB.canBeBuiltFunction = game -> { if (!game.getBuildings().contains(Item.HOPS_SEEDS)) { System.out.println(" "); // TODO return false; @@ -133,12 +127,7 @@ public enum Item implements Buildable { return true; }; - WINERY.function = game -> { - if (!game.getBuildings().contains(Item.VILLAGER_HOUSE)) { - System.out.println(" "); // TODO - return false; - } - + WINERY.canBeBuiltFunction = game -> { if (!game.getBuildings().contains(Item.GRAPEVINE_SEEDS)) { System.out.println(" "); // TODO return false; @@ -146,7 +135,25 @@ public enum Item implements Buildable { return true; }; - COAL_FACTORY.function = game -> { + CHEESE_FACTORY.canBeBuiltFunction = game -> { + if (!game.getBuildings().contains(Item.FENCE_WITH_COWS)) { + System.out.println(" "); // TODO + return false; + } + return true; + }; + } + + static { + VILLAGER_HOUSE.canBeBoughtFunction = game -> { + if (!game.getBuildings().contains(Item.CHURCH)) { + System.out.println(" "); //TODO + return false; + } + return true; + }; + + Function func = game -> { if (!game.getBuildings().contains(Item.VILLAGER_HOUSE)) { System.out.println(" "); // TODO return false; @@ -154,13 +161,18 @@ public enum Item implements Buildable { return true; }; - CHEESE_FACTORY.function = game -> { - if (!game.getBuildings().contains(Item.VILLAGER_HOUSE)) { - System.out.println(" "); // TODO - return false; - } - return true; - }; + WHEAT_FARMLAND.canBeBoughtFunction = func; + HOPS_FARMLAND.canBeBoughtFunction = func; + VINEYARD.canBeBoughtFunction = func; + + FENCE_WITH_COWS.canBeBoughtFunction = func; + + BAKERY.canBeBoughtFunction = func; + PUB.canBeBoughtFunction = func; + WINERY.canBeBoughtFunction = func; + + COAL_FACTORY.canBeBoughtFunction = func; + CHEESE_FACTORY.canBeBoughtFunction = func; } @Getter @@ -171,13 +183,17 @@ public enum Item implements Buildable { @Getter private final int price; - private Function function; + private Function canBeBuiltFunction; + + @Getter + private Function canBeBoughtFunction; Item(String name, boolean isPLaceable, int price) { this.name = name; this.isPLaceable = isPLaceable; - this.function = ignored -> isPLaceable; + this.canBeBuiltFunction = ignored -> isPLaceable; this.price = price; + this.canBeBoughtFunction = ignored -> true; } @Override @@ -185,6 +201,11 @@ public enum Item implements Buildable { if (!isPLaceable) { return false; } - return function.apply(game); + return canBeBuiltFunction.apply(game); + } + + @Override + public String toString() { + return name; } } diff --git a/src/main/java/cz/jull/Main.java b/src/main/java/cz/jull/Main.java index d419f6a..e468001 100644 --- a/src/main/java/cz/jull/Main.java +++ b/src/main/java/cz/jull/Main.java @@ -1,7 +1,31 @@ package cz.jull; +import cz.jull.exceptions.ItemNotAvailableException; + public class Main { public static void main(String[] args) { + Game game = new Game(); + game.generateStats(); + System.out.println(game.getPlayer().getCoins()); + + try { + game.getMarket().buyItem(game, Item.CHURCH); + + } catch (ItemNotAvailableException e) { + throw new RuntimeException(e); + } + System.out.println(game.getPlayer().getCoins()); + + System.out.println(game.getPlayer().getInventory()); + + try { + game.getMarket().buyItem(game, Item.WHEAT_FARMLAND); + + } catch (ItemNotAvailableException e) { + throw new RuntimeException(e); + } + System.out.println(game.getPlayer().getInventory()); + System.out.println(game.getPlayer().getCoins()); } } \ No newline at end of file diff --git a/src/main/java/cz/jull/exceptions/ItemNotAvailableException.java b/src/main/java/cz/jull/exceptions/ItemNotAvailableException.java new file mode 100644 index 0000000..1e5254c --- /dev/null +++ b/src/main/java/cz/jull/exceptions/ItemNotAvailableException.java @@ -0,0 +1,7 @@ +package cz.jull.exceptions; + +public class ItemNotAvailableException extends RuntimeException { + public ItemNotAvailableException(String message) { + super(message); + } +} diff --git a/src/main/java/cz/jull/market/Market.java b/src/main/java/cz/jull/market/Market.java index f24fffb..1b5aca1 100644 --- a/src/main/java/cz/jull/market/Market.java +++ b/src/main/java/cz/jull/market/Market.java @@ -3,6 +3,7 @@ package cz.jull.market; import cz.jull.Game; import cz.jull.Item; import cz.jull.Player; +import cz.jull.exceptions.ItemNotAvailableException; import cz.jull.surroundings.PathType; import cz.jull.surroundings.SoilType; import cz.jull.surroundings.WaterType; @@ -21,53 +22,23 @@ public class Market { player.getInventory().remove(item); } - public void buyItem(Game game, Item item) throws Exception { + public void buyItem(Game game, Item item) throws ItemNotAvailableException { + if (!item.getCanBeBoughtFunction().apply(game)) { + System.out.println(" "); //TODO + return; + } if (availableItems.contains(item)) { game.getPlayer().getInventory().add(item); game.getPlayer().setCoins(game.getPlayer().getCoins() - item.getPrice()); } else { - if (item == Item.WHEAT_SEEDS) { + 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 < game.getWaterType().getFarmlandAndVineyardCount() ; i++) { - game.getPlayer().getInventory().add(Item.WHEAT_SEEDS); + game.getPlayer().getInventory().add(item); + game.getPlayer().setCoins(game.getPlayer().getCoins() - item.getPrice()); } return; } - - if (item == Item.HOPS_SEEDS) { - for (int i = 0; i < game.getWaterType().getFarmlandAndVineyardCount() ; i++) { - game.getPlayer().getInventory().add(Item.HOPS_SEEDS); - } - return; - } - - if (item == Item.GRAPEVINE_SEEDS) { - for (int i = 0; i < game.getWaterType().getFarmlandAndVineyardCount() ; i++) { - game.getPlayer().getInventory().add(Item.GRAPEVINE_SEEDS); - } - return; - } - - if (item == Item.WHEAT_FARMLAND) { - for (int i = 0; i < game.getWaterType().getFarmlandAndVineyardCount() ; i++) { - game.getPlayer().getInventory().add(Item.WHEAT_FARMLAND); - } - return; - } - - if (item == Item.HOPS_FARMLAND) { - for (int i = 0; i < game.getWaterType().getFarmlandAndVineyardCount() ; i++) { - game.getPlayer().getInventory().add(Item.HOPS_FARMLAND); - } - return; - } - - if (item == Item.VINEYARD) { - for (int i = 0; i < game.getWaterType().getFarmlandAndVineyardCount() ; i++) { - game.getPlayer().getInventory().add(Item.VINEYARD); - } - return; - } - throw new Exception("Item is not available"); + throw new ItemNotAvailableException("Item is not available"); } }