Skip to content

Commit

Permalink
Using a decorator patter, add decorations to classes
Browse files Browse the repository at this point in the history
  • Loading branch information
diegogarciacs authored Jan 2, 2022
1 parent aae56d5 commit 9fcd6bb
Show file tree
Hide file tree
Showing 25 changed files with 372 additions and 0 deletions.
Binary file added Lab 13/CECS277-Lab13.pdf
Binary file not shown.
11 changes: 11 additions & 0 deletions Lab 13/Lab 13.iml
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 added Lab 13/out/production/Lab 13/Alien.class
Binary file not shown.
Binary file added Lab 13/out/production/Lab 13/Beast.class
Binary file not shown.
Binary file added Lab 13/out/production/Lab 13/CheckInput.class
Binary file not shown.
Binary file added Lab 13/out/production/Lab 13/Fire.class
Binary file not shown.
Binary file added Lab 13/out/production/Lab 13/Flying.class
Binary file not shown.
Binary file added Lab 13/out/production/Lab 13/Lasers.class
Binary file not shown.
Binary file added Lab 13/out/production/Lab 13/Main.class
Binary file not shown.
Binary file added Lab 13/out/production/Lab 13/Monster.class
Binary file not shown.
Binary file not shown.
Binary file added Lab 13/out/production/Lab 13/Poison.class
Binary file not shown.
Binary file added Lab 13/out/production/Lab 13/Undead.class
Binary file not shown.
19 changes: 19 additions & 0 deletions Lab 13/src/Alien.java
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;
}
}
18 changes: 18 additions & 0 deletions Lab 13/src/Beast.java
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;
}
}
133 changes: 133 additions & 0 deletions Lab 13/src/CheckInput.java
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;
}
}
17 changes: 17 additions & 0 deletions Lab 13/src/Fire.java
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();
}
}
16 changes: 16 additions & 0 deletions Lab 13/src/Flying.java
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 added Lab 13/src/Group1Lab13.zip
Binary file not shown.
16 changes: 16 additions & 0 deletions Lab 13/src/Lasers.java
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();
}
}
45 changes: 45 additions & 0 deletions Lab 13/src/Main.java
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;
}
}

}

}
39 changes: 39 additions & 0 deletions Lab 13/src/Monster.java
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();

}
24 changes: 24 additions & 0 deletions Lab 13/src/MonsterDecorator.java
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();
}
}
16 changes: 16 additions & 0 deletions Lab 13/src/Poison.java
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();
}
}
18 changes: 18 additions & 0 deletions Lab 13/src/Undead.java
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;
}
}

0 comments on commit 9fcd6bb

Please sign in to comment.