diff --git a/src/main/java/task_5/BattleException.java b/src/main/java/task_5/BattleException.java new file mode 100644 index 0000000..5936c2c --- /dev/null +++ b/src/main/java/task_5/BattleException.java @@ -0,0 +1,7 @@ +package task_5; + +public class BattleException extends RuntimeException { + public BattleException(String message) { + super(message); + } +} diff --git a/src/main/java/task_5/BattleSimulator.java b/src/main/java/task_5/BattleSimulator.java new file mode 100644 index 0000000..41e6c86 --- /dev/null +++ b/src/main/java/task_5/BattleSimulator.java @@ -0,0 +1,49 @@ +package task_5; + +import lombok.extern.slf4j.Slf4j; + +import java.util.ArrayList; +import java.util.Map; +import java.util.Random; +@Slf4j +public class BattleSimulator { + public static void main(String[] args) { + BattleSimulator battleSimulator = new BattleSimulator(); + Hero hero = new Hero(); + hero.setHeroName("Ivan"); + hero.setCurrentMana(50); + hero.setInventory(new ArrayList<>()); + hero.getInventory().add(new MagicItem("Bluster1", 10, 10)); + hero.getInventory().add(new MagicItem("Bluster2", 15, 5)); + hero.getInventory().add(new MagicItem("Bluster3", 14, 20)); + hero.getInventory().add(new MagicItem("Bluster4", 16, 13)); + hero.getInventory().add(new MagicItem("Bluster5", 6, 14)); + try { + battleSimulator.simulateBattle(hero, 10); + } catch (RuntimeException e) { + e.printStackTrace(); + } + } + + public void simulateBattle(Hero hero, int numTurns) { + if (hero.getInventory()==null || hero.getInventory().isEmpty()) { + throw new NoItemsException(hero.getHeroName() + " no have inventary"); + } + Random rnd = new Random(); + int randomNumber; + for (int i = 0; i < numTurns; i++) { + try { + Thread.sleep(2000); + } catch (InterruptedException e) { + log.info(e.getStackTrace()); + } + try { + randomNumber = rnd.nextInt(hero.getInventory().size()); + hero.useItem(hero.getInventory().get(randomNumber)); + } catch (RuntimeException e) { + e.printStackTrace(); + } + + } + } +} diff --git a/src/main/java/task_5/Hero.java b/src/main/java/task_5/Hero.java new file mode 100644 index 0000000..6303955 --- /dev/null +++ b/src/main/java/task_5/Hero.java @@ -0,0 +1,61 @@ +package task_5; + +import java.util.HashMap; +import java.util.List; +import java.util.Map; + +public class Hero { + private String heroName; + private int currentMana; + private List inventory; + private Map itemUsages = new HashMap<>(); + + public Hero() { + } + + public Hero(String heroName, int currentMana, List inventory) { + this.heroName = heroName; + this.currentMana = currentMana; + this.inventory = inventory; + } + + public void useItem(MagicItem magicItem) { + if (!inventory.contains(magicItem)) { + throw new BattleException(heroName + " dont have " + magicItem.getItemName()); + } + if (magicItem.getManaCost() > currentMana) { + throw new BattleException(heroName + " dont have mana to use " + magicItem.getItemName()); + } + long l = itemUsages.get(magicItem.getItemName()); + if ((System.currentTimeMillis() - l) < magicItem.getCooldown()) { + throw new BattleException(heroName + " dont have opportunity to use " + magicItem.getItemName() + " because its dont restore after use"); + } + currentMana -= magicItem.getManaCost(); + itemUsages.put(magicItem.getItemName(), System.currentTimeMillis()); + System.out.println(heroName + " use " + magicItem.getItemName()); + } + + public String getHeroName() { + return heroName; + } + + public void setHeroName(String heroName) { + this.heroName = heroName; + } + + public int getCurrentMana() { + return currentMana; + } + + public void setCurrentMana(int currentMana) { + this.currentMana = currentMana; + } + + public List getInventory() { + return inventory; + } + + public void setInventory(List inventory) { + this.inventory = inventory; + } +} diff --git a/src/main/java/task_5/MagicItem.java b/src/main/java/task_5/MagicItem.java new file mode 100644 index 0000000..2f00885 --- /dev/null +++ b/src/main/java/task_5/MagicItem.java @@ -0,0 +1,49 @@ +package task_5; + +public class MagicItem { + private String itemName; + private int manaCost; + private int cooldown; + + public MagicItem() { + } + + public MagicItem(String itemName, int manaCost, int cooldown) { + setItemName(itemName); + setManaCost(manaCost); + setCooldown(cooldown); + } + + public String getItemName() { + return itemName; + } + + public void setItemName(String itemName) { + if (itemName.isBlank()) { + throw new IllegalArgumentException("name not be empty"); + } + this.itemName = itemName; + } + + public int getManaCost() { + return manaCost; + } + + public void setManaCost(int manaCost) { + if (manaCost<=0) { + throw new IllegalArgumentException("mana cost must be ,ore than 0"); + } + this.manaCost = manaCost; + } + + public int getCooldown() { + return cooldown; + } + + public void setCooldown(int cooldown) { + if (cooldown<=0) { + throw new IllegalArgumentException("cool down must be ,ore than 0"); + } + this.cooldown = cooldown; + } +} diff --git a/src/main/java/task_5/NoItemsException.java b/src/main/java/task_5/NoItemsException.java new file mode 100644 index 0000000..a83bc15 --- /dev/null +++ b/src/main/java/task_5/NoItemsException.java @@ -0,0 +1,7 @@ +package task_5; + +public class NoItemsException extends RuntimeException { + public NoItemsException(String message) { + super(message); + } +}