Merge pull request #29 from ruslanpa/master

[refactor] Makes enums more readable.
This commit is contained in:
Ilkka Seppälä 2015-02-10 07:03:48 +02:00
commit c7a4a85213
9 changed files with 58 additions and 99 deletions

View File

@ -2,11 +2,16 @@ package com.iluwatar;
public enum Armor { public enum Armor {
CLOTHES, LEATHER, CHAIN_MAIL, PLATE_MAIL; CLOTHES("clothes"), LEATHER("leather"), CHAIN_MAIL("chain mail"), PLATE_MAIL("plate mail");
@Override private String title;
Armor(String title) {
this.title = title;
}
@Override
public String toString() { public String toString() {
return name().toLowerCase().replaceAll("_", " "); return title;
} }
} }

View File

@ -1,32 +1,22 @@
package com.iluwatar; package com.iluwatar;
/** /**
* *
* Enumeration for target size. * Enumeration for target size.
* *
*/ */
public enum Size { public enum Size {
SMALL, NORMAL, LARGE; SMALL("small"), NORMAL("normal"), LARGE("large"), UNDEFINED("");
private String title;
@Override Size(String title) {
this.title = title;
}
@Override
public String toString() { public String toString() {
return title;
String s = "";
switch (this) {
case LARGE:
s = "large";
break;
case NORMAL:
s = "normal";
break;
case SMALL:
s = "small";
break;
default:
break;
}
return s;
} }
} }

View File

@ -7,24 +7,16 @@ package com.iluwatar;
*/ */
public enum Visibility { public enum Visibility {
VISIBLE, INVISIBLE; VISIBLE("visible"), INVISIBLE("invisible"), UNDEFINED("");
@Override private String title;
Visibility(String title) {
this.title = title;
}
@Override
public String toString() { public String toString() {
return title;
String s = "";
switch (this) {
case INVISIBLE:
s = "invisible";
break;
case VISIBLE:
s = "visible";
break;
default:
break;
}
return s;
} }
} }

View File

@ -2,23 +2,16 @@ package com.iluwatar;
public enum WeaponType { public enum WeaponType {
SHORT_SWORD, SPEAR, AXE; SHORT_SWORD("short sword"), SPEAR("spear"), AXE("axe"), UNDEFINED("");
@Override private String title;
WeaponType(String title) {
this.title = title;
}
@Override
public String toString() { public String toString() {
String s = ""; return title;
switch (this) {
case SHORT_SWORD:
s = "short sword";
break;
case SPEAR:
s = "spear";
break;
case AXE:
s = "axe";
break;
}
return s;
} }
} }

View File

@ -7,6 +7,5 @@ package com.iluwatar;
*/ */
public interface Potion { public interface Potion {
public void drink(); void drink();
} }

View File

@ -1,6 +1,7 @@
package com.iluwatar; package com.iluwatar;
import java.util.EnumMap; import java.util.EnumMap;
import java.util.Map;
/** /**
* *
@ -12,7 +13,7 @@ import java.util.EnumMap;
*/ */
public class PotionFactory { public class PotionFactory {
private EnumMap<PotionType, Potion> potions; private final Map<PotionType, Potion> potions;
public PotionFactory() { public PotionFactory() {
potions = new EnumMap<>(PotionType.class); potions = new EnumMap<>(PotionType.class);

View File

@ -7,6 +7,5 @@ package com.iluwatar;
*/ */
public enum PotionType { public enum PotionType {
HEALING, INVISIBILITY, STRENGTH, HOLY_WATER, POISON; HEALING, INVISIBILITY, STRENGTH, HOLY_WATER, POISON
} }

View File

@ -7,20 +7,15 @@ package com.iluwatar;
*/ */
public enum Action { public enum Action {
HUNT, TALE, GOLD, ENEMY; HUNT("hunted a rabbit"), TALE("tells a tale"), GOLD("found gold"), ENEMY("spotted enemies"), NONE("");
public String toString() { private String title;
switch (this) { Action(String title) {
case ENEMY: this.title = title;
return "spotted enemies"; }
case GOLD:
return "found gold"; public String toString() {
case HUNT: return title;
return "hunted a rabbit";
case TALE:
return "tells a tale";
}
return "";
} }
} }

View File

@ -2,31 +2,16 @@ package com.iluwatar;
public enum StarType { public enum StarType {
SUN, RED_GIANT, WHITE_DWARF, SUPERNOVA, DEAD; SUN("sun"), RED_GIANT("red giant"), WHITE_DWARF("white dwarf"), SUPERNOVA("supernova"), DEAD("dead star"), UNDEFINED("");
@Override private String title;
StarType(String title) {
this.title = title;
}
@Override
public String toString() { public String toString() {
String s = ""; return title;
switch (this) {
case RED_GIANT:
s = "red giant";
break;
case SUN:
s = "sun";
break;
case SUPERNOVA:
s = "supernova";
break;
case WHITE_DWARF:
s = "white dwarf";
break;
case DEAD:
s = "dead star";
break;
default:
break;
}
return s;
} }
} }