From 0a260ae7e2302517b52063cb33549d68b8fbec7a Mon Sep 17 00:00:00 2001 From: South12309 Date: Thu, 26 Oct 2023 19:18:09 +0300 Subject: [PATCH 1/2] task5 is done --- src/main/java/task_5/BattleException.java | 7 +++ src/main/java/task_5/BattleSimulator.java | 47 ++++++++++++++++ src/main/java/task_5/Hero.java | 64 ++++++++++++++++++++++ src/main/java/task_5/MagicItem.java | 49 +++++++++++++++++ src/main/java/task_5/NoItemsException.java | 7 +++ 5 files changed, 174 insertions(+) create mode 100644 src/main/java/task_5/BattleException.java create mode 100644 src/main/java/task_5/BattleSimulator.java create mode 100644 src/main/java/task_5/Hero.java create mode 100644 src/main/java/task_5/MagicItem.java create mode 100644 src/main/java/task_5/NoItemsException.java 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..731bf2a --- /dev/null +++ b/src/main/java/task_5/BattleSimulator.java @@ -0,0 +1,47 @@ +package task_5; + +import java.util.ArrayList; +import java.util.Map; +import java.util.Random; + +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) { + throw new RuntimeException(e); + } + 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..1093d0a --- /dev/null +++ b/src/main/java/task_5/Hero.java @@ -0,0 +1,64 @@ +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 (l==null) { + l=0l; + } + if ((System.currentTimeMillis() - l) 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); + } +} From 021654b051d38ff3b3a4d2a70d0613f4f22b3e06 Mon Sep 17 00:00:00 2001 From: South12309 Date: Tue, 31 Oct 2023 15:28:52 +0300 Subject: [PATCH 2/2] task5 is done. Fixed by comments --- src/main/java/task_5/BattleSimulator.java | 6 ++++-- src/main/java/task_5/Hero.java | 9 +++------ 2 files changed, 7 insertions(+), 8 deletions(-) diff --git a/src/main/java/task_5/BattleSimulator.java b/src/main/java/task_5/BattleSimulator.java index 731bf2a..41e6c86 100644 --- a/src/main/java/task_5/BattleSimulator.java +++ b/src/main/java/task_5/BattleSimulator.java @@ -1,9 +1,11 @@ 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(); @@ -33,7 +35,7 @@ public void simulateBattle(Hero hero, int numTurns) { try { Thread.sleep(2000); } catch (InterruptedException e) { - throw new RuntimeException(e); + log.info(e.getStackTrace()); } try { randomNumber = rnd.nextInt(hero.getInventory().size()); diff --git a/src/main/java/task_5/Hero.java b/src/main/java/task_5/Hero.java index 1093d0a..6303955 100644 --- a/src/main/java/task_5/Hero.java +++ b/src/main/java/task_5/Hero.java @@ -26,14 +26,11 @@ public void useItem(MagicItem magicItem) { if (magicItem.getManaCost() > currentMana) { throw new BattleException(heroName + " dont have mana to use " + magicItem.getItemName()); } - Long l = itemUsages.get(magicItem.getItemName()); - if (l==null) { - l=0l; - } - if ((System.currentTimeMillis() - l)