Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 7 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,2 +1,9 @@
.idea
.gradle
.classpath
.project
.settings
bin/
!**/src/main/**/build/
!**/src/test/**/build/
build/
10 changes: 10 additions & 0 deletions build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,16 @@ repositories {
dependencies {
testImplementation 'org.junit.jupiter:junit-jupiter-api:5.7.2'
testRuntimeOnly 'org.junit.jupiter:junit-jupiter-engine:5.7.2'
testRuntimeOnly 'org.junit.platform:junit-platform-launcher'

compileOnly 'org.projectlombok:lombok:1.18.30'
annotationProcessor 'org.projectlombok:lombok:1.18.30'

testCompileOnly 'org.projectlombok:lombok:1.18.30'
testAnnotationProcessor 'org.projectlombok:lombok:1.18.30'

implementation group: 'org.slf4j', name: 'slf4j-api', version: '2.0.9'
implementation group: 'org.slf4j', name: 'slf4j-simple', version: '2.0.9'
}

test {
Expand Down
41 changes: 41 additions & 0 deletions src/main/java/task_5/BattleSimulator.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
package task_5;

import java.util.Random;

import lombok.extern.slf4j.Slf4j;

@Slf4j
public class BattleSimulator {

public void simulateBattle(Hero hero, int numTurns) throws InterruptedException {

if (hero == null) {
throw new IllegalArgumentException("Null hero was sent");
}

if (numTurns < 1) {
throw new IllegalArgumentException("Allowed 1 turn atleast");
}

log.info("Beginning a battle simulation with {}, number of turns: {}", hero.getHeroName(), numTurns );
Random random = new Random();

MagicItem[] inventory = hero.getInventory().toArray(new MagicItem[hero.getInventory().size()]);

for (int i = 0; i < numTurns; i++) {

log.info("Turn {} start", i + 1);

int nextInt = random.nextInt(inventory.length);

try {
hero.useItem(inventory[nextInt]);
} catch (RuntimeException e) {
log.error("Exception in useItem(): {}", e.getMessage());
}

Thread.sleep(2000);
}
}

}
86 changes: 86 additions & 0 deletions src/main/java/task_5/Hero.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,86 @@
package task_5;

import java.time.Instant;
import java.util.HashMap;
import java.util.HashSet;
import java.util.Map;
import java.util.Set;

import lombok.Getter;
import lombok.extern.slf4j.Slf4j;
import task_5.exceptions.BattleException;
import task_5.exceptions.NoItemsException;

@Slf4j
@Getter
public class Hero {

private final String heroName;
private int currentMana;
private final Set<MagicItem> inventory;
private final Map<String, Long> itemUsage;

public Hero(String heroName, int currentMana) {

if (heroName == null || heroName.isBlank()) {
throw new IllegalArgumentException("Name can't be null or blank");
}

if (currentMana < 0) {
throw new IllegalArgumentException("Mana can't be negative");
}
this.heroName = heroName;
this.currentMana = currentMana;
this.inventory = new HashSet<>();
this.itemUsage = new HashMap<>();
}

public boolean addItemToInventory(MagicItem item) {

if (item == null) {
throw new IllegalArgumentException("Null agrs");
}

String itemName = item.getItemName();

if (inventory.contains(item)) {
log.warn("{} is in {}'s inventory already", itemName, heroName);
} else {
log.info("Adding {} to {}'s inventory", itemName, heroName);
}

return inventory.add(item);
}

public void useItem(MagicItem item) {

if (item == null) {
throw new IllegalArgumentException("Null agrs");
}

String itemName = item.getItemName();
int itemManaCost = item.getManaCost();

if (!inventory.contains(item)) {
throw new NoItemsException(itemName + " is out of inventory");
}

if (itemManaCost > currentMana) {
throw new BattleException("Not enough mana to use " + itemName);
}

int cooldown = item.getCooldown();

if (itemUsage.containsKey(itemName)
&& Instant.now().getEpochSecond() - itemUsage.get(itemName) < cooldown) {
log.warn("{} with cooldown {} was used {} sec. ago", itemName, cooldown, Instant.now().getEpochSecond() - itemUsage.get(itemName));
throw new BattleException(itemName + " still cooling down");
}

itemUsage.put(itemName, Instant.now().getEpochSecond());
currentMana -= itemManaCost;
log.info("{} with mana cost: {} was used", item.getItemName(), itemManaCost);

}

}
30 changes: 30 additions & 0 deletions src/main/java/task_5/MagicItem.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
package task_5;

import lombok.EqualsAndHashCode;
import lombok.Getter;

@Getter
@EqualsAndHashCode
public class MagicItem {

private final String itemName;
private final int manaCost;
private final int cooldown;

public MagicItem(String itemName, int manaCost, int cooldown) {

if (itemName == null || itemName.isBlank()) {
throw new IllegalArgumentException("Name can't be null or blank");
}

if (manaCost <= 0 || cooldown <= 0) {
throw new IllegalArgumentException("Cost and cooldown time must be positive");
}
this.itemName = itemName;
this.manaCost = manaCost;
this.cooldown = cooldown;
}



}
130 changes: 130 additions & 0 deletions src/main/java/task_5/Task5.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,130 @@
package task_5;

import lombok.extern.slf4j.Slf4j;
import task_5.exceptions.BattleException;
import task_5.exceptions.NoItemsException;

@Slf4j
public class Task5 {

public static void main(String[] args) throws InterruptedException {

Hero hero1 = new Hero("Axe", 25);
Hero hero2 = new Hero("Huskar", 10);
Hero hero3= new Hero("Kunkka", 1);
Hero hero4 = new Hero("Riki", 55);
Hero hero5 = new Hero("Pugna", 22);
Hero hero6 = new Hero("Phoenix", 10000);

MagicItem magicItem1 = new MagicItem("Magic Wand", 36, 13);
MagicItem magicItem2 = new MagicItem("Bracer", 24, 10);
MagicItem magicItem3 = new MagicItem("Soul Ring", 19, 25);
MagicItem magicItem4 = new MagicItem("Helm of the Dominator", 39, 45);
MagicItem magicItem5 = new MagicItem("Falcon Blade", 16, 20);
MagicItem magicItem6 = new MagicItem("Oblivion Staff", 26, 31);
MagicItem magicItem7 = new MagicItem("Pavise", 19, 18);
MagicItem magicItem8 = new MagicItem("Mekansm", 22, 65);
MagicItem magicItem9 = new MagicItem("Force Staff", 150, 19);

try {
new MagicItem(null, 150, 19);
} catch (IllegalArgumentException e) {
log.error("Creation MagicItem exception: {}", e.getMessage());
}

try {
new MagicItem(" ", 150, 19);
} catch (IllegalArgumentException e) {
log.error("Creation MagicItem exception: {}", e.getMessage());
}

try {
new MagicItem("Test ", -1, 19);
} catch (IllegalArgumentException e) {
log.error("Creation MagicItem exception: {}", e.getMessage());
}

try {
new MagicItem("Test ", 100, -1);
} catch (IllegalArgumentException e) {
log.error("Creation MagicItem exception: {}", e.getMessage());
}

try {
new Hero(null, 15);
} catch (IllegalArgumentException e) {
log.error("Creation Hero exception: {}", e.getMessage());
}

try {
new Hero(" ", 15);
} catch (IllegalArgumentException e) {
log.error("Creation Hero exception: {}", e.getMessage());
}

try {
new Hero("Hero", -1);
} catch (IllegalArgumentException e) {
log.error("Creation Hero exception: {}", e.getMessage());
}

try {
hero1.addItemToInventory(null);
} catch (IllegalArgumentException e) {
log.error("Exception when invoke addItemToInventory(): {}", e.getMessage());
}

hero1.addItemToInventory(magicItem9);
hero1.addItemToInventory(magicItem9);
hero1.addItemToInventory(magicItem2);


try {
hero1.useItem(null);
} catch (IllegalArgumentException e) {
log.error("Exception when invoke useItem(): {}", e.getMessage());
}

try {
hero1.useItem(magicItem1);
} catch (NoItemsException e) {
log.error("Exception when invoke useItem(): {}", e.getMessage());
}

try {
hero1.useItem(magicItem9);
} catch (BattleException e) {
log.error("Exception when invoke useItem(): {}", e.getMessage());
}

hero1.useItem(magicItem2);

BattleSimulator battleSimulator = new BattleSimulator();

try {
battleSimulator.simulateBattle(null, 1);
} catch (IllegalArgumentException e) {
log.error("Exception when invoke simulateBattle(): {}", e.getMessage());
}

try {
battleSimulator.simulateBattle(hero1, 0);
} catch (IllegalArgumentException e) {
log.error("Exception when invoke simulateBattle(): {}", e.getMessage());
}

hero6.addItemToInventory(magicItem1);
hero6.addItemToInventory(magicItem2);
hero6.addItemToInventory(magicItem3);
hero6.addItemToInventory(magicItem4);
hero6.addItemToInventory(magicItem5);
hero6.addItemToInventory(magicItem6);
hero6.addItemToInventory(magicItem7);
hero6.addItemToInventory(magicItem8);
hero6.addItemToInventory(magicItem9);

battleSimulator.simulateBattle(hero6, 10);

}

}
10 changes: 10 additions & 0 deletions src/main/java/task_5/exceptions/BattleException.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
package task_5.exceptions;

public class BattleException extends RuntimeException {

public BattleException(String message) {
super(message);
}


}
9 changes: 9 additions & 0 deletions src/main/java/task_5/exceptions/NoItemsException.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
package task_5.exceptions;

public class NoItemsException extends RuntimeException {

public NoItemsException(String message) {
super(message);
}

}
30 changes: 30 additions & 0 deletions src/test/java/exceptions_workshop/task5/BattleSimulatorTest.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
package exceptions_workshop.task5;

import static org.junit.jupiter.api.Assertions.assertEquals;

import org.junit.jupiter.api.Assertions;
import org.junit.jupiter.api.Test;

import task_5.BattleSimulator;
import task_5.Hero;

public class BattleSimulatorTest {

private final BattleSimulator battleSimulator = new BattleSimulator();
private final Hero hero = new Hero("Phoenix", 10000);

@Test
void simulateBattle() {

Exception ex = Assertions.assertThrows(IllegalArgumentException.class,
() -> battleSimulator.simulateBattle(null, 1));

Exception ex2 = Assertions.assertThrows(IllegalArgumentException.class,
() -> battleSimulator.simulateBattle(hero, 0));

assertEquals("Null hero was sent", ex.getMessage());
assertEquals("Allowed 1 turn atleast", ex2.getMessage());

}

}
Loading