-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Using a decorator patter, add decorations to classes
- Loading branch information
1 parent
aae56d5
commit 9fcd6bb
Showing
25 changed files
with
372 additions
and
0 deletions.
There are no files selected for viewing
Binary file not shown.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
<?xml version="1.0" encoding="UTF-8"?> | ||
<module type="JAVA_MODULE" version="4"> | ||
<component name="NewModuleRootManager" inherit-compiler-output="true"> | ||
<exclude-output /> | ||
<content url="file://$MODULE_DIR$"> | ||
<sourceFolder url="file://$MODULE_DIR$/src" isTestSource="false" /> | ||
</content> | ||
<orderEntry type="inheritedJdk" /> | ||
<orderEntry type="sourceFolder" forTests="false" /> | ||
</component> | ||
</module> |
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
public class Alien extends Monster { | ||
/** | ||
* Basic base monster to be decorated with a name and hp. | ||
* @param n name of monster. | ||
* @param h hp of monster. | ||
*/ | ||
public Alien(String n, int h) { | ||
super(n, h); | ||
} | ||
|
||
/** | ||
* Basic attack points of the alien monster. | ||
* @return attack points of alien. | ||
*/ | ||
@Override | ||
int attack() { | ||
return 3; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
public class Beast extends Monster{ | ||
/** | ||
* Basic base monster to be decorated with a name and hp. | ||
* @param n name of monster. | ||
* @param h hp of monster. | ||
*/ | ||
public Beast(String n, int h) { | ||
super(n, h); | ||
} | ||
/** | ||
* Basic attack points of the beast monster. | ||
* @return attack points of beast monster. | ||
*/ | ||
@Override | ||
int attack() { | ||
return 3; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,133 @@ | ||
import java.util.Scanner; | ||
|
||
/** | ||
* Static functions used to check console input for validity. | ||
* | ||
* Use: Place CheckInput class in the same project folder as your code. | ||
* Call CheckInput functions from your code using "CheckInput." | ||
* | ||
* Example: int num = CheckInput.getInt(); | ||
* | ||
* @author Shannon Cleary 2020 | ||
*/ | ||
public class CheckInput { | ||
|
||
/** | ||
* Checks if the inputted value is an integer. | ||
* @return the valid input. | ||
*/ | ||
public static int getInt() { | ||
Scanner in = new Scanner( System.in ); | ||
int input = 0; | ||
boolean valid = false; | ||
while( !valid ) { | ||
if( in.hasNextInt() ) { | ||
input = in.nextInt(); | ||
valid = true; | ||
} else { | ||
in.next(); //clear invalid string | ||
System.out.println( "Invalid Input." ); | ||
} | ||
} | ||
return input; | ||
} | ||
|
||
/** | ||
* Checks if the inputted value is an integer and | ||
* within the specified range (ex: 1-10) | ||
* @param low lower bound of the range. | ||
* @param high upper bound of the range. | ||
* @return the valid input. | ||
*/ | ||
public static int getIntRange( int low, int high ) { | ||
Scanner in = new Scanner( System.in ); | ||
int input = 0; | ||
boolean valid = false; | ||
while( !valid ) { | ||
if( in.hasNextInt() ) { | ||
input = in.nextInt(); | ||
if( input <= high && input >= low ) { | ||
valid = true; | ||
} else { | ||
System.out.println( "Invalid Range." ); | ||
} | ||
} else { | ||
in.next(); //clear invalid string | ||
System.out.println( "Invalid Input." ); | ||
} | ||
} | ||
return input; | ||
} | ||
/** | ||
* Checks if the inputted value is a non-negative integer. | ||
* @return the valid input. | ||
*/ | ||
public static int getPositiveInt( ) { | ||
Scanner in = new Scanner( System.in ); | ||
int input = 0; | ||
boolean valid = false; | ||
while( !valid ) { | ||
if( in.hasNextInt() ) { | ||
input = in.nextInt(); | ||
if( input >= 0 ) { | ||
valid = true; | ||
} else { | ||
System.out.println( "Invalid Range." ); | ||
} | ||
} else { | ||
in.next(); //clear invalid string | ||
System.out.println( "Invalid Input." ); | ||
} | ||
} | ||
return input; | ||
} | ||
|
||
/** | ||
* Checks if the inputted value is a double. | ||
* @return the valid input. | ||
*/ | ||
public static double getDouble() { | ||
Scanner in = new Scanner( System.in ); | ||
double input = 0; | ||
boolean valid = false; | ||
while( !valid ) { | ||
if( in.hasNextDouble() ) { | ||
input = in.nextDouble(); | ||
valid = true; | ||
} else { | ||
in.next(); //clear invalid string | ||
System.out.println( "Invalid Input." ); | ||
} | ||
} | ||
return input; | ||
} | ||
|
||
/** | ||
* Takes in a string from the user. | ||
* @return the inputted String. | ||
*/ | ||
public static String getString() { | ||
Scanner in = new Scanner( System.in ); | ||
String input = in.nextLine(); | ||
return input; | ||
} | ||
|
||
/** | ||
* Takes in a yes/no from the user. | ||
* @return true if yes, false if no. | ||
*/ | ||
public static boolean getYesNo(){ | ||
boolean valid = false; | ||
while( !valid ) { | ||
String s = getString(); | ||
if( s.equalsIgnoreCase("yes") || s.equalsIgnoreCase("y") ) { | ||
return true; | ||
} else if( s.equalsIgnoreCase("no") || s.equalsIgnoreCase("n") ) { | ||
return false; | ||
} else { | ||
System.out.println( "Invalid Input." ); | ||
} | ||
} | ||
return false; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
public class Fire extends MonsterDecorator { | ||
/** | ||
* Creates a decorator to give a fire name and health increase to our monster. | ||
* @param m the monster to be decorated. | ||
*/ | ||
public Fire(Monster m){ | ||
super(m, " Hotboi", 2); | ||
} | ||
|
||
/** | ||
* Buffs our monsters attack points. | ||
* @return the prior attack points + the buff. | ||
*/ | ||
public int attack(){ | ||
return 3 + super.attack(); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
public class Flying extends MonsterDecorator{ | ||
/** | ||
* Creates a decorator to give a flying name and health increase to our monster. | ||
* @param m the monster to be decorated. | ||
*/ | ||
public Flying(Monster m) { | ||
super(m, " Flying Raijin", 1); | ||
} | ||
/** | ||
* Buffs our monsters attack points. | ||
* @return the prior attack points + the buff. | ||
*/ | ||
public int attack(){ | ||
return 1 + super.attack(); | ||
} | ||
} |
Binary file not shown.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
public class Lasers extends MonsterDecorator { | ||
/** | ||
* Creates a decorator to give a laser name and health increase to our monster. | ||
* @param m the monster to be decorated. | ||
*/ | ||
public Lasers(Monster m) { | ||
super(m, " with Laser Beams", 2); | ||
} | ||
/** | ||
* Buffs our monsters attack points. | ||
* @return the prior attack points + the buff. | ||
*/ | ||
public int attack(){ | ||
return 2 + super.attack(); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,45 @@ | ||
public class Main { | ||
public static void main( String [] args ){ | ||
System.out.println("Monster Creator!"); | ||
System.out.println("Choose a monster."); | ||
System.out.println("1. Alien\n2. Beast\n3. Undead"); | ||
int choice = CheckInput.getIntRange(1,3); | ||
monsterBuffer(choice); | ||
} | ||
|
||
/** | ||
* Method that takes the initial choice if user to create a base monster, then buffs. | ||
* @param choice initial choice of monster. | ||
*/ | ||
public static void monsterBuffer(int choice){ | ||
Monster m = null; | ||
if (choice == 1){ | ||
m = new Alien("Alien",2); | ||
|
||
} else if (choice == 2){ | ||
m = new Beast("Beast",3); | ||
} else if (choice == 3){ | ||
m = new Undead("Undead",3); | ||
} | ||
boolean again = true; | ||
while (again){ | ||
System.out.println(m.getName() + " has " + m.getHp() +" health, and attacks for " + m.attack()+ " damage" + | ||
"."); | ||
System.out.println("Add an ability:\n1. Fire\n2. Flying\n3. Lasers\n4. Poison\n5. Quit"); | ||
choice = CheckInput.getIntRange(1,5); | ||
if (choice == 1){ | ||
m = new Fire(m); | ||
} else if (choice == 2){ | ||
m = new Flying(m); | ||
} else if (choice == 3){ | ||
m = new Lasers(m); | ||
} else if (choice == 4){ | ||
m = new Poison(m); | ||
} else if (choice == 5){ | ||
again = false; | ||
} | ||
} | ||
|
||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,39 @@ | ||
public abstract class Monster { | ||
// hp initialization | ||
private int hp; | ||
// name initialization | ||
private String name; | ||
|
||
/** | ||
* Creates a monster with name and hp. | ||
* @param n the name of the monster. | ||
* @param h hp of the monster. | ||
*/ | ||
public Monster(String n, int h){ | ||
this.name = n; | ||
this.hp = h; | ||
} | ||
|
||
/** | ||
* Grabs name of the monster. | ||
* @return the name of the monster object. | ||
*/ | ||
public String getName(){ | ||
return this.name; | ||
} | ||
|
||
/** | ||
* Grabs the hp of the monster and returns it. | ||
* @return the hp of the monster object. | ||
*/ | ||
public int getHp(){ | ||
return this.hp; | ||
} | ||
|
||
/** | ||
* Abstract attack method to be overridden. | ||
* @return returns an integer of the attack points. | ||
*/ | ||
abstract int attack(); | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
public abstract class MonsterDecorator extends Monster { | ||
// Monster to be decorated | ||
private Monster tempMonster; | ||
|
||
/** | ||
* Creates a monster decorated extended from Monster to be buffed/manipulated. | ||
* @param m the Monster object to be buffed/manipulated | ||
* @param extraName extra name to be added to the Monster. | ||
* @param extraHp additional HP to be added to the Monster. | ||
*/ | ||
public MonsterDecorator(Monster m, String extraName, int extraHp) { | ||
super(m.getName() + extraName,m.getHp() + extraHp); | ||
this.tempMonster = m; | ||
} | ||
|
||
/** | ||
* Returns attack points of newly decorated monster. | ||
* @return attack of the newly decorated monster. | ||
*/ | ||
@Override | ||
public int attack() { | ||
return tempMonster.attack(); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
public class Poison extends MonsterDecorator { | ||
/** | ||
* Creates a decorator to give a poison name and health increase to our monster. | ||
* @param m the monster to be decorated. | ||
*/ | ||
public Poison(Monster m) { | ||
super(m, " Toxic Avenger", 1); | ||
} | ||
/** | ||
* Basic attack points of the undead monster. | ||
* @return attack points of undead. | ||
*/ | ||
public int attack(){ | ||
return 3 + super.attack(); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
public class Undead extends Monster{ | ||
/** | ||
* Basic base monster to be decorated with a name and hp. | ||
* @param n name of monster. | ||
* @param h hp of monster. | ||
*/ | ||
public Undead(String n, int h) { | ||
super(n, h); | ||
} | ||
/** | ||
* Basic attack points of the undead monster. | ||
* @return attack points of undead. | ||
*/ | ||
@Override | ||
int attack() { | ||
return 3; | ||
} | ||
} |