Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
Prev Previous commit
Next Next commit
Moves property to Java 11
  • Loading branch information
anuragagarwal561994 committed Dec 29, 2019
commit 5eee102650f288e096b7ad36fe8ac09f29d7471e
16 changes: 8 additions & 8 deletions property/src/main/java/com/iluwatar/property/App.java
Original file line number Diff line number Diff line change
Expand Up @@ -49,36 +49,36 @@ public class App {
*/
public static void main(String[] args) {
/* set up */
Prototype charProto = new Character();
var charProto = new Character();
charProto.set(Stats.STRENGTH, 10);
charProto.set(Stats.AGILITY, 10);
charProto.set(Stats.ARMOR, 10);
charProto.set(Stats.ATTACK_POWER, 10);

Character mageProto = new Character(Type.MAGE, charProto);
var mageProto = new Character(Type.MAGE, charProto);
mageProto.set(Stats.INTELLECT, 15);
mageProto.set(Stats.SPIRIT, 10);

Character warProto = new Character(Type.WARRIOR, charProto);
var warProto = new Character(Type.WARRIOR, charProto);
warProto.set(Stats.RAGE, 15);
warProto.set(Stats.ARMOR, 15); // boost default armor for warrior

Character rogueProto = new Character(Type.ROGUE, charProto);
var rogueProto = new Character(Type.ROGUE, charProto);
rogueProto.set(Stats.ENERGY, 15);
rogueProto.set(Stats.AGILITY, 15); // boost default agility for rogue

/* usage */
Character mag = new Character("Player_1", mageProto);
var mag = new Character("Player_1", mageProto);
mag.set(Stats.ARMOR, 8);
LOGGER.info(mag.toString());

Character warrior = new Character("Player_2", warProto);
var warrior = new Character("Player_2", warProto);
LOGGER.info(warrior.toString());

Character rogue = new Character("Player_3", rogueProto);
var rogue = new Character("Player_3", rogueProto);
LOGGER.info(rogue.toString());

Character rogueDouble = new Character("Player_4", rogue);
var rogueDouble = new Character("Player_4", rogue);
rogueDouble.set(Stats.ATTACK_POWER, 12);
LOGGER.info(rogueDouble.toString());
}
Expand Down
8 changes: 4 additions & 4 deletions property/src/main/java/com/iluwatar/property/Character.java
Original file line number Diff line number Diff line change
Expand Up @@ -93,7 +93,7 @@ public Type type() {

@Override
public Integer get(Stats stat) {
boolean containsValue = properties.containsKey(stat);
var containsValue = properties.containsKey(stat);
if (containsValue) {
return properties.get(stat);
} else {
Expand All @@ -118,7 +118,7 @@ public void remove(Stats stat) {

@Override
public String toString() {
StringBuilder builder = new StringBuilder();
var builder = new StringBuilder();
if (name != null) {
builder.append("Player: ").append(name).append('\n');
}
Expand All @@ -128,8 +128,8 @@ public String toString() {
}

builder.append("Stats:\n");
for (Stats stat : Stats.values()) {
Integer value = this.get(stat);
for (var stat : Stats.values()) {
var value = this.get(stat);
if (value == null) {
continue;
}
Expand Down
5 changes: 1 addition & 4 deletions property/src/test/java/com/iluwatar/property/AppTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -26,15 +26,12 @@
import org.junit.jupiter.api.Test;

/**
*
* Application test
*
*/
public class AppTest {

@Test
public void test() {
String[] args = {};
App.main(args);
App.main(new String[]{});
}
}
43 changes: 21 additions & 22 deletions property/src/test/java/com/iluwatar/property/CharacterTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -23,14 +23,15 @@

package com.iluwatar.property;

import org.junit.jupiter.api.Test;

import static com.iluwatar.property.Character.Type;
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertFalse;
import static org.junit.jupiter.api.Assertions.assertNull;
import static org.junit.jupiter.api.Assertions.assertTrue;

import java.util.Arrays;
import org.junit.jupiter.api.Test;

/**
* Date: 12/28/15 - 7:46 PM
*
Expand All @@ -40,13 +41,13 @@ public class CharacterTest {

@Test
public void testPrototypeStats() throws Exception {
final Character prototype = new Character();
final var prototype = new Character();

for (final Stats stat : Stats.values()) {
for (final var stat : Stats.values()) {
assertFalse(prototype.has(stat));
assertNull(prototype.get(stat));

final Integer expectedValue = stat.ordinal();
final var expectedValue = stat.ordinal();
prototype.set(stat, expectedValue);
assertTrue(prototype.has(stat));
assertEquals(expectedValue, prototype.get(stat));
Expand All @@ -59,66 +60,64 @@ public void testPrototypeStats() throws Exception {
}

@Test
public void testCharacterStats() throws Exception {
final Character prototype = new Character();
for (final Stats stat : Stats.values()) {
prototype.set(stat, stat.ordinal());
}
public void testCharacterStats() {
final var prototype = new Character();
Arrays.stream(Stats.values()).forEach(stat -> prototype.set(stat, stat.ordinal()));

final Character mage = new Character(Type.MAGE, prototype);
for (final Stats stat : Stats.values()) {
final Integer expectedValue = stat.ordinal();
final var mage = new Character(Type.MAGE, prototype);
for (final var stat : Stats.values()) {
final var expectedValue = stat.ordinal();
assertTrue(mage.has(stat));
assertEquals(expectedValue, mage.get(stat));
}
}

@Test
public void testToString() {
final Character prototype = new Character();
final var prototype = new Character();
prototype.set(Stats.ARMOR, 1);
prototype.set(Stats.AGILITY, 2);
prototype.set(Stats.INTELLECT, 3);
assertEquals("Stats:\n - AGILITY:2\n - ARMOR:1\n - INTELLECT:3\n", prototype.toString());

final Character stupid = new Character(Type.ROGUE, prototype);
final var stupid = new Character(Type.ROGUE, prototype);
stupid.remove(Stats.INTELLECT);
assertEquals("Character type: ROGUE\nStats:\n - AGILITY:2\n - ARMOR:1\n", stupid.toString());

final Character weak = new Character("weak", prototype);
final var weak = new Character("weak", prototype);
weak.remove(Stats.ARMOR);
assertEquals("Player: weak\nStats:\n - AGILITY:2\n - INTELLECT:3\n", weak.toString());

}

@Test
public void testName() {
final Character prototype = new Character();
final var prototype = new Character();
prototype.set(Stats.ARMOR, 1);
prototype.set(Stats.INTELLECT, 2);
assertNull(prototype.name());

final Character stupid = new Character(Type.ROGUE, prototype);
final var stupid = new Character(Type.ROGUE, prototype);
stupid.remove(Stats.INTELLECT);
assertNull(stupid.name());

final Character weak = new Character("weak", prototype);
final var weak = new Character("weak", prototype);
weak.remove(Stats.ARMOR);
assertEquals("weak", weak.name());
}

@Test
public void testType() {
final Character prototype = new Character();
final var prototype = new Character();
prototype.set(Stats.ARMOR, 1);
prototype.set(Stats.INTELLECT, 2);
assertNull(prototype.type());

final Character stupid = new Character(Type.ROGUE, prototype);
final var stupid = new Character(Type.ROGUE, prototype);
stupid.remove(Stats.INTELLECT);
assertEquals(Type.ROGUE, stupid.type());

final Character weak = new Character("weak", prototype);
final var weak = new Character("weak", prototype);
weak.remove(Stats.ARMOR);
assertNull(weak.type());
}
Expand Down