Skip to content

Commit

Permalink
iluwatar#88 Made main code comply with CheckStyle.
Browse files Browse the repository at this point in the history
  • Loading branch information
Jeremy-Cains-ANU committed Oct 27, 2022
1 parent 04ed1f2 commit f6f0eef
Show file tree
Hide file tree
Showing 6 changed files with 77 additions and 22 deletions.
32 changes: 23 additions & 9 deletions facet/README.md
Expand Up @@ -44,7 +44,7 @@ public class Knight {
private Attack attack;
private DragonFacet dragonFacet;

public Knight (String name, Attack attack, DragonFacet dragonFacet) {
public Knight(String name, Attack attack, DragonFacet dragonFacet) {
this.name = name;
this.attack = attack;
this.dragonFacet = dragonFacet;
Expand All @@ -53,7 +53,7 @@ public class Knight {
public void attackDragon() {
int oldHealth = dragonFacet.getHealth();
dragonFacet.receiveAttack(attack);
if(oldHealth == dragonFacet.getHealth()){
if (oldHealth == dragonFacet.getHealth()) {
LOGGER.info("{}: Darn it! {} did nothing.", name, attack);
} else {
LOGGER.info("{}: Huzzah! {} hurt that dastardly dragon.", name, attack);
Expand Down Expand Up @@ -87,16 +87,16 @@ public class Dragon {
this.health = health;
}

int f_getHealth() {
int facetedGetHealth() {
return health;
}

void setHealth(int health) {
this.health = health;
}

void f_receiveAttack(Attack attack) {
switch(attack) {
void facetedReceiveAttack(Attack attack) {
switch (attack) {
case ARROW:
health -= 10;
break;
Expand All @@ -114,6 +114,19 @@ Then we have the `DragonFacet` to add control to `Dragon`.

```java
@Slf4j
package com.iluwatar.facet.dragon;

import com.iluwatar.facet.Attack;
import lombok.extern.slf4j.Slf4j;

/**
* The facet class which acts as an interface/wrapper for {@link Dragon}.
* It only allows access to the methods with names beginning
* with 'faceted', and also checks in receiveAttack() what
* the type of attack is, so that it can filter some illegal
* values.
*/
@Slf4j
public class DragonFacet {
private Dragon dragon;

Expand All @@ -122,15 +135,16 @@ public class DragonFacet {
}

public int getHealth() {
return dragon.f_getHealth();
return dragon.facetedGetHealth();
}

public void receiveAttack(Attack attack) {
if(attack == Attack.WATER_PISTOL || attack == Attack.ARROW) {
dragon.f_receiveAttack(attack);
if (attack == Attack.WATER_PISTOL || attack == Attack.ARROW) {
dragon.facetedReceiveAttack(attack);
}
}
}

```

Note that `DragonFacet` only provides access to two of the three methods in `Dragon`
Expand Down
4 changes: 3 additions & 1 deletion facet/src/main/java/com/iluwatar/facet/App.java
Expand Up @@ -24,7 +24,9 @@
*/
package com.iluwatar.facet;

import com.iluwatar.facet.dragon.*;
import com.iluwatar.facet.dragon.Dragon;
import com.iluwatar.facet.dragon.DragonFacet;


/**
* A facet is a class functioning as an interface to something else which provides less functionality.
Expand Down
4 changes: 4 additions & 0 deletions facet/src/main/java/com/iluwatar/facet/Attack.java
@@ -1,5 +1,9 @@
package com.iluwatar.facet;

/**
* Four attacks that the knights can have.
* These have various levels of usefulness against a dragon.
*/
public enum Attack {
ARROW, WATER_PISTOL, SWORD, FLAME_THROWER
}
19 changes: 17 additions & 2 deletions facet/src/main/java/com/iluwatar/facet/Knight.java
Expand Up @@ -3,22 +3,37 @@
import com.iluwatar.facet.dragon.DragonFacet;
import lombok.extern.slf4j.Slf4j;

/**
* Simple knight class. It is given a reference only to a DragonFacet,
* not a dragon itself. This restricts its access to its functions and
* disallows some incorrect parameters in the receiveAttack() function.
*/
@Slf4j
public class Knight {
private final String name;
private Attack attack;
private DragonFacet dragonFacet;

public Knight (String name, Attack attack, DragonFacet dragonFacet) {
/**
* Simple constructor for a Knight.
*
* @param name The name of the knight
* @param attack His type of attack
* @param dragonFacet The reference to the dragon wrapped by a facet
*/
public Knight(String name, Attack attack, DragonFacet dragonFacet) {
this.name = name;
this.attack = attack;
this.dragonFacet = dragonFacet;
}

/**
* Try to attack the dragon through the facet.
*/
public void attackDragon() {
int oldHealth = dragonFacet.getHealth();
dragonFacet.receiveAttack(attack);
if(oldHealth == dragonFacet.getHealth()){
if (oldHealth == dragonFacet.getHealth()) {
LOGGER.info("{}: Darn it! {} did nothing.", name, attack);
} else {
LOGGER.info("{}: Huzzah! {} hurt that dastardly dragon.", name, attack);
Expand Down
19 changes: 13 additions & 6 deletions facet/src/main/java/com/iluwatar/facet/dragon/Dragon.java
Expand Up @@ -4,9 +4,9 @@

/**
* Dragon object needs to be protected, since the other objects shouldn't be
* allowed to edit its health.
*
*
* allowed to edit its health. That is why it is in its own package with
* {@link DragonFacet} and all the functions have no access level modifiers,
* meaning only classes in the same package have access.
*/
public class Dragon {
private int health;
Expand All @@ -15,16 +15,23 @@ public Dragon(int health) {
this.health = health;
}

int f_getHealth() {
int facetedGetHealth() {
return health;
}

/**
* This has no access level modifier, so only other classes within
* the same package can access this function. This protects the knight
* from being able to arbitrarily change the dragon's health.
*
* @param health The new health of the dragon
*/
void setHealth(int health) {
this.health = health;
}

void f_receiveAttack(Attack attack) {
switch(attack) {
void facetedReceiveAttack(Attack attack) {
switch (attack) {
case ARROW:
health -= 10;
break;
Expand Down
21 changes: 17 additions & 4 deletions facet/src/main/java/com/iluwatar/facet/dragon/DragonFacet.java
@@ -1,9 +1,15 @@
package com.iluwatar.facet.dragon;

import com.iluwatar.facet.Attack;
import com.iluwatar.facet.dragon.Dragon;
import lombok.extern.slf4j.Slf4j;

/**
* The facet class which acts as an interface/wrapper for {@link Dragon}.
* It only allows access to the methods with names beginning
* with 'faceted', and also checks in receiveAttack() what
* the type of attack is, so that it can filter some illegal
* values.
*/
@Slf4j
public class DragonFacet {
private Dragon dragon;
Expand All @@ -13,12 +19,19 @@ public DragonFacet(Dragon dragon) {
}

public int getHealth() {
return dragon.f_getHealth();
return dragon.facetedGetHealth();
}

/**
* This performs a check of the attack, and for certain illegal values
* (namely FLAME_THROWER and SWORD), the attack will not even alert
* the dragon.
*
* @param attack The attack which is attempted against dragon
*/
public void receiveAttack(Attack attack) {
if(attack == Attack.WATER_PISTOL || attack == Attack.ARROW) {
dragon.f_receiveAttack(attack);
if (attack == Attack.WATER_PISTOL || attack == Attack.ARROW) {
dragon.facetedReceiveAttack(attack);
}
}
}

0 comments on commit f6f0eef

Please sign in to comment.