Skip to content

Commit

Permalink
Implement inheritance from new class Entity
Browse files Browse the repository at this point in the history
  • Loading branch information
Marcusjenks committed Jan 13, 2018
1 parent e6310ea commit 7214463
Show file tree
Hide file tree
Showing 4 changed files with 83 additions and 107 deletions.
2 changes: 1 addition & 1 deletion src/battle/Battle.java
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ public static void main(String[] args) {
boolean running = true;
while (running) {
enemy = new Enemy();
//enemy.getInfo();
enemy.getInfo();

enemy.takeDamage(player.getStrength());
enemy.stillAlive();
Expand Down
98 changes: 33 additions & 65 deletions src/battle/Enemy.java
Original file line number Diff line number Diff line change
Expand Up @@ -11,76 +11,44 @@
import org.w3c.dom.Node;
import org.w3c.dom.NodeList;

public class Enemy {

public Enemy() {

try {

File fXmlFile = new File("enemy.xml");
DocumentBuilderFactory dbFactory = DocumentBuilderFactory.newInstance();
DocumentBuilder dBuilder = dbFactory.newDocumentBuilder();
Document doc = dBuilder.parse(fXmlFile);

NodeList nList = doc.getElementsByTagName("Enemy");

Random random = new Random();
public class Enemy extends Entity{

Node nNode = nList.item(random.nextInt(nList.getLength()));

if (nNode.getNodeType() == Node.ELEMENT_NODE) {

Element eElement = (Element) nNode;

name = eElement.getElementsByTagName("Name").item(0).getTextContent();
health = Integer.parseInt(eElement.getElementsByTagName("Health").item(0).getTextContent());
strength = Integer.parseInt(eElement.getElementsByTagName("Strength").item(0).getTextContent());
exp = Integer.parseInt(eElement.getElementsByTagName("Exp").item(0).getTextContent());
public Enemy() {
//super(name, health, strength, exp);

}
} catch (Exception e) {
e.printStackTrace();
try {

File fXmlFile = new File("enemy.xml");
DocumentBuilderFactory dbFactory = DocumentBuilderFactory.newInstance();
DocumentBuilder dBuilder = dbFactory.newDocumentBuilder();
Document doc = dBuilder.parse(fXmlFile);

NodeList nList = doc.getElementsByTagName("Enemy");

Random random = new Random();

Node nNode = nList.item(random.nextInt(nList.getLength()));

if (nNode.getNodeType() == Node.ELEMENT_NODE) {

Element eElement = (Element) nNode;

name = eElement.getElementsByTagName("Name").item(0).getTextContent();
health = Integer.parseInt(eElement.getElementsByTagName("Health").item(0).getTextContent());
strength = Integer.parseInt(eElement.getElementsByTagName("Strength").item(0).getTextContent());
exp = Integer.parseInt(eElement.getElementsByTagName("Exp").item(0).getTextContent());

}
} catch (Exception e) {
e.printStackTrace();
}
}
public void stillAlive() {
if (this.getHealth() <=0){
System.out.println("You defeated " + this.getName() + "!");
}
}

private String name;
private int health;
private int strength;
private int exp;


public String getName() {
return name;
}

public int getHealth() {
return health;
}

public int getStrength() {
return strength;
}

public int getExp() {
return exp;
}

public void getInfo() {
System.out.println("Enemy: " + getName() + " | Health: " + getHealth() + " | Strength: " + getStrength());

}

public void takeDamage(int strength2) {
this.health = getHealth() - strength2;


}

public void stillAlive() {
if (this.health <=0){
System.out.println("You have defeated: " + getName());
}

}
}

43 changes: 43 additions & 0 deletions src/battle/Entity.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
package battle;

public class Entity {
public Entity(){

}

protected String name;
protected int health;
protected int strength;
protected int exp;

public String getName() {
return name;
}

public int getHealth() {
return health;
}

public int getStrength() {
return strength;
}

public int getExp() {
return exp;
}

public void getInfo() {
System.out.println("Name: " + getName() + " | Health: " + getHealth() + " | Strength: " + getStrength());

}

public void takeDamage(int strength2) {
this.health = getHealth() - strength2;

}

public void stillAlive() {
}

}

47 changes: 6 additions & 41 deletions src/battle/Player.java
Original file line number Diff line number Diff line change
@@ -1,52 +1,17 @@
package battle;

public class Player{
public Player(String name, int health, int strength, int exp){
public class Player extends Entity{

public Player(String name, int health, int strength, int exp) {
this.name = name;
this.health = health;
this.strength = strength;
this.exp = exp;

}

private String name;
private int health;
private int strength;
private int exp;

public String getName() {
return name;
}

public int getHealth() {
return health;
}

public int getStrength() {
return strength;
}

public int getExp() {
return exp;
}

public void getInfo() {
System.out.println("Name: " + getName() + " | Health: " + getHealth() + " | Strength: " + getStrength());

}

public void takeDamage(int strength2) {
this.health = getHealth() - strength2;

}

}
public void stillAlive() {
if (this.health <=0){
System.out.println("You died! How Lame");
if (this.getHealth() <=0){
System.out.println("You died! How Lame");
}

}


}

0 comments on commit 7214463

Please sign in to comment.