Formatted all files to the same standard
This commit is contained in:
parent
53a2a8b150
commit
3da9ad5469
@ -8,7 +8,9 @@ package com.iluwatar;
|
|||||||
public interface KingdomFactory {
|
public interface KingdomFactory {
|
||||||
|
|
||||||
Castle createCastle();
|
Castle createCastle();
|
||||||
|
|
||||||
King createKing();
|
King createKing();
|
||||||
|
|
||||||
Army createArmy();
|
Army createArmy();
|
||||||
|
|
||||||
}
|
}
|
||||||
|
@ -2,9 +2,8 @@ package com.iluwatar;
|
|||||||
|
|
||||||
/**
|
/**
|
||||||
*
|
*
|
||||||
* Adapter class. Adapts the interface of the device
|
* Adapter class. Adapts the interface of the device (GoblinGlider) into
|
||||||
* (GoblinGlider) into Engineer interface expected
|
* Engineer interface expected by the client (GnomeEngineeringManager).
|
||||||
* by the client (GnomeEngineeringManager).
|
|
||||||
*
|
*
|
||||||
*/
|
*/
|
||||||
public class GnomeEngineer implements Engineer {
|
public class GnomeEngineer implements Engineer {
|
||||||
|
@ -2,8 +2,7 @@ package com.iluwatar;
|
|||||||
|
|
||||||
/**
|
/**
|
||||||
*
|
*
|
||||||
* GnomeEngineering manager uses Engineer to
|
* GnomeEngineering manager uses Engineer to operate devices.
|
||||||
* operate devices.
|
|
||||||
*
|
*
|
||||||
*/
|
*/
|
||||||
public class GnomeEngineeringManager implements Engineer {
|
public class GnomeEngineeringManager implements Engineer {
|
||||||
|
@ -10,19 +10,22 @@ package com.iluwatar;
|
|||||||
public class App {
|
public class App {
|
||||||
|
|
||||||
public static void main(String[] args) {
|
public static void main(String[] args) {
|
||||||
BlindingMagicWeapon blindingMagicWeapon = new BlindingMagicWeapon(new Excalibur());
|
BlindingMagicWeapon blindingMagicWeapon = new BlindingMagicWeapon(
|
||||||
|
new Excalibur());
|
||||||
blindingMagicWeapon.wield();
|
blindingMagicWeapon.wield();
|
||||||
blindingMagicWeapon.blind();
|
blindingMagicWeapon.blind();
|
||||||
blindingMagicWeapon.swing();
|
blindingMagicWeapon.swing();
|
||||||
blindingMagicWeapon.unwield();
|
blindingMagicWeapon.unwield();
|
||||||
|
|
||||||
FlyingMagicWeapon flyingMagicWeapon = new FlyingMagicWeapon(new Mjollnir());
|
FlyingMagicWeapon flyingMagicWeapon = new FlyingMagicWeapon(
|
||||||
|
new Mjollnir());
|
||||||
flyingMagicWeapon.wield();
|
flyingMagicWeapon.wield();
|
||||||
flyingMagicWeapon.fly();
|
flyingMagicWeapon.fly();
|
||||||
flyingMagicWeapon.swing();
|
flyingMagicWeapon.swing();
|
||||||
flyingMagicWeapon.unwield();
|
flyingMagicWeapon.unwield();
|
||||||
|
|
||||||
SoulEatingMagicWeapon soulEatingMagicWeapon = new SoulEatingMagicWeapon(new Stormbringer());
|
SoulEatingMagicWeapon soulEatingMagicWeapon = new SoulEatingMagicWeapon(
|
||||||
|
new Stormbringer());
|
||||||
soulEatingMagicWeapon.wield();
|
soulEatingMagicWeapon.wield();
|
||||||
soulEatingMagicWeapon.swing();
|
soulEatingMagicWeapon.swing();
|
||||||
soulEatingMagicWeapon.eatSoul();
|
soulEatingMagicWeapon.eatSoul();
|
||||||
|
@ -19,7 +19,8 @@ public class Excalibur extends BlindingMagicWeaponImp {
|
|||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void blindImp() {
|
public void blindImp() {
|
||||||
System.out.println("bright light streams from Excalibur blinding the enemy");
|
System.out
|
||||||
|
.println("bright light streams from Excalibur blinding the enemy");
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
@ -19,7 +19,8 @@ public class Mjollnir extends FlyingMagicWeaponImp {
|
|||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void flyImp() {
|
public void flyImp() {
|
||||||
System.out.println("Mjollnir hits the enemy in the air and returns back to the owner's hand");
|
System.out
|
||||||
|
.println("Mjollnir hits the enemy in the air and returns back to the owner's hand");
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
@ -20,23 +20,18 @@ public class App {
|
|||||||
public static void main(String[] args) {
|
public static void main(String[] args) {
|
||||||
|
|
||||||
Hero mage = new HeroBuilder(Profession.MAGE, "Riobard")
|
Hero mage = new HeroBuilder(Profession.MAGE, "Riobard")
|
||||||
.withHairColor(HairColor.BLACK)
|
.withHairColor(HairColor.BLACK).withWeapon(Weapon.DAGGER)
|
||||||
.withWeapon(Weapon.DAGGER)
|
|
||||||
.build();
|
.build();
|
||||||
System.out.println(mage);
|
System.out.println(mage);
|
||||||
|
|
||||||
Hero warrior = new HeroBuilder(Profession.WARRIOR, "Amberjill")
|
Hero warrior = new HeroBuilder(Profession.WARRIOR, "Amberjill")
|
||||||
.withHairColor(HairColor.BLOND)
|
.withHairColor(HairColor.BLOND)
|
||||||
.withHairType(HairType.LONG_CURLY)
|
.withHairType(HairType.LONG_CURLY).withArmor(Armor.CHAIN_MAIL)
|
||||||
.withArmor(Armor.CHAIN_MAIL)
|
.withWeapon(Weapon.SWORD).build();
|
||||||
.withWeapon(Weapon.SWORD)
|
|
||||||
.build();
|
|
||||||
System.out.println(warrior);
|
System.out.println(warrior);
|
||||||
|
|
||||||
Hero thief = new HeroBuilder(Profession.THIEF, "Desmond")
|
Hero thief = new HeroBuilder(Profession.THIEF, "Desmond")
|
||||||
.withHairType(HairType.BALD)
|
.withHairType(HairType.BALD).withWeapon(Weapon.BOW).build();
|
||||||
.withWeapon(Weapon.BOW)
|
|
||||||
.build();
|
|
||||||
System.out.println(thief);
|
System.out.println(thief);
|
||||||
|
|
||||||
}
|
}
|
||||||
|
@ -8,10 +8,18 @@ public enum Armor {
|
|||||||
public String toString() {
|
public String toString() {
|
||||||
String s = "";
|
String s = "";
|
||||||
switch (this) {
|
switch (this) {
|
||||||
case CLOTHES: s = "clothes"; break;
|
case CLOTHES:
|
||||||
case LEATHER: s = "leather armor"; break;
|
s = "clothes";
|
||||||
case CHAIN_MAIL: s = "chain mail"; break;
|
break;
|
||||||
case PLATE_MAIL: s = "plate mail"; break;
|
case LEATHER:
|
||||||
|
s = "leather armor";
|
||||||
|
break;
|
||||||
|
case CHAIN_MAIL:
|
||||||
|
s = "chain mail";
|
||||||
|
break;
|
||||||
|
case PLATE_MAIL:
|
||||||
|
s = "plate mail";
|
||||||
|
break;
|
||||||
}
|
}
|
||||||
return s;
|
return s;
|
||||||
}
|
}
|
||||||
|
@ -8,11 +8,21 @@ public enum HairColor {
|
|||||||
public String toString() {
|
public String toString() {
|
||||||
String s = "";
|
String s = "";
|
||||||
switch (this) {
|
switch (this) {
|
||||||
case WHITE: s = "white"; break;
|
case WHITE:
|
||||||
case BLOND: s = "blond"; break;
|
s = "white";
|
||||||
case RED: s = "red"; break;
|
break;
|
||||||
case BROWN: s = "brown"; break;
|
case BLOND:
|
||||||
case BLACK: s = "black"; break;
|
s = "blond";
|
||||||
|
break;
|
||||||
|
case RED:
|
||||||
|
s = "red";
|
||||||
|
break;
|
||||||
|
case BROWN:
|
||||||
|
s = "brown";
|
||||||
|
break;
|
||||||
|
case BLACK:
|
||||||
|
s = "black";
|
||||||
|
break;
|
||||||
}
|
}
|
||||||
return s;
|
return s;
|
||||||
}
|
}
|
||||||
|
@ -8,11 +8,21 @@ public enum HairType {
|
|||||||
public String toString() {
|
public String toString() {
|
||||||
String s = "";
|
String s = "";
|
||||||
switch (this) {
|
switch (this) {
|
||||||
case BALD: s = "bold"; break;
|
case BALD:
|
||||||
case SHORT: s = "short"; break;
|
s = "bold";
|
||||||
case CURLY: s = "curly"; break;
|
break;
|
||||||
case LONG_STRAIGHT: s = "long straight"; break;
|
case SHORT:
|
||||||
case LONG_CURLY: s = "long curly"; break;
|
s = "short";
|
||||||
|
break;
|
||||||
|
case CURLY:
|
||||||
|
s = "curly";
|
||||||
|
break;
|
||||||
|
case LONG_STRAIGHT:
|
||||||
|
s = "long straight";
|
||||||
|
break;
|
||||||
|
case LONG_CURLY:
|
||||||
|
s = "long curly";
|
||||||
|
break;
|
||||||
}
|
}
|
||||||
return s;
|
return s;
|
||||||
}
|
}
|
||||||
|
@ -94,7 +94,8 @@ public class Hero {
|
|||||||
|
|
||||||
public HeroBuilder(Profession profession, String name) {
|
public HeroBuilder(Profession profession, String name) {
|
||||||
if (profession == null || name == null) {
|
if (profession == null || name == null) {
|
||||||
throw new NullPointerException("profession and name can not be null");
|
throw new NullPointerException(
|
||||||
|
"profession and name can not be null");
|
||||||
}
|
}
|
||||||
this.profession = profession;
|
this.profession = profession;
|
||||||
this.name = name;
|
this.name = name;
|
||||||
|
@ -8,10 +8,18 @@ public enum Profession {
|
|||||||
public String toString() {
|
public String toString() {
|
||||||
String s = "";
|
String s = "";
|
||||||
switch (this) {
|
switch (this) {
|
||||||
case WARRIOR: s = "Warrior"; break;
|
case WARRIOR:
|
||||||
case THIEF: s = "Thief"; break;
|
s = "Warrior";
|
||||||
case MAGE: s = "Mage"; break;
|
break;
|
||||||
case PRIEST: s = "Priest"; break;
|
case THIEF:
|
||||||
|
s = "Thief";
|
||||||
|
break;
|
||||||
|
case MAGE:
|
||||||
|
s = "Mage";
|
||||||
|
break;
|
||||||
|
case PRIEST:
|
||||||
|
s = "Priest";
|
||||||
|
break;
|
||||||
}
|
}
|
||||||
return s;
|
return s;
|
||||||
}
|
}
|
||||||
|
@ -8,11 +8,21 @@ public enum Weapon {
|
|||||||
public String toString() {
|
public String toString() {
|
||||||
String s = "";
|
String s = "";
|
||||||
switch (this) {
|
switch (this) {
|
||||||
case DAGGER: s = "dagger"; break;
|
case DAGGER:
|
||||||
case SWORD: s = "sword"; break;
|
s = "dagger";
|
||||||
case AXE: s = "axe"; break;
|
break;
|
||||||
case WARHAMMER: s = "warhammer"; break;
|
case SWORD:
|
||||||
case BOW: s = "bow"; break;
|
s = "sword";
|
||||||
|
break;
|
||||||
|
case AXE:
|
||||||
|
s = "axe";
|
||||||
|
break;
|
||||||
|
case WARHAMMER:
|
||||||
|
s = "warhammer";
|
||||||
|
break;
|
||||||
|
case BOW:
|
||||||
|
s = "bow";
|
||||||
|
break;
|
||||||
}
|
}
|
||||||
return s;
|
return s;
|
||||||
}
|
}
|
||||||
|
@ -14,7 +14,8 @@ public class App {
|
|||||||
|
|
||||||
OrcKing king = new OrcKing();
|
OrcKing king = new OrcKing();
|
||||||
king.makeRequest(new Request(RequestType.DEFEND_CASTLE, "defend castle"));
|
king.makeRequest(new Request(RequestType.DEFEND_CASTLE, "defend castle"));
|
||||||
king.makeRequest(new Request(RequestType.TORTURE_PRISONER, "torture prisoner"));
|
king.makeRequest(new Request(RequestType.TORTURE_PRISONER,
|
||||||
|
"torture prisoner"));
|
||||||
king.makeRequest(new Request(RequestType.COLLECT_TAX, "collect tax"));
|
king.makeRequest(new Request(RequestType.COLLECT_TAX, "collect tax"));
|
||||||
|
|
||||||
}
|
}
|
||||||
|
@ -2,8 +2,6 @@ package com.iluwatar;
|
|||||||
|
|
||||||
public enum RequestType {
|
public enum RequestType {
|
||||||
|
|
||||||
DEFEND_CASTLE,
|
DEFEND_CASTLE, TORTURE_PRISONER, COLLECT_TAX
|
||||||
TORTURE_PRISONER,
|
|
||||||
COLLECT_TAX
|
|
||||||
|
|
||||||
}
|
}
|
||||||
|
@ -2,9 +2,7 @@ package com.iluwatar;
|
|||||||
|
|
||||||
public enum Size {
|
public enum Size {
|
||||||
|
|
||||||
SMALL,
|
SMALL, NORMAL, LARGE;
|
||||||
NORMAL,
|
|
||||||
LARGE;
|
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public String toString() {
|
public String toString() {
|
||||||
@ -27,5 +25,4 @@ public enum Size {
|
|||||||
return s;
|
return s;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
}
|
}
|
||||||
|
@ -26,6 +26,7 @@ public abstract class Target {
|
|||||||
public abstract String toString();
|
public abstract String toString();
|
||||||
|
|
||||||
public void printStatus() {
|
public void printStatus() {
|
||||||
System.out.println(String.format("%s, size=%s visibility=%s", this, getSize(), getVisibility()));
|
System.out.println(String.format("%s, size=%s visibility=%s", this,
|
||||||
|
getSize(), getVisibility()));
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -2,8 +2,7 @@ package com.iluwatar;
|
|||||||
|
|
||||||
public enum Visibility {
|
public enum Visibility {
|
||||||
|
|
||||||
VISIBLE,
|
VISIBLE, INVISIBLE;
|
||||||
INVISIBLE;
|
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public String toString() {
|
public String toString() {
|
||||||
|
@ -10,15 +10,20 @@ public class Messenger {
|
|||||||
|
|
||||||
List<Word> words = new ArrayList<Word>();
|
List<Word> words = new ArrayList<Word>();
|
||||||
|
|
||||||
words.add(new Word(Arrays.asList(new Letter('W'), new Letter('h'), new Letter('e'), new Letter('r'), new Letter('e'))));
|
words.add(new Word(Arrays.asList(new Letter('W'), new Letter('h'),
|
||||||
words.add(new Word(Arrays.asList(new Letter('t'), new Letter('h'), new Letter('e'), new Letter('r'), new Letter('e'))));
|
new Letter('e'), new Letter('r'), new Letter('e'))));
|
||||||
|
words.add(new Word(Arrays.asList(new Letter('t'), new Letter('h'),
|
||||||
|
new Letter('e'), new Letter('r'), new Letter('e'))));
|
||||||
words.add(new Word(Arrays.asList(new Letter('i'), new Letter('s'))));
|
words.add(new Word(Arrays.asList(new Letter('i'), new Letter('s'))));
|
||||||
words.add(new Word(Arrays.asList(new Letter('a'))));
|
words.add(new Word(Arrays.asList(new Letter('a'))));
|
||||||
words.add(new Word(Arrays.asList(new Letter('w'), new Letter('h'), new Letter('i'), new Letter('p'))));
|
words.add(new Word(Arrays.asList(new Letter('w'), new Letter('h'),
|
||||||
words.add(new Word(Arrays.asList(new Letter('t'), new Letter('h'), new Letter('e'), new Letter('r'), new Letter('e'))));
|
new Letter('i'), new Letter('p'))));
|
||||||
|
words.add(new Word(Arrays.asList(new Letter('t'), new Letter('h'),
|
||||||
|
new Letter('e'), new Letter('r'), new Letter('e'))));
|
||||||
words.add(new Word(Arrays.asList(new Letter('i'), new Letter('s'))));
|
words.add(new Word(Arrays.asList(new Letter('i'), new Letter('s'))));
|
||||||
words.add(new Word(Arrays.asList(new Letter('a'))));
|
words.add(new Word(Arrays.asList(new Letter('a'))));
|
||||||
words.add(new Word(Arrays.asList(new Letter('w'), new Letter('a'), new Letter('y'))));
|
words.add(new Word(Arrays.asList(new Letter('w'), new Letter('a'),
|
||||||
|
new Letter('y'))));
|
||||||
|
|
||||||
return new Sentence(words);
|
return new Sentence(words);
|
||||||
|
|
||||||
@ -28,12 +33,18 @@ public class Messenger {
|
|||||||
|
|
||||||
List<Word> words = new ArrayList<Word>();
|
List<Word> words = new ArrayList<Word>();
|
||||||
|
|
||||||
words.add(new Word(Arrays.asList(new Letter('M'), new Letter('u'), new Letter('c'), new Letter('h'))));
|
words.add(new Word(Arrays.asList(new Letter('M'), new Letter('u'),
|
||||||
words.add(new Word(Arrays.asList(new Letter('w'), new Letter('i'), new Letter('n'), new Letter('d'))));
|
new Letter('c'), new Letter('h'))));
|
||||||
words.add(new Word(Arrays.asList(new Letter('p'), new Letter('o'), new Letter('u'), new Letter('r'), new Letter('s'))));
|
words.add(new Word(Arrays.asList(new Letter('w'), new Letter('i'),
|
||||||
words.add(new Word(Arrays.asList(new Letter('f'), new Letter('r'), new Letter('o'), new Letter('m'))));
|
new Letter('n'), new Letter('d'))));
|
||||||
words.add(new Word(Arrays.asList(new Letter('y'), new Letter('o'), new Letter('u'), new Letter('r'))));
|
words.add(new Word(Arrays.asList(new Letter('p'), new Letter('o'),
|
||||||
words.add(new Word(Arrays.asList(new Letter('m'), new Letter('o'), new Letter('u'), new Letter('t'), new Letter('h'))));
|
new Letter('u'), new Letter('r'), new Letter('s'))));
|
||||||
|
words.add(new Word(Arrays.asList(new Letter('f'), new Letter('r'),
|
||||||
|
new Letter('o'), new Letter('m'))));
|
||||||
|
words.add(new Word(Arrays.asList(new Letter('y'), new Letter('o'),
|
||||||
|
new Letter('u'), new Letter('r'))));
|
||||||
|
words.add(new Word(Arrays.asList(new Letter('m'), new Letter('o'),
|
||||||
|
new Letter('u'), new Letter('t'), new Letter('h'))));
|
||||||
|
|
||||||
return new Sentence(words);
|
return new Sentence(words);
|
||||||
|
|
||||||
|
@ -20,5 +20,4 @@ public class Sentence extends LetterComposite {
|
|||||||
System.out.print(".");
|
System.out.print(".");
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
}
|
}
|
||||||
|
@ -3,6 +3,7 @@ package com.iluwatar;
|
|||||||
public interface Hostile {
|
public interface Hostile {
|
||||||
|
|
||||||
void attack();
|
void attack();
|
||||||
|
|
||||||
void fleeBattle();
|
void fleeBattle();
|
||||||
|
|
||||||
}
|
}
|
||||||
|
@ -19,7 +19,8 @@ public class App {
|
|||||||
executorService.execute(new Runnable() {
|
executorService.execute(new Runnable() {
|
||||||
@Override
|
@Override
|
||||||
public void run() {
|
public void run() {
|
||||||
while (inventory.addItem(new Item()));
|
while (inventory.addItem(new Item()))
|
||||||
|
;
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
@ -5,7 +5,6 @@ import java.util.List;
|
|||||||
import java.util.concurrent.locks.Lock;
|
import java.util.concurrent.locks.Lock;
|
||||||
import java.util.concurrent.locks.ReentrantLock;
|
import java.util.concurrent.locks.ReentrantLock;
|
||||||
|
|
||||||
|
|
||||||
public class Inventory {
|
public class Inventory {
|
||||||
|
|
||||||
private int inventorySize;
|
private int inventorySize;
|
||||||
|
@ -8,12 +8,17 @@ public enum WeaponType {
|
|||||||
public String toString() {
|
public String toString() {
|
||||||
String s = "";
|
String s = "";
|
||||||
switch (this) {
|
switch (this) {
|
||||||
case SHORT_SWORD: s = "short sword"; break;
|
case SHORT_SWORD:
|
||||||
case SPEAR: s = "spear"; break;
|
s = "short sword";
|
||||||
case AXE: s = "axe"; break;
|
break;
|
||||||
|
case SPEAR:
|
||||||
|
s = "spear";
|
||||||
|
break;
|
||||||
|
case AXE:
|
||||||
|
s = "axe";
|
||||||
|
break;
|
||||||
}
|
}
|
||||||
return s;
|
return s;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
}
|
}
|
||||||
|
@ -4,7 +4,8 @@ public class HealingPotion implements Potion {
|
|||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void drink() {
|
public void drink() {
|
||||||
System.out.println("You feel healed. (Potion=" + System.identityHashCode(this) + ")");
|
System.out.println("You feel healed. (Potion="
|
||||||
|
+ System.identityHashCode(this) + ")");
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
@ -4,7 +4,8 @@ public class HolyWaterPotion implements Potion {
|
|||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void drink() {
|
public void drink() {
|
||||||
System.out.println("You feel blessed. (Potion=" + System.identityHashCode(this) + ")");
|
System.out.println("You feel blessed. (Potion="
|
||||||
|
+ System.identityHashCode(this) + ")");
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
@ -4,7 +4,8 @@ public class InvisibilityPotion implements Potion {
|
|||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void drink() {
|
public void drink() {
|
||||||
System.out.println("You become invisible. (Potion=" + System.identityHashCode(this) + ")");
|
System.out.println("You become invisible. (Potion="
|
||||||
|
+ System.identityHashCode(this) + ")");
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
@ -4,7 +4,8 @@ public class PoisonPotion implements Potion {
|
|||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void drink() {
|
public void drink() {
|
||||||
System.out.println("Urgh! This is poisonous. (Potion=" + System.identityHashCode(this) + ")");
|
System.out.println("Urgh! This is poisonous. (Potion="
|
||||||
|
+ System.identityHashCode(this) + ")");
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
@ -2,10 +2,6 @@ package com.iluwatar;
|
|||||||
|
|
||||||
public enum PotionType {
|
public enum PotionType {
|
||||||
|
|
||||||
HEALING,
|
HEALING, INVISIBILITY, STRENGTH, HOLY_WATER, POISON;
|
||||||
INVISIBILITY,
|
|
||||||
STRENGTH,
|
|
||||||
HOLY_WATER,
|
|
||||||
POISON;
|
|
||||||
|
|
||||||
}
|
}
|
||||||
|
@ -4,7 +4,8 @@ public class StrengthPotion implements Potion {
|
|||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void drink() {
|
public void drink() {
|
||||||
System.out.println("You feel strong. (Potion=" + System.identityHashCode(this) + ")");
|
System.out.println("You feel strong. (Potion="
|
||||||
|
+ System.identityHashCode(this) + ")");
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
@ -25,22 +25,28 @@ public class App {
|
|||||||
if (isOperator(s)) {
|
if (isOperator(s)) {
|
||||||
Expression rightExpression = stack.pop();
|
Expression rightExpression = stack.pop();
|
||||||
Expression leftExpression = stack.pop();
|
Expression leftExpression = stack.pop();
|
||||||
System.out.println(String.format("popped from stack left: %d right: %d",
|
System.out
|
||||||
leftExpression.interpret(), rightExpression.interpret()));
|
.println(String.format(
|
||||||
|
"popped from stack left: %d right: %d",
|
||||||
|
leftExpression.interpret(),
|
||||||
|
rightExpression.interpret()));
|
||||||
Expression operator = getOperatorInstance(s, leftExpression,
|
Expression operator = getOperatorInstance(s, leftExpression,
|
||||||
rightExpression);
|
rightExpression);
|
||||||
System.out.println(String.format("operator: %s", operator));
|
System.out.println(String.format("operator: %s", operator));
|
||||||
int result = operator.interpret();
|
int result = operator.interpret();
|
||||||
NumberExpression resultExpression = new NumberExpression(result);
|
NumberExpression resultExpression = new NumberExpression(result);
|
||||||
stack.push(resultExpression);
|
stack.push(resultExpression);
|
||||||
System.out.println(String.format("push result to stack: %d", resultExpression.interpret()));
|
System.out.println(String.format("push result to stack: %d",
|
||||||
|
resultExpression.interpret()));
|
||||||
} else {
|
} else {
|
||||||
Expression i = new NumberExpression(s);
|
Expression i = new NumberExpression(s);
|
||||||
stack.push(i);
|
stack.push(i);
|
||||||
System.out.println(String.format("push to stack: %d", i.interpret()));
|
System.out.println(String.format("push to stack: %d",
|
||||||
|
i.interpret()));
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
System.out.println(String.format("result: %d", stack.pop().interpret()));
|
System.out
|
||||||
|
.println(String.format("result: %d", stack.pop().interpret()));
|
||||||
}
|
}
|
||||||
|
|
||||||
public static boolean isOperator(String s) {
|
public static boolean isOperator(String s) {
|
||||||
|
@ -5,7 +5,8 @@ public class MultiplyExpression extends Expression {
|
|||||||
private Expression leftExpression;
|
private Expression leftExpression;
|
||||||
private Expression rightExpression;
|
private Expression rightExpression;
|
||||||
|
|
||||||
public MultiplyExpression(Expression leftExpression, Expression rightExpression) {
|
public MultiplyExpression(Expression leftExpression,
|
||||||
|
Expression rightExpression) {
|
||||||
this.leftExpression = leftExpression;
|
this.leftExpression = leftExpression;
|
||||||
this.rightExpression = rightExpression;
|
this.rightExpression = rightExpression;
|
||||||
}
|
}
|
||||||
|
@ -2,9 +2,6 @@ package com.iluwatar;
|
|||||||
|
|
||||||
public enum ItemType {
|
public enum ItemType {
|
||||||
|
|
||||||
ANY,
|
ANY, WEAPON, RING, POTION
|
||||||
WEAPON,
|
|
||||||
RING,
|
|
||||||
POTION
|
|
||||||
|
|
||||||
}
|
}
|
||||||
|
@ -39,7 +39,8 @@ public class TreasureChestItemIterator implements ItemIterator {
|
|||||||
tempIdx = -1;
|
tempIdx = -1;
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
if (type.equals(ItemType.ANY) || items.get(tempIdx).getType().equals(type)) {
|
if (type.equals(ItemType.ANY)
|
||||||
|
|| items.get(tempIdx).getType().equals(type)) {
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -63,6 +63,7 @@ public class Star {
|
|||||||
|
|
||||||
@Override
|
@Override
|
||||||
public String toString() {
|
public String toString() {
|
||||||
return String.format("%s age: %d years mass: %d tons", type.toString(), ageYears, massTons);
|
return String.format("%s age: %d years mass: %d tons", type.toString(),
|
||||||
|
ageYears, massTons);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -14,18 +14,23 @@ public class StarMementoInternal implements StarMemento {
|
|||||||
public StarType getType() {
|
public StarType getType() {
|
||||||
return type;
|
return type;
|
||||||
}
|
}
|
||||||
|
|
||||||
public void setType(StarType type) {
|
public void setType(StarType type) {
|
||||||
this.type = type;
|
this.type = type;
|
||||||
}
|
}
|
||||||
|
|
||||||
public int getAgeYears() {
|
public int getAgeYears() {
|
||||||
return ageYears;
|
return ageYears;
|
||||||
}
|
}
|
||||||
|
|
||||||
public void setAgeYears(int ageYears) {
|
public void setAgeYears(int ageYears) {
|
||||||
this.ageYears = ageYears;
|
this.ageYears = ageYears;
|
||||||
}
|
}
|
||||||
|
|
||||||
public int getMassTons() {
|
public int getMassTons() {
|
||||||
return massTons;
|
return massTons;
|
||||||
}
|
}
|
||||||
|
|
||||||
public void setMassTons(int massTons) {
|
public void setMassTons(int massTons) {
|
||||||
this.massTons = massTons;
|
this.massTons = massTons;
|
||||||
}
|
}
|
||||||
|
@ -2,11 +2,7 @@ package com.iluwatar;
|
|||||||
|
|
||||||
public enum StarType {
|
public enum StarType {
|
||||||
|
|
||||||
SUN,
|
SUN, RED_GIANT, WHITE_DWARF, SUPERNOVA, DEAD;
|
||||||
RED_GIANT,
|
|
||||||
WHITE_DWARF,
|
|
||||||
SUPERNOVA,
|
|
||||||
DEAD;
|
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public String toString() {
|
public String toString() {
|
||||||
|
@ -5,8 +5,8 @@ import java.io.File;
|
|||||||
import java.io.FileReader;
|
import java.io.FileReader;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Every instance of this class represents the Model component
|
* Every instance of this class represents the Model component in the
|
||||||
* in the Model-View-Presenter architectural pattern.
|
* Model-View-Presenter architectural pattern.
|
||||||
*
|
*
|
||||||
* It is responsible for reading and loading the contents of a given file.
|
* It is responsible for reading and loading the contents of a given file.
|
||||||
*/
|
*/
|
||||||
@ -27,7 +27,8 @@ public class FileLoader {
|
|||||||
*/
|
*/
|
||||||
public String loadData() {
|
public String loadData() {
|
||||||
try {
|
try {
|
||||||
BufferedReader br = new BufferedReader(new FileReader(new File(this.fileName)));
|
BufferedReader br = new BufferedReader(new FileReader(new File(
|
||||||
|
this.fileName)));
|
||||||
String text = "";
|
String text = "";
|
||||||
String line = "";
|
String line = "";
|
||||||
|
|
||||||
@ -51,7 +52,8 @@ public class FileLoader {
|
|||||||
/**
|
/**
|
||||||
* Sets the path of the file to be loaded, to the given value.
|
* Sets the path of the file to be loaded, to the given value.
|
||||||
*
|
*
|
||||||
* @param fileName The path of the file to be loaded.
|
* @param fileName
|
||||||
|
* The path of the file to be loaded.
|
||||||
*/
|
*/
|
||||||
public void setFileName(String fileName) {
|
public void setFileName(String fileName) {
|
||||||
this.fileName = fileName;
|
this.fileName = fileName;
|
||||||
|
@ -14,10 +14,11 @@ import javax.swing.JTextArea;
|
|||||||
import javax.swing.JTextField;
|
import javax.swing.JTextField;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* This class is the GUI implementation of the View component
|
* This class is the GUI implementation of the View component In the
|
||||||
* In the Model-View-Presenter pattern.
|
* Model-View-Presenter pattern.
|
||||||
*/
|
*/
|
||||||
public class FileSelectorJFrame extends JFrame implements FileSelectorView, ActionListener {
|
public class FileSelectorJFrame extends JFrame implements FileSelectorView,
|
||||||
|
ActionListener {
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Default serial version ID.
|
* Default serial version ID.
|
||||||
@ -45,8 +46,7 @@ public class FileSelectorJFrame extends JFrame implements FileSelectorView, Acti
|
|||||||
private JLabel contents;
|
private JLabel contents;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* The text field for giving the name of the file
|
* The text field for giving the name of the file that we want to open.
|
||||||
* that we want to open.
|
|
||||||
*/
|
*/
|
||||||
private JTextField input;
|
private JTextField input;
|
||||||
|
|
||||||
|
@ -1,10 +1,11 @@
|
|||||||
package com.iluwatar;
|
package com.iluwatar;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Every instance of this class represents the Presenter component
|
* Every instance of this class represents the Presenter component in the
|
||||||
* in the Model-View-Presenter architectural pattern.
|
* Model-View-Presenter architectural pattern.
|
||||||
*
|
*
|
||||||
* It is responsible for reacting to the user's actions and update the View component.
|
* It is responsible for reacting to the user's actions and update the View
|
||||||
|
* component.
|
||||||
*/
|
*/
|
||||||
public class FileSelectorPresenter {
|
public class FileSelectorPresenter {
|
||||||
|
|
||||||
@ -21,7 +22,8 @@ public class FileSelectorPresenter {
|
|||||||
/**
|
/**
|
||||||
* Constructor
|
* Constructor
|
||||||
*
|
*
|
||||||
* @param view The view component that the presenter will interact with.
|
* @param view
|
||||||
|
* The view component that the presenter will interact with.
|
||||||
*/
|
*/
|
||||||
public FileSelectorPresenter(FileSelectorView view) {
|
public FileSelectorPresenter(FileSelectorView view) {
|
||||||
this.view = view;
|
this.view = view;
|
||||||
@ -30,7 +32,8 @@ public class FileSelectorPresenter {
|
|||||||
/**
|
/**
|
||||||
* Sets the FileLoader object, to the value given as parameter.
|
* Sets the FileLoader object, to the value given as parameter.
|
||||||
*
|
*
|
||||||
* @param loader The new FileLoader object(the Model component).
|
* @param loader
|
||||||
|
* The new FileLoader object(the Model component).
|
||||||
*/
|
*/
|
||||||
public void setLoader(FileLoader loader) {
|
public void setLoader(FileLoader loader) {
|
||||||
this.loader = loader;
|
this.loader = loader;
|
||||||
|
@ -1,14 +1,14 @@
|
|||||||
package com.iluwatar;
|
package com.iluwatar;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Every instance of this class represents the Stub component in
|
* Every instance of this class represents the Stub component in the
|
||||||
* the Model-View-Presenter architectural pattern.
|
* Model-View-Presenter architectural pattern.
|
||||||
*
|
*
|
||||||
* The stub implements the View interface and it is useful when
|
* The stub implements the View interface and it is useful when we want the test
|
||||||
* we want the test the reaction to user events, such as mouse clicks.
|
* the reaction to user events, such as mouse clicks.
|
||||||
*
|
*
|
||||||
* Since we can not test the GUI directly, the MVP pattern provides
|
* Since we can not test the GUI directly, the MVP pattern provides this
|
||||||
* this functionality through the View's dummy implementation, the Stub.
|
* functionality through the View's dummy implementation, the Stub.
|
||||||
*/
|
*/
|
||||||
public class FileSelectorStub implements FileSelectorView {
|
public class FileSelectorStub implements FileSelectorView {
|
||||||
|
|
||||||
|
@ -1,9 +1,8 @@
|
|||||||
package com.iluwatar;
|
package com.iluwatar;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* This interface represents the View component in the
|
* This interface represents the View component in the Model-View-Presenter
|
||||||
* Model-View-Presenter pattern. It can be implemented
|
* pattern. It can be implemented by either the GUI components, or by the Stub.
|
||||||
* by either the GUI components, or by the Stub.
|
|
||||||
*/
|
*/
|
||||||
public interface FileSelectorView {
|
public interface FileSelectorView {
|
||||||
|
|
||||||
@ -25,7 +24,8 @@ public interface FileSelectorView {
|
|||||||
/**
|
/**
|
||||||
* Sets the presenter component, to the one given as parameter.
|
* Sets the presenter component, to the one given as parameter.
|
||||||
*
|
*
|
||||||
* @param presenter The new presenter component.
|
* @param presenter
|
||||||
|
* The new presenter component.
|
||||||
*/
|
*/
|
||||||
public void setPresenter(FileSelectorPresenter presenter);
|
public void setPresenter(FileSelectorPresenter presenter);
|
||||||
|
|
||||||
@ -37,7 +37,8 @@ public interface FileSelectorView {
|
|||||||
/**
|
/**
|
||||||
* Sets the file's name, to the value given as parameter.
|
* Sets the file's name, to the value given as parameter.
|
||||||
*
|
*
|
||||||
* @param name The new name of the file.
|
* @param name
|
||||||
|
* The new name of the file.
|
||||||
*/
|
*/
|
||||||
public void setFileName(String name);
|
public void setFileName(String name);
|
||||||
|
|
||||||
@ -49,14 +50,16 @@ public interface FileSelectorView {
|
|||||||
/**
|
/**
|
||||||
* Displays a message to the users.
|
* Displays a message to the users.
|
||||||
*
|
*
|
||||||
* @param message The message to be displayed.
|
* @param message
|
||||||
|
* The message to be displayed.
|
||||||
*/
|
*/
|
||||||
public void showMessage(String message);
|
public void showMessage(String message);
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Displays the data to the view.
|
* Displays the data to the view.
|
||||||
*
|
*
|
||||||
* @param data The data to be written.
|
* @param data
|
||||||
|
* The data to be written.
|
||||||
*/
|
*/
|
||||||
public void displayData(String data);
|
public void displayData(String data);
|
||||||
}
|
}
|
||||||
|
@ -6,8 +6,8 @@ import org.junit.Before;
|
|||||||
import org.junit.Test;
|
import org.junit.Test;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* This test case is responsible for testing our application
|
* This test case is responsible for testing our application by taking advantage
|
||||||
* by taking advantage of the Model-View-Controller architectural pattern.
|
* of the Model-View-Controller architectural pattern.
|
||||||
*/
|
*/
|
||||||
public class FileSelectorPresenterTest {
|
public class FileSelectorPresenterTest {
|
||||||
|
|
||||||
@ -26,7 +26,6 @@ public class FileSelectorPresenterTest {
|
|||||||
*/
|
*/
|
||||||
private FileLoader loader;
|
private FileLoader loader;
|
||||||
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Initializes the components of the test case.
|
* Initializes the components of the test case.
|
||||||
*/
|
*/
|
||||||
@ -64,8 +63,8 @@ public class FileSelectorPresenterTest {
|
|||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Tests if we receive a confirmation when we attempt to open a file
|
* Tests if we receive a confirmation when we attempt to open a file that
|
||||||
* that it's name is null or an empty string.
|
* it's name is null or an empty string.
|
||||||
*/
|
*/
|
||||||
@Test
|
@Test
|
||||||
public void fileConfirmationWhenNameIsNull() {
|
public void fileConfirmationWhenNameIsNull() {
|
||||||
@ -80,8 +79,8 @@ public class FileSelectorPresenterTest {
|
|||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Tests if we receive a confirmation when we attempt to open a file
|
* Tests if we receive a confirmation when we attempt to open a file that it
|
||||||
* that it doesn't exist.
|
* doesn't exist.
|
||||||
*/
|
*/
|
||||||
@Test
|
@Test
|
||||||
public void fileConfirmationWhenFileDoesNotExist() {
|
public void fileConfirmationWhenFileDoesNotExist() {
|
||||||
|
@ -6,7 +6,8 @@ public class Hobbits implements WeatherObserver {
|
|||||||
public void update(WeatherType currentWeather) {
|
public void update(WeatherType currentWeather) {
|
||||||
switch (currentWeather) {
|
switch (currentWeather) {
|
||||||
case COLD:
|
case COLD:
|
||||||
System.out.println("The hobbits are shivering in the cold weather.");
|
System.out
|
||||||
|
.println("The hobbits are shivering in the cold weather.");
|
||||||
break;
|
break;
|
||||||
case RAINY:
|
case RAINY:
|
||||||
System.out.println("The hobbits look for cover from the rain.");
|
System.out.println("The hobbits look for cover from the rain.");
|
||||||
@ -15,7 +16,8 @@ public class Hobbits implements WeatherObserver {
|
|||||||
System.out.println("The happy hobbits bade in the warm sun.");
|
System.out.println("The happy hobbits bade in the warm sun.");
|
||||||
break;
|
break;
|
||||||
case WINDY:
|
case WINDY:
|
||||||
System.out.println("The hobbits hold their hats tightly in the windy weather.");
|
System.out
|
||||||
|
.println("The hobbits hold their hats tightly in the windy weather.");
|
||||||
break;
|
break;
|
||||||
default:
|
default:
|
||||||
break;
|
break;
|
||||||
|
@ -5,8 +5,8 @@ import java.util.List;
|
|||||||
|
|
||||||
/**
|
/**
|
||||||
*
|
*
|
||||||
* Weather can be observed by implementing WeatherObserver
|
* Weather can be observed by implementing WeatherObserver interface and
|
||||||
* interface and registering as listener.
|
* registering as listener.
|
||||||
*
|
*
|
||||||
*/
|
*/
|
||||||
public class Weather {
|
public class Weather {
|
||||||
|
@ -2,10 +2,7 @@ package com.iluwatar;
|
|||||||
|
|
||||||
public enum WeatherType {
|
public enum WeatherType {
|
||||||
|
|
||||||
SUNNY,
|
SUNNY, RAINY, WINDY, COLD;
|
||||||
RAINY,
|
|
||||||
WINDY,
|
|
||||||
COLD;
|
|
||||||
|
|
||||||
public String toString() {
|
public String toString() {
|
||||||
return this.name().toLowerCase();
|
return this.name().toLowerCase();
|
||||||
|
@ -15,7 +15,8 @@ public class App {
|
|||||||
Warlord warlord;
|
Warlord warlord;
|
||||||
Beast beast;
|
Beast beast;
|
||||||
|
|
||||||
factory = new HeroFactoryImpl(new ElfMage(), new ElfWarlord(), new ElfBeast());
|
factory = new HeroFactoryImpl(new ElfMage(), new ElfWarlord(),
|
||||||
|
new ElfBeast());
|
||||||
mage = factory.createMage();
|
mage = factory.createMage();
|
||||||
warlord = factory.createWarlord();
|
warlord = factory.createWarlord();
|
||||||
beast = factory.createBeast();
|
beast = factory.createBeast();
|
||||||
@ -23,7 +24,8 @@ public class App {
|
|||||||
System.out.println(warlord);
|
System.out.println(warlord);
|
||||||
System.out.println(beast);
|
System.out.println(beast);
|
||||||
|
|
||||||
factory = new HeroFactoryImpl(new OrcMage(), new OrcWarlord(), new OrcBeast());
|
factory = new HeroFactoryImpl(new OrcMage(), new OrcWarlord(),
|
||||||
|
new OrcBeast());
|
||||||
mage = factory.createMage();
|
mage = factory.createMage();
|
||||||
warlord = factory.createWarlord();
|
warlord = factory.createWarlord();
|
||||||
beast = factory.createBeast();
|
beast = factory.createBeast();
|
||||||
|
@ -9,7 +9,8 @@ public class IvoryTower {
|
|||||||
|
|
||||||
private static IvoryTower instance = new IvoryTower();
|
private static IvoryTower instance = new IvoryTower();
|
||||||
|
|
||||||
private IvoryTower() {}
|
private IvoryTower() {
|
||||||
|
}
|
||||||
|
|
||||||
public static IvoryTower getInstance() {
|
public static IvoryTower getInstance() {
|
||||||
return instance;
|
return instance;
|
||||||
|
@ -4,7 +4,8 @@ public class ProjectileStrategy implements DragonSlayingStrategy {
|
|||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void execute() {
|
public void execute() {
|
||||||
System.out.println("You shoot the dragon with the magical crossbow and it falls dead on the ground!");
|
System.out
|
||||||
|
.println("You shoot the dragon with the magical crossbow and it falls dead on the ground!");
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
@ -4,7 +4,8 @@ public class SpellStrategy implements DragonSlayingStrategy {
|
|||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void execute() {
|
public void execute() {
|
||||||
System.out.println("You cast the spell of disintegration and the dragon vaporizes in a pile of dust!");
|
System.out
|
||||||
|
.println("You cast the spell of disintegration and the dragon vaporizes in a pile of dust!");
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
@ -9,12 +9,14 @@ public class SubtleMethod extends StealingMethod {
|
|||||||
|
|
||||||
@Override
|
@Override
|
||||||
protected void confuseTarget(String target) {
|
protected void confuseTarget(String target) {
|
||||||
System.out.println("Approach the " + target + " with tears running and hug him!");
|
System.out.println("Approach the " + target
|
||||||
|
+ " with tears running and hug him!");
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
protected void stealTheItem(String target) {
|
protected void stealTheItem(String target) {
|
||||||
System.out.println("While in close contact grab the " + target + "'s wallet.");
|
System.out.println("While in close contact grab the " + target
|
||||||
|
+ "'s wallet.");
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
@ -11,9 +11,9 @@ public class App {
|
|||||||
|
|
||||||
public static void main(String[] args) {
|
public static void main(String[] args) {
|
||||||
|
|
||||||
Commander commander = new Commander(
|
Commander commander = new Commander(new Sergeant(new Soldier(),
|
||||||
new Sergeant(new Soldier(), new Soldier(), new Soldier()),
|
new Soldier(), new Soldier()), new Sergeant(new Soldier(),
|
||||||
new Sergeant(new Soldier(), new Soldier(), new Soldier()));
|
new Soldier(), new Soldier()));
|
||||||
commander.accept(new SoldierVisitor());
|
commander.accept(new SoldierVisitor());
|
||||||
commander.accept(new SergeantVisitor());
|
commander.accept(new SergeantVisitor());
|
||||||
commander.accept(new CommanderVisitor());
|
commander.accept(new CommanderVisitor());
|
||||||
|
@ -8,7 +8,9 @@ package com.iluwatar;
|
|||||||
public interface UnitVisitor {
|
public interface UnitVisitor {
|
||||||
|
|
||||||
void visitSoldier(Soldier soldier);
|
void visitSoldier(Soldier soldier);
|
||||||
|
|
||||||
void visitSergeant(Sergeant sergeant);
|
void visitSergeant(Sergeant sergeant);
|
||||||
|
|
||||||
void visitCommander(Commander commander);
|
void visitCommander(Commander commander);
|
||||||
|
|
||||||
}
|
}
|
||||||
|
Loading…
x
Reference in New Issue
Block a user