In: Computer Science
Implement a Factory Design Pattern for the code below:
MAIN:
import java.util.ArrayList;
import java.util.List;
import java.util.Random;
public class Main {
public static void main(String[] args) {
Character char1 = new Orc("Grumlin");
Character char2 = new Elf("Therae");
int damageDealt = char1.attackEnemy();
System.out.println(char1.name + " has attacked an enemy " +
"and dealt " + damageDealt + " damage");
char1.hasCastSpellSkill = true;
damageDealt = char1.attackEnemy();
System.out.println(char1.name + " has attacked an enemy " +
"and dealt " + damageDealt + " damage");
int damageTaken = char2.takeHit();
System.out.println(char2.name + " has taken a hit and " +
"been dealt " + damageTaken + " damage");
char2.hasDodgeAttackSkill = true;
damageTaken = char2.takeHit();
System.out.println(char2.name + " has taken a hit and " +
"been dealt " + damageTaken + " damage");
}
}
CHARACTER:
import java.util.Random;
public abstract class Character {
protected String name;
protected int strength;
protected int resilience;
protected boolean hasCastSpellSkill;
protected boolean hasDodgeAttackSkill;
public int attackEnemy() {
Random random = new Random();
int damageDealt;
if (hasCastSpellSkill) {
int spellDamage = random.nextInt(5);
damageDealt = this.strength + spellDamage;
} else {
damageDealt = strength;
}
return damageDealt;
}
public int takeHit() {
Random random = new Random();
int damageDealt = random.nextInt(15);
int damageTaken;
if (hasDodgeAttackSkill) {
double chanceToDodge = random.nextDouble();
if (chanceToDodge > 0.50) {
damageTaken = 0;
} else {
damageTaken = damageDealt - resilience;
}
} else {
damageTaken = damageDealt - resilience;
}
if (damageTaken < 0) {
damageTaken = 0;
}
return damageTaken;
}
}
ELF:
public class Elf extends Character {
public Elf(String name) {
this.name = name;
this.strength = 4;
this.resilience = 2;
}
}
ORC:
public class Orc extends Character {
public Orc(String name) {
this.name = name;
this.strength = 10;
this.resilience = 9;
}
}
import java.util.ArrayList;
import java.util.List;
import java.util.Random;
class CharacterFactory {
public static Character createOrc() {
return new
Orc("Grumlin");
}
public static Character createElf() {
return new
Elf("Therae");
}
}
public class Main {
public static void main(String[] args)
{
Character
char1 = CharacterFactory.createOrc();
Character char2 =
CharacterFactory.createElf();
int damageDealt =
char1.attackEnemy();
System.out.println(char1.name
+ " has attacked an enemy " +
"and dealt " +
damageDealt + " damage");
char1.hasCastSpellSkill
= true;
damageDealt =
char1.attackEnemy();
System.out.println(char1.name
+ " has attacked an enemy " +
"and dealt " +
damageDealt + " damage");
int damageTaken =
char2.takeHit();
System.out.println(char2.name
+ " has taken a hit and " +
"been dealt " +
damageTaken + " damage");
char2.hasDodgeAttackSkill
= true;
damageTaken =
char2.takeHit();
System.out.println(char2.name
+ " has taken a hit and " +
"been dealt " +
damageTaken + " damage");
}
}
**************************************************
I have tried to answer your question to best of my efforts.
However, if you still face any issues in the answer, please let me
know via the comment section. Your feedback will imporve as well as
encourage me to keep up the good work.
If i was able to help you, then please provide me an
upvote(thumbs up). Thanks!