Formatted all files to the same standard
This commit is contained in:
parent
53a2a8b150
commit
3da9ad5469
@ -12,18 +12,18 @@ package com.iluwatar;
|
||||
*/
|
||||
public class App {
|
||||
|
||||
public static void main(String[] args) {
|
||||
createKingdom(new ElfKingdomFactory());
|
||||
createKingdom(new OrcKingdomFactory());
|
||||
}
|
||||
public static void main(String[] args) {
|
||||
createKingdom(new ElfKingdomFactory());
|
||||
createKingdom(new OrcKingdomFactory());
|
||||
}
|
||||
|
||||
public static void createKingdom(KingdomFactory factory) {
|
||||
King king = factory.createKing();
|
||||
Castle castle = factory.createCastle();
|
||||
Army army = factory.createArmy();
|
||||
System.out.println("The kingdom was created.");
|
||||
System.out.println(king);
|
||||
System.out.println(castle);
|
||||
System.out.println(army);
|
||||
}
|
||||
public static void createKingdom(KingdomFactory factory) {
|
||||
King king = factory.createKing();
|
||||
Castle castle = factory.createCastle();
|
||||
Army army = factory.createArmy();
|
||||
System.out.println("The kingdom was created.");
|
||||
System.out.println(king);
|
||||
System.out.println(castle);
|
||||
System.out.println(army);
|
||||
}
|
||||
}
|
||||
|
@ -8,7 +8,9 @@ package com.iluwatar;
|
||||
public interface KingdomFactory {
|
||||
|
||||
Castle createCastle();
|
||||
|
||||
King createKing();
|
||||
|
||||
Army createArmy();
|
||||
|
||||
}
|
||||
|
@ -14,8 +14,8 @@ package com.iluwatar;
|
||||
*/
|
||||
public class App {
|
||||
|
||||
public static void main(String[] args) {
|
||||
GnomeEngineeringManager manager = new GnomeEngineeringManager();
|
||||
manager.operateDevice();
|
||||
}
|
||||
public static void main(String[] args) {
|
||||
GnomeEngineeringManager manager = new GnomeEngineeringManager();
|
||||
manager.operateDevice();
|
||||
}
|
||||
}
|
||||
|
@ -2,9 +2,8 @@ package com.iluwatar;
|
||||
|
||||
/**
|
||||
*
|
||||
* Adapter class. Adapts the interface of the device
|
||||
* (GoblinGlider) into Engineer interface expected
|
||||
* by the client (GnomeEngineeringManager).
|
||||
* Adapter class. Adapts the interface of the device (GoblinGlider) into
|
||||
* Engineer interface expected by the client (GnomeEngineeringManager).
|
||||
*
|
||||
*/
|
||||
public class GnomeEngineer implements Engineer {
|
||||
|
@ -2,8 +2,7 @@ package com.iluwatar;
|
||||
|
||||
/**
|
||||
*
|
||||
* GnomeEngineering manager uses Engineer to
|
||||
* operate devices.
|
||||
* GnomeEngineering manager uses Engineer to operate devices.
|
||||
*
|
||||
*/
|
||||
public class GnomeEngineeringManager implements Engineer {
|
||||
|
@ -9,24 +9,27 @@ package com.iluwatar;
|
||||
*/
|
||||
public class App {
|
||||
|
||||
public static void main(String[] args) {
|
||||
BlindingMagicWeapon blindingMagicWeapon = new BlindingMagicWeapon(new Excalibur());
|
||||
blindingMagicWeapon.wield();
|
||||
blindingMagicWeapon.blind();
|
||||
blindingMagicWeapon.swing();
|
||||
blindingMagicWeapon.unwield();
|
||||
public static void main(String[] args) {
|
||||
BlindingMagicWeapon blindingMagicWeapon = new BlindingMagicWeapon(
|
||||
new Excalibur());
|
||||
blindingMagicWeapon.wield();
|
||||
blindingMagicWeapon.blind();
|
||||
blindingMagicWeapon.swing();
|
||||
blindingMagicWeapon.unwield();
|
||||
|
||||
FlyingMagicWeapon flyingMagicWeapon = new FlyingMagicWeapon(new Mjollnir());
|
||||
flyingMagicWeapon.wield();
|
||||
flyingMagicWeapon.fly();
|
||||
flyingMagicWeapon.swing();
|
||||
flyingMagicWeapon.unwield();
|
||||
FlyingMagicWeapon flyingMagicWeapon = new FlyingMagicWeapon(
|
||||
new Mjollnir());
|
||||
flyingMagicWeapon.wield();
|
||||
flyingMagicWeapon.fly();
|
||||
flyingMagicWeapon.swing();
|
||||
flyingMagicWeapon.unwield();
|
||||
|
||||
SoulEatingMagicWeapon soulEatingMagicWeapon = new SoulEatingMagicWeapon(new Stormbringer());
|
||||
soulEatingMagicWeapon.wield();
|
||||
soulEatingMagicWeapon.swing();
|
||||
soulEatingMagicWeapon.eatSoul();
|
||||
soulEatingMagicWeapon.unwield();
|
||||
SoulEatingMagicWeapon soulEatingMagicWeapon = new SoulEatingMagicWeapon(
|
||||
new Stormbringer());
|
||||
soulEatingMagicWeapon.wield();
|
||||
soulEatingMagicWeapon.swing();
|
||||
soulEatingMagicWeapon.eatSoul();
|
||||
soulEatingMagicWeapon.unwield();
|
||||
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@ -19,7 +19,8 @@ public class Excalibur extends BlindingMagicWeaponImp {
|
||||
|
||||
@Override
|
||||
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
|
||||
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");
|
||||
}
|
||||
|
||||
}
|
||||
|
@ -17,27 +17,22 @@ import com.iluwatar.Hero.HeroBuilder;
|
||||
*/
|
||||
public class App {
|
||||
|
||||
public static void main(String[] args) {
|
||||
public static void main(String[] args) {
|
||||
|
||||
Hero mage = new HeroBuilder(Profession.MAGE, "Riobard")
|
||||
.withHairColor(HairColor.BLACK)
|
||||
.withWeapon(Weapon.DAGGER)
|
||||
.build();
|
||||
System.out.println(mage);
|
||||
Hero mage = new HeroBuilder(Profession.MAGE, "Riobard")
|
||||
.withHairColor(HairColor.BLACK).withWeapon(Weapon.DAGGER)
|
||||
.build();
|
||||
System.out.println(mage);
|
||||
|
||||
Hero warrior = new HeroBuilder(Profession.WARRIOR, "Amberjill")
|
||||
.withHairColor(HairColor.BLOND)
|
||||
.withHairType(HairType.LONG_CURLY)
|
||||
.withArmor(Armor.CHAIN_MAIL)
|
||||
.withWeapon(Weapon.SWORD)
|
||||
.build();
|
||||
System.out.println(warrior);
|
||||
Hero warrior = new HeroBuilder(Profession.WARRIOR, "Amberjill")
|
||||
.withHairColor(HairColor.BLOND)
|
||||
.withHairType(HairType.LONG_CURLY).withArmor(Armor.CHAIN_MAIL)
|
||||
.withWeapon(Weapon.SWORD).build();
|
||||
System.out.println(warrior);
|
||||
|
||||
Hero thief = new HeroBuilder(Profession.THIEF, "Desmond")
|
||||
.withHairType(HairType.BALD)
|
||||
.withWeapon(Weapon.BOW)
|
||||
.build();
|
||||
System.out.println(thief);
|
||||
Hero thief = new HeroBuilder(Profession.THIEF, "Desmond")
|
||||
.withHairType(HairType.BALD).withWeapon(Weapon.BOW).build();
|
||||
System.out.println(thief);
|
||||
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@ -7,11 +7,19 @@ public enum Armor {
|
||||
@Override
|
||||
public String toString() {
|
||||
String s = "";
|
||||
switch(this) {
|
||||
case CLOTHES: s = "clothes"; break;
|
||||
case LEATHER: s = "leather armor"; break;
|
||||
case CHAIN_MAIL: s = "chain mail"; break;
|
||||
case PLATE_MAIL: s = "plate mail"; break;
|
||||
switch (this) {
|
||||
case CLOTHES:
|
||||
s = "clothes";
|
||||
break;
|
||||
case LEATHER:
|
||||
s = "leather armor";
|
||||
break;
|
||||
case CHAIN_MAIL:
|
||||
s = "chain mail";
|
||||
break;
|
||||
case PLATE_MAIL:
|
||||
s = "plate mail";
|
||||
break;
|
||||
}
|
||||
return s;
|
||||
}
|
||||
|
@ -7,12 +7,22 @@ public enum HairColor {
|
||||
@Override
|
||||
public String toString() {
|
||||
String s = "";
|
||||
switch(this) {
|
||||
case WHITE: s = "white"; break;
|
||||
case BLOND: s = "blond"; break;
|
||||
case RED: s = "red"; break;
|
||||
case BROWN: s = "brown"; break;
|
||||
case BLACK: s = "black"; break;
|
||||
switch (this) {
|
||||
case WHITE:
|
||||
s = "white";
|
||||
break;
|
||||
case BLOND:
|
||||
s = "blond";
|
||||
break;
|
||||
case RED:
|
||||
s = "red";
|
||||
break;
|
||||
case BROWN:
|
||||
s = "brown";
|
||||
break;
|
||||
case BLACK:
|
||||
s = "black";
|
||||
break;
|
||||
}
|
||||
return s;
|
||||
}
|
||||
|
@ -7,12 +7,22 @@ public enum HairType {
|
||||
@Override
|
||||
public String toString() {
|
||||
String s = "";
|
||||
switch(this) {
|
||||
case BALD: s = "bold"; break;
|
||||
case SHORT: s = "short"; break;
|
||||
case CURLY: s = "curly"; break;
|
||||
case LONG_STRAIGHT: s = "long straight"; break;
|
||||
case LONG_CURLY: s = "long curly"; break;
|
||||
switch (this) {
|
||||
case BALD:
|
||||
s = "bold";
|
||||
break;
|
||||
case SHORT:
|
||||
s = "short";
|
||||
break;
|
||||
case CURLY:
|
||||
s = "curly";
|
||||
break;
|
||||
case LONG_STRAIGHT:
|
||||
s = "long straight";
|
||||
break;
|
||||
case LONG_CURLY:
|
||||
s = "long curly";
|
||||
break;
|
||||
}
|
||||
return s;
|
||||
}
|
||||
|
@ -94,7 +94,8 @@ public class Hero {
|
||||
|
||||
public HeroBuilder(Profession profession, String name) {
|
||||
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.name = name;
|
||||
|
@ -7,11 +7,19 @@ public enum Profession {
|
||||
@Override
|
||||
public String toString() {
|
||||
String s = "";
|
||||
switch(this) {
|
||||
case WARRIOR: s = "Warrior"; break;
|
||||
case THIEF: s = "Thief"; break;
|
||||
case MAGE: s = "Mage"; break;
|
||||
case PRIEST: s = "Priest"; break;
|
||||
switch (this) {
|
||||
case WARRIOR:
|
||||
s = "Warrior";
|
||||
break;
|
||||
case THIEF:
|
||||
s = "Thief";
|
||||
break;
|
||||
case MAGE:
|
||||
s = "Mage";
|
||||
break;
|
||||
case PRIEST:
|
||||
s = "Priest";
|
||||
break;
|
||||
}
|
||||
return s;
|
||||
}
|
||||
|
@ -7,12 +7,22 @@ public enum Weapon {
|
||||
@Override
|
||||
public String toString() {
|
||||
String s = "";
|
||||
switch(this) {
|
||||
case DAGGER: s = "dagger"; break;
|
||||
case SWORD: s = "sword"; break;
|
||||
case AXE: s = "axe"; break;
|
||||
case WARHAMMER: s = "warhammer"; break;
|
||||
case BOW: s = "bow"; break;
|
||||
switch (this) {
|
||||
case DAGGER:
|
||||
s = "dagger";
|
||||
break;
|
||||
case SWORD:
|
||||
s = "sword";
|
||||
break;
|
||||
case AXE:
|
||||
s = "axe";
|
||||
break;
|
||||
case WARHAMMER:
|
||||
s = "warhammer";
|
||||
break;
|
||||
case BOW:
|
||||
s = "bow";
|
||||
break;
|
||||
}
|
||||
return s;
|
||||
}
|
||||
|
@ -10,12 +10,13 @@ package com.iluwatar;
|
||||
*/
|
||||
public class App {
|
||||
|
||||
public static void main(String[] args) {
|
||||
public static void main(String[] args) {
|
||||
|
||||
OrcKing king = new OrcKing();
|
||||
king.makeRequest(new Request(RequestType.DEFEND_CASTLE, "defend castle"));
|
||||
king.makeRequest(new Request(RequestType.TORTURE_PRISONER, "torture prisoner"));
|
||||
king.makeRequest(new Request(RequestType.COLLECT_TAX, "collect tax"));
|
||||
OrcKing king = new OrcKing();
|
||||
king.makeRequest(new Request(RequestType.DEFEND_CASTLE, "defend castle"));
|
||||
king.makeRequest(new Request(RequestType.TORTURE_PRISONER,
|
||||
"torture prisoner"));
|
||||
king.makeRequest(new Request(RequestType.COLLECT_TAX, "collect tax"));
|
||||
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@ -2,8 +2,6 @@ package com.iluwatar;
|
||||
|
||||
public enum RequestType {
|
||||
|
||||
DEFEND_CASTLE,
|
||||
TORTURE_PRISONER,
|
||||
COLLECT_TAX
|
||||
DEFEND_CASTLE, TORTURE_PRISONER, COLLECT_TAX
|
||||
|
||||
}
|
||||
|
@ -8,18 +8,18 @@ package com.iluwatar;
|
||||
*/
|
||||
public class App {
|
||||
|
||||
public static void main(String[] args) {
|
||||
Wizard wizard = new Wizard();
|
||||
Goblin goblin = new Goblin();
|
||||
public static void main(String[] args) {
|
||||
Wizard wizard = new Wizard();
|
||||
Goblin goblin = new Goblin();
|
||||
|
||||
goblin.printStatus();
|
||||
goblin.printStatus();
|
||||
|
||||
wizard.castSpell(new ShrinkSpell(), goblin);
|
||||
goblin.printStatus();
|
||||
wizard.castSpell(new ShrinkSpell(), goblin);
|
||||
goblin.printStatus();
|
||||
|
||||
wizard.castSpell(new InvisibilitySpell(), goblin);
|
||||
goblin.printStatus();
|
||||
wizard.undoLastSpell();
|
||||
goblin.printStatus();
|
||||
}
|
||||
wizard.castSpell(new InvisibilitySpell(), goblin);
|
||||
goblin.printStatus();
|
||||
wizard.undoLastSpell();
|
||||
goblin.printStatus();
|
||||
}
|
||||
}
|
||||
|
@ -2,9 +2,7 @@ package com.iluwatar;
|
||||
|
||||
public enum Size {
|
||||
|
||||
SMALL,
|
||||
NORMAL,
|
||||
LARGE;
|
||||
SMALL, NORMAL, LARGE;
|
||||
|
||||
@Override
|
||||
public String toString() {
|
||||
@ -27,5 +25,4 @@ public enum Size {
|
||||
return s;
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
|
@ -26,6 +26,7 @@ public abstract class Target {
|
||||
public abstract String toString();
|
||||
|
||||
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 {
|
||||
|
||||
VISIBLE,
|
||||
INVISIBLE;
|
||||
VISIBLE, INVISIBLE;
|
||||
|
||||
@Override
|
||||
public String toString() {
|
||||
|
@ -9,17 +9,17 @@ package com.iluwatar;
|
||||
*/
|
||||
public class App {
|
||||
|
||||
public static void main(String[] args) {
|
||||
System.out.println("Message from the orcs: ");
|
||||
public static void main(String[] args) {
|
||||
System.out.println("Message from the orcs: ");
|
||||
|
||||
LetterComposite orcMessage = new Messenger().messageFromOrcs();
|
||||
orcMessage.print();
|
||||
LetterComposite orcMessage = new Messenger().messageFromOrcs();
|
||||
orcMessage.print();
|
||||
|
||||
System.out.println("\n");
|
||||
System.out.println("\n");
|
||||
|
||||
System.out.println("Message from the elves: ");
|
||||
System.out.println("Message from the elves: ");
|
||||
|
||||
LetterComposite elfMessage = new Messenger().messageFromElves();
|
||||
elfMessage.print();
|
||||
}
|
||||
LetterComposite elfMessage = new Messenger().messageFromElves();
|
||||
elfMessage.print();
|
||||
}
|
||||
}
|
||||
|
@ -26,7 +26,7 @@ public abstract class LetterComposite {
|
||||
|
||||
public void print() {
|
||||
printThisBefore();
|
||||
for (LetterComposite letter: children) {
|
||||
for (LetterComposite letter : children) {
|
||||
letter.print();
|
||||
}
|
||||
printThisAfter();
|
||||
|
@ -10,15 +10,20 @@ public class Messenger {
|
||||
|
||||
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('t'), new Letter('h'), new Letter('e'), new Letter('r'), new Letter('e'))));
|
||||
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('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('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('t'), new Letter('h'), new Letter('e'), new Letter('r'), new Letter('e'))));
|
||||
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('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('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);
|
||||
|
||||
@ -28,12 +33,18 @@ public class Messenger {
|
||||
|
||||
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('w'), new Letter('i'), new Letter('n'), new Letter('d'))));
|
||||
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('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'))));
|
||||
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('w'), new Letter('i'),
|
||||
new Letter('n'), new Letter('d'))));
|
||||
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('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);
|
||||
|
||||
|
@ -5,7 +5,7 @@ import java.util.List;
|
||||
public class Sentence extends LetterComposite {
|
||||
|
||||
public Sentence(List<Word> words) {
|
||||
for (Word w: words) {
|
||||
for (Word w : words) {
|
||||
this.add(w);
|
||||
}
|
||||
}
|
||||
@ -20,5 +20,4 @@ public class Sentence extends LetterComposite {
|
||||
System.out.print(".");
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
|
@ -5,7 +5,7 @@ import java.util.List;
|
||||
public class Word extends LetterComposite {
|
||||
|
||||
public Word(List<Letter> letters) {
|
||||
for (Letter l: letters) {
|
||||
for (Letter l : letters) {
|
||||
this.add(l);
|
||||
}
|
||||
}
|
||||
|
@ -9,16 +9,16 @@ package com.iluwatar;
|
||||
*/
|
||||
public class App {
|
||||
|
||||
public static void main(String[] args) {
|
||||
public static void main(String[] args) {
|
||||
|
||||
System.out.println("A simple looking troll approaches.");
|
||||
Hostile troll = new Troll();
|
||||
troll.attack();
|
||||
troll.fleeBattle();
|
||||
System.out.println("A simple looking troll approaches.");
|
||||
Hostile troll = new Troll();
|
||||
troll.attack();
|
||||
troll.fleeBattle();
|
||||
|
||||
System.out.println("\nA smart looking troll surprises you.");
|
||||
Hostile smart = new SmartTroll(new Troll());
|
||||
smart.attack();
|
||||
smart.fleeBattle();
|
||||
}
|
||||
System.out.println("\nA smart looking troll surprises you.");
|
||||
Hostile smart = new SmartTroll(new Troll());
|
||||
smart.attack();
|
||||
smart.fleeBattle();
|
||||
}
|
||||
}
|
||||
|
@ -3,6 +3,7 @@ package com.iluwatar;
|
||||
public interface Hostile {
|
||||
|
||||
void attack();
|
||||
|
||||
void fleeBattle();
|
||||
|
||||
}
|
||||
|
@ -12,16 +12,17 @@ import java.util.concurrent.Executors;
|
||||
*/
|
||||
public class App {
|
||||
|
||||
public static void main(String[] args) {
|
||||
final Inventory inventory = new Inventory(1000);
|
||||
ExecutorService executorService = Executors.newFixedThreadPool(3);
|
||||
for (int i = 0; i < 3; i++) {
|
||||
executorService.execute(new Runnable() {
|
||||
@Override
|
||||
public void run() {
|
||||
while (inventory.addItem(new Item()));
|
||||
}
|
||||
});
|
||||
}
|
||||
}
|
||||
public static void main(String[] args) {
|
||||
final Inventory inventory = new Inventory(1000);
|
||||
ExecutorService executorService = Executors.newFixedThreadPool(3);
|
||||
for (int i = 0; i < 3; i++) {
|
||||
executorService.execute(new Runnable() {
|
||||
@Override
|
||||
public void run() {
|
||||
while (inventory.addItem(new Item()))
|
||||
;
|
||||
}
|
||||
});
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@ -5,7 +5,6 @@ import java.util.List;
|
||||
import java.util.concurrent.locks.Lock;
|
||||
import java.util.concurrent.locks.ReentrantLock;
|
||||
|
||||
|
||||
public class Inventory {
|
||||
|
||||
private int inventorySize;
|
||||
@ -17,16 +16,16 @@ public class Inventory {
|
||||
this.items = new ArrayList<Item>(inventorySize);
|
||||
}
|
||||
|
||||
public boolean addItem(Item item){
|
||||
if(items.size()<inventorySize){
|
||||
public boolean addItem(Item item) {
|
||||
if (items.size() < inventorySize) {
|
||||
lock.lock();
|
||||
try{
|
||||
if(items.size()<inventorySize){
|
||||
try {
|
||||
if (items.size() < inventorySize) {
|
||||
items.add(item);
|
||||
System.out.println(Thread.currentThread());
|
||||
return true;
|
||||
}
|
||||
}finally{
|
||||
} finally {
|
||||
lock.unlock();
|
||||
}
|
||||
}
|
||||
|
@ -7,10 +7,10 @@ package com.iluwatar;
|
||||
*/
|
||||
public class App {
|
||||
|
||||
public static void main(String[] args) {
|
||||
DwarvenGoldmineFacade facade = new DwarvenGoldmineFacade();
|
||||
facade.startNewDay();
|
||||
facade.digOutGold();
|
||||
facade.endDay();
|
||||
}
|
||||
public static void main(String[] args) {
|
||||
DwarvenGoldmineFacade facade = new DwarvenGoldmineFacade();
|
||||
facade.startNewDay();
|
||||
facade.digOutGold();
|
||||
facade.endDay();
|
||||
}
|
||||
}
|
||||
|
@ -15,20 +15,20 @@ public class DwarvenGoldmineFacade {
|
||||
}
|
||||
|
||||
public void startNewDay() {
|
||||
for (DwarvenMineWorker worker: workers) {
|
||||
for (DwarvenMineWorker worker : workers) {
|
||||
worker.wakeUp();
|
||||
worker.goToMine();
|
||||
}
|
||||
}
|
||||
|
||||
public void digOutGold() {
|
||||
for (DwarvenMineWorker worker: workers) {
|
||||
for (DwarvenMineWorker worker : workers) {
|
||||
worker.work();
|
||||
}
|
||||
}
|
||||
|
||||
public void endDay() {
|
||||
for (DwarvenMineWorker worker: workers) {
|
||||
for (DwarvenMineWorker worker : workers) {
|
||||
worker.goHome();
|
||||
worker.goToSleep();
|
||||
}
|
||||
|
@ -9,20 +9,20 @@ package com.iluwatar;
|
||||
*/
|
||||
public class App {
|
||||
|
||||
public static void main(String[] args) {
|
||||
Blacksmith blacksmith;
|
||||
Weapon weapon;
|
||||
public static void main(String[] args) {
|
||||
Blacksmith blacksmith;
|
||||
Weapon weapon;
|
||||
|
||||
blacksmith = new OrcBlacksmith();
|
||||
weapon = blacksmith.manufactureWeapon(WeaponType.SPEAR);
|
||||
System.out.println(weapon);
|
||||
weapon = blacksmith.manufactureWeapon(WeaponType.AXE);
|
||||
System.out.println(weapon);
|
||||
blacksmith = new OrcBlacksmith();
|
||||
weapon = blacksmith.manufactureWeapon(WeaponType.SPEAR);
|
||||
System.out.println(weapon);
|
||||
weapon = blacksmith.manufactureWeapon(WeaponType.AXE);
|
||||
System.out.println(weapon);
|
||||
|
||||
blacksmith = new ElfBlacksmith();
|
||||
weapon = blacksmith.manufactureWeapon(WeaponType.SHORT_SWORD);
|
||||
System.out.println(weapon);
|
||||
weapon = blacksmith.manufactureWeapon(WeaponType.SPEAR);
|
||||
System.out.println(weapon);
|
||||
}
|
||||
blacksmith = new ElfBlacksmith();
|
||||
weapon = blacksmith.manufactureWeapon(WeaponType.SHORT_SWORD);
|
||||
System.out.println(weapon);
|
||||
weapon = blacksmith.manufactureWeapon(WeaponType.SPEAR);
|
||||
System.out.println(weapon);
|
||||
}
|
||||
}
|
||||
|
@ -7,13 +7,18 @@ public enum WeaponType {
|
||||
@Override
|
||||
public String toString() {
|
||||
String s = "";
|
||||
switch(this) {
|
||||
case SHORT_SWORD: s = "short sword"; break;
|
||||
case SPEAR: s = "spear"; break;
|
||||
case AXE: s = "axe"; break;
|
||||
switch (this) {
|
||||
case SHORT_SWORD:
|
||||
s = "short sword";
|
||||
break;
|
||||
case SPEAR:
|
||||
s = "spear";
|
||||
break;
|
||||
case AXE:
|
||||
s = "axe";
|
||||
break;
|
||||
}
|
||||
return s;
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
|
@ -43,13 +43,13 @@ public class AlchemistShop {
|
||||
|
||||
System.out.println("Enumerating top shelf potions\n");
|
||||
|
||||
for (Potion p: topShelf) {
|
||||
for (Potion p : topShelf) {
|
||||
p.drink();
|
||||
}
|
||||
|
||||
System.out.println("\nEnumerating bottom shelf potions\n");
|
||||
|
||||
for (Potion p: bottomShelf) {
|
||||
for (Potion p : bottomShelf) {
|
||||
p.drink();
|
||||
}
|
||||
|
||||
|
@ -9,8 +9,8 @@ package com.iluwatar;
|
||||
*/
|
||||
public class App {
|
||||
|
||||
public static void main(String[] args) {
|
||||
AlchemistShop alchemistShop = new AlchemistShop();
|
||||
alchemistShop.enumerate();
|
||||
}
|
||||
public static void main(String[] args) {
|
||||
AlchemistShop alchemistShop = new AlchemistShop();
|
||||
alchemistShop.enumerate();
|
||||
}
|
||||
}
|
||||
|
@ -4,7 +4,8 @@ public class HealingPotion implements Potion {
|
||||
|
||||
@Override
|
||||
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
|
||||
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
|
||||
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
|
||||
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 {
|
||||
|
||||
HEALING,
|
||||
INVISIBILITY,
|
||||
STRENGTH,
|
||||
HOLY_WATER,
|
||||
POISON;
|
||||
HEALING, INVISIBILITY, STRENGTH, HOLY_WATER, POISON;
|
||||
|
||||
}
|
||||
|
@ -4,7 +4,8 @@ public class StrengthPotion implements Potion {
|
||||
|
||||
@Override
|
||||
public void drink() {
|
||||
System.out.println("You feel strong. (Potion=" + System.identityHashCode(this) + ")");
|
||||
System.out.println("You feel strong. (Potion="
|
||||
+ System.identityHashCode(this) + ")");
|
||||
}
|
||||
|
||||
}
|
||||
|
@ -10,57 +10,63 @@ import java.util.Stack;
|
||||
*/
|
||||
public class App {
|
||||
|
||||
/**
|
||||
*
|
||||
* Expressions can be evaluated using prefix, infix or postfix notations
|
||||
* This sample uses postfix, where operator comes after the operands
|
||||
*
|
||||
*/
|
||||
public static void main(String[] args) {
|
||||
String tokenString = "4 3 2 - 1 + *";
|
||||
Stack<Expression> stack = new Stack<>();
|
||||
/**
|
||||
*
|
||||
* Expressions can be evaluated using prefix, infix or postfix notations
|
||||
* This sample uses postfix, where operator comes after the operands
|
||||
*
|
||||
*/
|
||||
public static void main(String[] args) {
|
||||
String tokenString = "4 3 2 - 1 + *";
|
||||
Stack<Expression> stack = new Stack<>();
|
||||
|
||||
String[] tokenList = tokenString.split(" ");
|
||||
for (String s : tokenList) {
|
||||
if (isOperator(s)) {
|
||||
Expression rightExpression = stack.pop();
|
||||
Expression leftExpression = stack.pop();
|
||||
System.out.println(String.format("popped from stack left: %d right: %d",
|
||||
leftExpression.interpret(), rightExpression.interpret()));
|
||||
Expression operator = getOperatorInstance(s, leftExpression,
|
||||
rightExpression);
|
||||
System.out.println(String.format("operator: %s", operator));
|
||||
int result = operator.interpret();
|
||||
NumberExpression resultExpression = new NumberExpression(result);
|
||||
stack.push(resultExpression);
|
||||
System.out.println(String.format("push result to stack: %d", resultExpression.interpret()));
|
||||
} else {
|
||||
Expression i = new NumberExpression(s);
|
||||
stack.push(i);
|
||||
System.out.println(String.format("push to stack: %d", i.interpret()));
|
||||
}
|
||||
}
|
||||
System.out.println(String.format("result: %d", stack.pop().interpret()));
|
||||
}
|
||||
String[] tokenList = tokenString.split(" ");
|
||||
for (String s : tokenList) {
|
||||
if (isOperator(s)) {
|
||||
Expression rightExpression = stack.pop();
|
||||
Expression leftExpression = stack.pop();
|
||||
System.out
|
||||
.println(String.format(
|
||||
"popped from stack left: %d right: %d",
|
||||
leftExpression.interpret(),
|
||||
rightExpression.interpret()));
|
||||
Expression operator = getOperatorInstance(s, leftExpression,
|
||||
rightExpression);
|
||||
System.out.println(String.format("operator: %s", operator));
|
||||
int result = operator.interpret();
|
||||
NumberExpression resultExpression = new NumberExpression(result);
|
||||
stack.push(resultExpression);
|
||||
System.out.println(String.format("push result to stack: %d",
|
||||
resultExpression.interpret()));
|
||||
} else {
|
||||
Expression i = new NumberExpression(s);
|
||||
stack.push(i);
|
||||
System.out.println(String.format("push to stack: %d",
|
||||
i.interpret()));
|
||||
}
|
||||
}
|
||||
System.out
|
||||
.println(String.format("result: %d", stack.pop().interpret()));
|
||||
}
|
||||
|
||||
public static boolean isOperator(String s) {
|
||||
if (s.equals("+") || s.equals("-") || s.equals("*")) {
|
||||
return true;
|
||||
} else {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
public static boolean isOperator(String s) {
|
||||
if (s.equals("+") || s.equals("-") || s.equals("*")) {
|
||||
return true;
|
||||
} else {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
public static Expression getOperatorInstance(String s, Expression left,
|
||||
Expression right) {
|
||||
switch (s) {
|
||||
case "+":
|
||||
return new PlusExpression(left, right);
|
||||
case "-":
|
||||
return new MinusExpression(left, right);
|
||||
case "*":
|
||||
return new MultiplyExpression(left, right);
|
||||
}
|
||||
return null;
|
||||
}
|
||||
public static Expression getOperatorInstance(String s, Expression left,
|
||||
Expression right) {
|
||||
switch (s) {
|
||||
case "+":
|
||||
return new PlusExpression(left, right);
|
||||
case "-":
|
||||
return new MinusExpression(left, right);
|
||||
case "*":
|
||||
return new MultiplyExpression(left, right);
|
||||
}
|
||||
return null;
|
||||
}
|
||||
}
|
||||
|
@ -5,7 +5,8 @@ public class MultiplyExpression extends Expression {
|
||||
private Expression leftExpression;
|
||||
private Expression rightExpression;
|
||||
|
||||
public MultiplyExpression(Expression leftExpression, Expression rightExpression) {
|
||||
public MultiplyExpression(Expression leftExpression,
|
||||
Expression rightExpression) {
|
||||
this.leftExpression = leftExpression;
|
||||
this.rightExpression = rightExpression;
|
||||
}
|
||||
|
@ -9,33 +9,33 @@ package com.iluwatar;
|
||||
*/
|
||||
public class App {
|
||||
|
||||
public static void main(String[] args) {
|
||||
TreasureChest chest = new TreasureChest();
|
||||
public static void main(String[] args) {
|
||||
TreasureChest chest = new TreasureChest();
|
||||
|
||||
ItemIterator ringIterator = chest.Iterator(ItemType.RING);
|
||||
while (ringIterator.hasNext()) {
|
||||
System.out.println(ringIterator.next());
|
||||
}
|
||||
ItemIterator ringIterator = chest.Iterator(ItemType.RING);
|
||||
while (ringIterator.hasNext()) {
|
||||
System.out.println(ringIterator.next());
|
||||
}
|
||||
|
||||
System.out.println("----------");
|
||||
System.out.println("----------");
|
||||
|
||||
ItemIterator potionIterator = chest.Iterator(ItemType.POTION);
|
||||
while (potionIterator.hasNext()) {
|
||||
System.out.println(potionIterator.next());
|
||||
}
|
||||
ItemIterator potionIterator = chest.Iterator(ItemType.POTION);
|
||||
while (potionIterator.hasNext()) {
|
||||
System.out.println(potionIterator.next());
|
||||
}
|
||||
|
||||
System.out.println("----------");
|
||||
System.out.println("----------");
|
||||
|
||||
ItemIterator weaponIterator = chest.Iterator(ItemType.WEAPON);
|
||||
while (weaponIterator.hasNext()) {
|
||||
System.out.println(weaponIterator.next());
|
||||
}
|
||||
ItemIterator weaponIterator = chest.Iterator(ItemType.WEAPON);
|
||||
while (weaponIterator.hasNext()) {
|
||||
System.out.println(weaponIterator.next());
|
||||
}
|
||||
|
||||
System.out.println("----------");
|
||||
System.out.println("----------");
|
||||
|
||||
ItemIterator it = chest.Iterator(ItemType.ANY);
|
||||
while (it.hasNext()) {
|
||||
System.out.println(it.next());
|
||||
}
|
||||
}
|
||||
ItemIterator it = chest.Iterator(ItemType.ANY);
|
||||
while (it.hasNext()) {
|
||||
System.out.println(it.next());
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@ -2,9 +2,6 @@ package com.iluwatar;
|
||||
|
||||
public enum ItemType {
|
||||
|
||||
ANY,
|
||||
WEAPON,
|
||||
RING,
|
||||
POTION
|
||||
ANY, WEAPON, RING, POTION
|
||||
|
||||
}
|
||||
|
@ -39,7 +39,8 @@ public class TreasureChestItemIterator implements ItemIterator {
|
||||
tempIdx = -1;
|
||||
break;
|
||||
}
|
||||
if (type.equals(ItemType.ANY) || items.get(tempIdx).getType().equals(type)) {
|
||||
if (type.equals(ItemType.ANY)
|
||||
|| items.get(tempIdx).getType().equals(type)) {
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
@ -8,21 +8,21 @@ package com.iluwatar;
|
||||
*/
|
||||
public class App {
|
||||
|
||||
public static void main(String[] args) {
|
||||
Party party = new PartyImpl();
|
||||
Hobbit hobbit = new Hobbit();
|
||||
Wizard wizard = new Wizard();
|
||||
Rogue rogue = new Rogue();
|
||||
Hunter hunter = new Hunter();
|
||||
public static void main(String[] args) {
|
||||
Party party = new PartyImpl();
|
||||
Hobbit hobbit = new Hobbit();
|
||||
Wizard wizard = new Wizard();
|
||||
Rogue rogue = new Rogue();
|
||||
Hunter hunter = new Hunter();
|
||||
|
||||
party.addMember(hobbit);
|
||||
party.addMember(wizard);
|
||||
party.addMember(rogue);
|
||||
party.addMember(hunter);
|
||||
party.addMember(hobbit);
|
||||
party.addMember(wizard);
|
||||
party.addMember(rogue);
|
||||
party.addMember(hunter);
|
||||
|
||||
hobbit.act(Action.ENEMY);
|
||||
wizard.act(Action.TALE);
|
||||
rogue.act(Action.GOLD);
|
||||
hunter.act(Action.HUNT);
|
||||
}
|
||||
hobbit.act(Action.ENEMY);
|
||||
wizard.act(Action.TALE);
|
||||
rogue.act(Action.GOLD);
|
||||
hunter.act(Action.HUNT);
|
||||
}
|
||||
}
|
||||
|
@ -13,7 +13,7 @@ public class PartyImpl implements Party {
|
||||
|
||||
@Override
|
||||
public void act(PartyMember actor, Action action) {
|
||||
for (PartyMember member: members) {
|
||||
for (PartyMember member : members) {
|
||||
if (member != actor) {
|
||||
member.partyAction(action);
|
||||
}
|
||||
|
@ -11,26 +11,26 @@ import java.util.Stack;
|
||||
*/
|
||||
public class App {
|
||||
|
||||
public static void main(String[] args) {
|
||||
Stack<StarMemento> states = new Stack<>();
|
||||
public static void main(String[] args) {
|
||||
Stack<StarMemento> states = new Stack<>();
|
||||
|
||||
Star star = new Star(StarType.SUN, 10000000, 500000);
|
||||
System.out.println(star);
|
||||
states.add(star.getMemento());
|
||||
star.timePasses();
|
||||
System.out.println(star);
|
||||
states.add(star.getMemento());
|
||||
star.timePasses();
|
||||
System.out.println(star);
|
||||
states.add(star.getMemento());
|
||||
star.timePasses();
|
||||
System.out.println(star);
|
||||
states.add(star.getMemento());
|
||||
star.timePasses();
|
||||
System.out.println(star);
|
||||
while (states.size() > 0) {
|
||||
star.setMemento(states.pop());
|
||||
System.out.println(star);
|
||||
}
|
||||
}
|
||||
Star star = new Star(StarType.SUN, 10000000, 500000);
|
||||
System.out.println(star);
|
||||
states.add(star.getMemento());
|
||||
star.timePasses();
|
||||
System.out.println(star);
|
||||
states.add(star.getMemento());
|
||||
star.timePasses();
|
||||
System.out.println(star);
|
||||
states.add(star.getMemento());
|
||||
star.timePasses();
|
||||
System.out.println(star);
|
||||
states.add(star.getMemento());
|
||||
star.timePasses();
|
||||
System.out.println(star);
|
||||
while (states.size() > 0) {
|
||||
star.setMemento(states.pop());
|
||||
System.out.println(star);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@ -63,6 +63,7 @@ public class Star {
|
||||
|
||||
@Override
|
||||
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() {
|
||||
return type;
|
||||
}
|
||||
|
||||
public void setType(StarType type) {
|
||||
this.type = type;
|
||||
}
|
||||
|
||||
public int getAgeYears() {
|
||||
return ageYears;
|
||||
}
|
||||
|
||||
public void setAgeYears(int ageYears) {
|
||||
this.ageYears = ageYears;
|
||||
}
|
||||
|
||||
public int getMassTons() {
|
||||
return massTons;
|
||||
}
|
||||
|
||||
public void setMassTons(int massTons) {
|
||||
this.massTons = massTons;
|
||||
}
|
||||
|
@ -2,11 +2,7 @@ package com.iluwatar;
|
||||
|
||||
public enum StarType {
|
||||
|
||||
SUN,
|
||||
RED_GIANT,
|
||||
WHITE_DWARF,
|
||||
SUPERNOVA,
|
||||
DEAD;
|
||||
SUN, RED_GIANT, WHITE_DWARF, SUPERNOVA, DEAD;
|
||||
|
||||
@Override
|
||||
public String toString() {
|
||||
|
@ -5,8 +5,8 @@ import java.io.File;
|
||||
import java.io.FileReader;
|
||||
|
||||
/**
|
||||
* Every instance of this class represents the Model component
|
||||
* in the Model-View-Presenter architectural pattern.
|
||||
* Every instance of this class represents the Model component in the
|
||||
* Model-View-Presenter architectural pattern.
|
||||
*
|
||||
* It is responsible for reading and loading the contents of a given file.
|
||||
*/
|
||||
@ -27,11 +27,12 @@ public class FileLoader {
|
||||
*/
|
||||
public String loadData() {
|
||||
try {
|
||||
BufferedReader br = new BufferedReader(new FileReader(new File(this.fileName)));
|
||||
BufferedReader br = new BufferedReader(new FileReader(new File(
|
||||
this.fileName)));
|
||||
String text = "";
|
||||
String line = "";
|
||||
|
||||
while( (line = br.readLine()) != null ) {
|
||||
while ((line = br.readLine()) != null) {
|
||||
text += line + "\n";
|
||||
}
|
||||
|
||||
@ -41,7 +42,7 @@ public class FileLoader {
|
||||
return text;
|
||||
}
|
||||
|
||||
catch(Exception e) {
|
||||
catch (Exception e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
|
||||
@ -51,7 +52,8 @@ public class FileLoader {
|
||||
/**
|
||||
* 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) {
|
||||
this.fileName = fileName;
|
||||
|
@ -14,10 +14,11 @@ import javax.swing.JTextArea;
|
||||
import javax.swing.JTextField;
|
||||
|
||||
/**
|
||||
* This class is the GUI implementation of the View component
|
||||
* In the Model-View-Presenter pattern.
|
||||
* This class is the GUI implementation of the View component In the
|
||||
* Model-View-Presenter pattern.
|
||||
*/
|
||||
public class FileSelectorJFrame extends JFrame implements FileSelectorView, ActionListener {
|
||||
public class FileSelectorJFrame extends JFrame implements FileSelectorView,
|
||||
ActionListener {
|
||||
|
||||
/**
|
||||
* Default serial version ID.
|
||||
@ -45,8 +46,7 @@ public class FileSelectorJFrame extends JFrame implements FileSelectorView, Acti
|
||||
private JLabel contents;
|
||||
|
||||
/**
|
||||
* The text field for giving the name of the file
|
||||
* that we want to open.
|
||||
* The text field for giving the name of the file that we want to open.
|
||||
*/
|
||||
private JTextField input;
|
||||
|
||||
@ -142,13 +142,13 @@ public class FileSelectorJFrame extends JFrame implements FileSelectorView, Acti
|
||||
|
||||
@Override
|
||||
public void actionPerformed(ActionEvent e) {
|
||||
if(e.getSource() == this.OK) {
|
||||
if (e.getSource() == this.OK) {
|
||||
this.fileName = this.input.getText();
|
||||
presenter.fileNameChanged();
|
||||
presenter.confirmed();
|
||||
}
|
||||
|
||||
else if(e.getSource() == this.cancel) {
|
||||
else if (e.getSource() == this.cancel) {
|
||||
presenter.cancelled();
|
||||
}
|
||||
}
|
||||
|
@ -1,10 +1,11 @@
|
||||
package com.iluwatar;
|
||||
|
||||
/**
|
||||
* Every instance of this class represents the Presenter component
|
||||
* in the Model-View-Presenter architectural pattern.
|
||||
* Every instance of this class represents the Presenter component in the
|
||||
* 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 {
|
||||
|
||||
@ -21,7 +22,8 @@ public class FileSelectorPresenter {
|
||||
/**
|
||||
* 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) {
|
||||
this.view = view;
|
||||
@ -30,7 +32,8 @@ public class FileSelectorPresenter {
|
||||
/**
|
||||
* 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) {
|
||||
this.loader = loader;
|
||||
@ -52,12 +55,12 @@ public class FileSelectorPresenter {
|
||||
}
|
||||
|
||||
public void confirmed() {
|
||||
if(loader.getFileName() == null || loader.getFileName().equals("")) {
|
||||
if (loader.getFileName() == null || loader.getFileName().equals("")) {
|
||||
view.showMessage("Please give the name of the file first!");
|
||||
return;
|
||||
}
|
||||
|
||||
if(loader.fileExists()) {
|
||||
if (loader.fileExists()) {
|
||||
String data = loader.loadData();
|
||||
view.displayData(data);
|
||||
}
|
||||
|
@ -1,14 +1,14 @@
|
||||
package com.iluwatar;
|
||||
|
||||
/**
|
||||
* Every instance of this class represents the Stub component in
|
||||
* the Model-View-Presenter architectural pattern.
|
||||
* Every instance of this class represents the Stub component in the
|
||||
* Model-View-Presenter architectural pattern.
|
||||
*
|
||||
* The stub implements the View interface and it is useful when
|
||||
* we want the test the reaction to user events, such as mouse clicks.
|
||||
* The stub implements the View interface and it is useful when we want the test
|
||||
* the reaction to user events, such as mouse clicks.
|
||||
*
|
||||
* Since we can not test the GUI directly, the MVP pattern provides
|
||||
* this functionality through the View's dummy implementation, the Stub.
|
||||
* Since we can not test the GUI directly, the MVP pattern provides this
|
||||
* functionality through the View's dummy implementation, the Stub.
|
||||
*/
|
||||
public class FileSelectorStub implements FileSelectorView {
|
||||
|
||||
|
@ -1,9 +1,8 @@
|
||||
package com.iluwatar;
|
||||
|
||||
/**
|
||||
* This interface represents the View component in the
|
||||
* Model-View-Presenter pattern. It can be implemented
|
||||
* by either the GUI components, or by the Stub.
|
||||
* This interface represents the View component in the Model-View-Presenter
|
||||
* pattern. It can be implemented by either the GUI components, or by the Stub.
|
||||
*/
|
||||
public interface FileSelectorView {
|
||||
|
||||
@ -25,7 +24,8 @@ public interface FileSelectorView {
|
||||
/**
|
||||
* 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);
|
||||
|
||||
@ -37,7 +37,8 @@ public interface FileSelectorView {
|
||||
/**
|
||||
* 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);
|
||||
|
||||
@ -49,14 +50,16 @@ public interface FileSelectorView {
|
||||
/**
|
||||
* 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);
|
||||
|
||||
/**
|
||||
* 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);
|
||||
}
|
||||
|
@ -6,8 +6,8 @@ import org.junit.Before;
|
||||
import org.junit.Test;
|
||||
|
||||
/**
|
||||
* This test case is responsible for testing our application
|
||||
* by taking advantage of the Model-View-Controller architectural pattern.
|
||||
* This test case is responsible for testing our application by taking advantage
|
||||
* of the Model-View-Controller architectural pattern.
|
||||
*/
|
||||
public class FileSelectorPresenterTest {
|
||||
|
||||
@ -26,7 +26,6 @@ public class FileSelectorPresenterTest {
|
||||
*/
|
||||
private FileLoader loader;
|
||||
|
||||
|
||||
/**
|
||||
* 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
|
||||
* that it's name is null or an empty string.
|
||||
* Tests if we receive a confirmation when we attempt to open a file that
|
||||
* it's name is null or an empty string.
|
||||
*/
|
||||
@Test
|
||||
public void fileConfirmationWhenNameIsNull() {
|
||||
@ -80,8 +79,8 @@ public class FileSelectorPresenterTest {
|
||||
}
|
||||
|
||||
/**
|
||||
* Tests if we receive a confirmation when we attempt to open a file
|
||||
* that it doesn't exist.
|
||||
* Tests if we receive a confirmation when we attempt to open a file that it
|
||||
* doesn't exist.
|
||||
*/
|
||||
@Test
|
||||
public void fileConfirmationWhenFileDoesNotExist() {
|
||||
|
@ -8,16 +8,16 @@ package com.iluwatar;
|
||||
*/
|
||||
public class App {
|
||||
|
||||
public static void main(String[] args) {
|
||||
public static void main(String[] args) {
|
||||
|
||||
Weather weather = new Weather();
|
||||
weather.addObserver(new Orcs());
|
||||
weather.addObserver(new Hobbits());
|
||||
Weather weather = new Weather();
|
||||
weather.addObserver(new Orcs());
|
||||
weather.addObserver(new Hobbits());
|
||||
|
||||
weather.timePasses();
|
||||
weather.timePasses();
|
||||
weather.timePasses();
|
||||
weather.timePasses();
|
||||
weather.timePasses();
|
||||
weather.timePasses();
|
||||
weather.timePasses();
|
||||
weather.timePasses();
|
||||
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@ -6,7 +6,8 @@ public class Hobbits implements WeatherObserver {
|
||||
public void update(WeatherType currentWeather) {
|
||||
switch (currentWeather) {
|
||||
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;
|
||||
case RAINY:
|
||||
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.");
|
||||
break;
|
||||
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;
|
||||
default:
|
||||
break;
|
||||
|
@ -5,8 +5,8 @@ import java.util.List;
|
||||
|
||||
/**
|
||||
*
|
||||
* Weather can be observed by implementing WeatherObserver
|
||||
* interface and registering as listener.
|
||||
* Weather can be observed by implementing WeatherObserver interface and
|
||||
* registering as listener.
|
||||
*
|
||||
*/
|
||||
public class Weather {
|
||||
@ -49,7 +49,7 @@ public class Weather {
|
||||
}
|
||||
|
||||
private void notifyObservers() {
|
||||
for (WeatherObserver obs: observers) {
|
||||
for (WeatherObserver obs : observers) {
|
||||
obs.update(currentWeather);
|
||||
}
|
||||
}
|
||||
|
@ -2,10 +2,7 @@ package com.iluwatar;
|
||||
|
||||
public enum WeatherType {
|
||||
|
||||
SUNNY,
|
||||
RAINY,
|
||||
WINDY,
|
||||
COLD;
|
||||
SUNNY, RAINY, WINDY, COLD;
|
||||
|
||||
public String toString() {
|
||||
return this.name().toLowerCase();
|
||||
|
@ -9,26 +9,28 @@ package com.iluwatar;
|
||||
*/
|
||||
public class App {
|
||||
|
||||
public static void main(String[] args) {
|
||||
HeroFactory factory;
|
||||
Mage mage;
|
||||
Warlord warlord;
|
||||
Beast beast;
|
||||
public static void main(String[] args) {
|
||||
HeroFactory factory;
|
||||
Mage mage;
|
||||
Warlord warlord;
|
||||
Beast beast;
|
||||
|
||||
factory = new HeroFactoryImpl(new ElfMage(), new ElfWarlord(), new ElfBeast());
|
||||
mage = factory.createMage();
|
||||
warlord = factory.createWarlord();
|
||||
beast = factory.createBeast();
|
||||
System.out.println(mage);
|
||||
System.out.println(warlord);
|
||||
System.out.println(beast);
|
||||
factory = new HeroFactoryImpl(new ElfMage(), new ElfWarlord(),
|
||||
new ElfBeast());
|
||||
mage = factory.createMage();
|
||||
warlord = factory.createWarlord();
|
||||
beast = factory.createBeast();
|
||||
System.out.println(mage);
|
||||
System.out.println(warlord);
|
||||
System.out.println(beast);
|
||||
|
||||
factory = new HeroFactoryImpl(new OrcMage(), new OrcWarlord(), new OrcBeast());
|
||||
mage = factory.createMage();
|
||||
warlord = factory.createWarlord();
|
||||
beast = factory.createBeast();
|
||||
System.out.println(mage);
|
||||
System.out.println(warlord);
|
||||
System.out.println(beast);
|
||||
}
|
||||
factory = new HeroFactoryImpl(new OrcMage(), new OrcWarlord(),
|
||||
new OrcBeast());
|
||||
mage = factory.createMage();
|
||||
warlord = factory.createWarlord();
|
||||
beast = factory.createBeast();
|
||||
System.out.println(mage);
|
||||
System.out.println(warlord);
|
||||
System.out.println(beast);
|
||||
}
|
||||
}
|
||||
|
@ -7,14 +7,14 @@ package com.iluwatar;
|
||||
*/
|
||||
public class App {
|
||||
|
||||
public static void main(String[] args) {
|
||||
public static void main(String[] args) {
|
||||
|
||||
WizardTowerProxy tower = new WizardTowerProxy();
|
||||
tower.enter(new Wizard("Red wizard"));
|
||||
tower.enter(new Wizard("White wizard"));
|
||||
tower.enter(new Wizard("Black wizard"));
|
||||
tower.enter(new Wizard("Green wizard"));
|
||||
tower.enter(new Wizard("Brown wizard"));
|
||||
WizardTowerProxy tower = new WizardTowerProxy();
|
||||
tower.enter(new Wizard("Red wizard"));
|
||||
tower.enter(new Wizard("White wizard"));
|
||||
tower.enter(new Wizard("Black wizard"));
|
||||
tower.enter(new Wizard("Green wizard"));
|
||||
tower.enter(new Wizard("Brown wizard"));
|
||||
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@ -8,12 +8,12 @@ package com.iluwatar;
|
||||
*/
|
||||
public class App {
|
||||
|
||||
public static void main(String[] args) {
|
||||
public static void main(String[] args) {
|
||||
|
||||
IvoryTower ivoryTower1 = IvoryTower.getInstance();
|
||||
IvoryTower ivoryTower2 = IvoryTower.getInstance();
|
||||
System.out.println("ivoryTower1=" + ivoryTower1);
|
||||
System.out.println("ivoryTower2=" + ivoryTower2);
|
||||
IvoryTower ivoryTower1 = IvoryTower.getInstance();
|
||||
IvoryTower ivoryTower2 = IvoryTower.getInstance();
|
||||
System.out.println("ivoryTower1=" + ivoryTower1);
|
||||
System.out.println("ivoryTower2=" + ivoryTower2);
|
||||
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@ -9,7 +9,8 @@ public class IvoryTower {
|
||||
|
||||
private static IvoryTower instance = new IvoryTower();
|
||||
|
||||
private IvoryTower() {}
|
||||
private IvoryTower() {
|
||||
}
|
||||
|
||||
public static IvoryTower getInstance() {
|
||||
return instance;
|
||||
|
@ -9,14 +9,14 @@ package com.iluwatar;
|
||||
*/
|
||||
public class App {
|
||||
|
||||
public static void main(String[] args) {
|
||||
public static void main(String[] args) {
|
||||
|
||||
Mammoth mammoth = new Mammoth();
|
||||
mammoth.observe();
|
||||
mammoth.timePasses();
|
||||
mammoth.observe();
|
||||
mammoth.timePasses();
|
||||
mammoth.observe();
|
||||
Mammoth mammoth = new Mammoth();
|
||||
mammoth.observe();
|
||||
mammoth.timePasses();
|
||||
mammoth.observe();
|
||||
mammoth.timePasses();
|
||||
mammoth.observe();
|
||||
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@ -8,15 +8,15 @@ package com.iluwatar;
|
||||
*/
|
||||
public class App {
|
||||
|
||||
public static void main(String[] args) {
|
||||
System.out.println("Green dragon spotted ahead!");
|
||||
DragonSlayer dragonSlayer = new DragonSlayer(new MeleeStrategy());
|
||||
dragonSlayer.goToBattle();
|
||||
System.out.println("Red dragon emerges.");
|
||||
dragonSlayer.changeStrategy(new ProjectileStrategy());
|
||||
dragonSlayer.goToBattle();
|
||||
System.out.println("Black dragon lands before you.");
|
||||
dragonSlayer.changeStrategy(new SpellStrategy());
|
||||
dragonSlayer.goToBattle();
|
||||
}
|
||||
public static void main(String[] args) {
|
||||
System.out.println("Green dragon spotted ahead!");
|
||||
DragonSlayer dragonSlayer = new DragonSlayer(new MeleeStrategy());
|
||||
dragonSlayer.goToBattle();
|
||||
System.out.println("Red dragon emerges.");
|
||||
dragonSlayer.changeStrategy(new ProjectileStrategy());
|
||||
dragonSlayer.goToBattle();
|
||||
System.out.println("Black dragon lands before you.");
|
||||
dragonSlayer.changeStrategy(new SpellStrategy());
|
||||
dragonSlayer.goToBattle();
|
||||
}
|
||||
}
|
||||
|
@ -4,7 +4,8 @@ public class ProjectileStrategy implements DragonSlayingStrategy {
|
||||
|
||||
@Override
|
||||
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
|
||||
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!");
|
||||
}
|
||||
|
||||
}
|
||||
|
@ -8,10 +8,10 @@ package com.iluwatar;
|
||||
*/
|
||||
public class App {
|
||||
|
||||
public static void main(String[] args) {
|
||||
HalflingThief thief = new HalflingThief(new HitAndRunMethod());
|
||||
thief.steal();
|
||||
thief.changeMethod(new SubtleMethod());
|
||||
thief.steal();
|
||||
}
|
||||
public static void main(String[] args) {
|
||||
HalflingThief thief = new HalflingThief(new HitAndRunMethod());
|
||||
thief.steal();
|
||||
thief.changeMethod(new SubtleMethod());
|
||||
thief.steal();
|
||||
}
|
||||
}
|
||||
|
@ -9,12 +9,14 @@ public class SubtleMethod extends StealingMethod {
|
||||
|
||||
@Override
|
||||
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
|
||||
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.");
|
||||
}
|
||||
|
||||
}
|
||||
|
@ -9,14 +9,14 @@ package com.iluwatar;
|
||||
*/
|
||||
public class App {
|
||||
|
||||
public static void main(String[] args) {
|
||||
public static void main(String[] args) {
|
||||
|
||||
Commander commander = new Commander(
|
||||
new Sergeant(new Soldier(), new Soldier(), new Soldier()),
|
||||
new Sergeant(new Soldier(), new Soldier(), new Soldier()));
|
||||
commander.accept(new SoldierVisitor());
|
||||
commander.accept(new SergeantVisitor());
|
||||
commander.accept(new CommanderVisitor());
|
||||
Commander commander = new Commander(new Sergeant(new Soldier(),
|
||||
new Soldier(), new Soldier()), new Sergeant(new Soldier(),
|
||||
new Soldier(), new Soldier()));
|
||||
commander.accept(new SoldierVisitor());
|
||||
commander.accept(new SergeantVisitor());
|
||||
commander.accept(new CommanderVisitor());
|
||||
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@ -2,7 +2,7 @@ package com.iluwatar;
|
||||
|
||||
public class Commander extends Unit {
|
||||
|
||||
public Commander(Unit ... children) {
|
||||
public Commander(Unit... children) {
|
||||
super(children);
|
||||
}
|
||||
|
||||
|
@ -2,7 +2,7 @@ package com.iluwatar;
|
||||
|
||||
public class Sergeant extends Unit {
|
||||
|
||||
public Sergeant(Unit ... children) {
|
||||
public Sergeant(Unit... children) {
|
||||
super(children);
|
||||
}
|
||||
|
||||
|
@ -2,7 +2,7 @@ package com.iluwatar;
|
||||
|
||||
public class Soldier extends Unit {
|
||||
|
||||
public Soldier(Unit ... children) {
|
||||
public Soldier(Unit... children) {
|
||||
super(children);
|
||||
}
|
||||
|
||||
|
@ -9,12 +9,12 @@ public abstract class Unit {
|
||||
|
||||
private Unit[] children;
|
||||
|
||||
public Unit(Unit ... children) {
|
||||
public Unit(Unit... children) {
|
||||
this.children = children;
|
||||
}
|
||||
|
||||
public void accept(UnitVisitor visitor) {
|
||||
for (Unit child: children) {
|
||||
for (Unit child : children) {
|
||||
child.accept(visitor);
|
||||
}
|
||||
}
|
||||
|
@ -8,7 +8,9 @@ package com.iluwatar;
|
||||
public interface UnitVisitor {
|
||||
|
||||
void visitSoldier(Soldier soldier);
|
||||
|
||||
void visitSergeant(Sergeant sergeant);
|
||||
|
||||
void visitCommander(Commander commander);
|
||||
|
||||
}
|
||||
|
Loading…
x
Reference in New Issue
Block a user