-
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.
State Pattern for a puppy simulation
3 possible states of sleeping, eating or playing.
- Loading branch information
1 parent
9fcd6bb
commit 8d27968
Showing
9 changed files
with
317 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> |
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,21 @@ | ||
public class AsleepState implements PuppyState{ | ||
/** | ||
* Function that returns a string that tells the user it's currently asleep and unwilling to play. | ||
* @return string detailing the current puppy status. | ||
*/ | ||
@Override | ||
public String play(Puppy p) { | ||
return "Puppy is asleep. It doesn't wanna play now."; | ||
} | ||
/** | ||
* Function that sets the puppy into an eatingState, increments the number of feeds, and returns a string of the puppy action. | ||
* @return string detailing the current puppy status. | ||
*/ | ||
@Override | ||
public String feed(Puppy p) { | ||
p.setState(new EatingState()); | ||
p.incFeeds(); | ||
return "The puppy smells the food and jolts awake."; | ||
} | ||
|
||
} |
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,25 @@ | ||
public class EatingState implements PuppyState{ | ||
/** | ||
* Function that increments the puppys plays and sets the puppy into the playstate. | ||
* @return string detailing the current puppy status. | ||
*/ | ||
@Override | ||
public String play(Puppy p) { | ||
p.setState(new PlayState()); | ||
p.incPlays(); | ||
return "The puppy looks up from it's food and chases down the ball."; | ||
} | ||
/** | ||
* Function that increments the puppies feeds (unless it reaches a increment greater than 2, where it's reset to default asleep state) and returns current puppy action. | ||
* @return string detailing current puppy status. | ||
*/ | ||
@Override | ||
public String feed(Puppy p) { | ||
int n = p.incFeeds(); | ||
if (n > 2){ | ||
p.reset(); | ||
return "The puppy eats so much it falls asleep"; | ||
} | ||
return "The puppy continues to eat as you add another scoop of kibble to it's bowl."; | ||
} | ||
} |
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,26 @@ | ||
/* | ||
Vo, Nguyen | ||
Garcia, Diego | ||
CS277 | ||
LAB 14 | ||
12/09/2021 | ||
*/ | ||
|
||
class Main { | ||
public static void main(String[] args) { | ||
|
||
System.out.println("Congratulations! You just got a new puppy!"); | ||
Puppy p = new Puppy(); | ||
int choice = 0; | ||
while(choice != 3) | ||
{ | ||
|
||
System.out.println("1. Feed\n2. Play\n3. Quit"); | ||
choice = CheckInput.getIntRange(1,3); | ||
if(choice == 1) | ||
System.out.println(p.giveFood()); | ||
else if(choice == 2) | ||
System.out.println(p.throwBall()); | ||
} | ||
} | ||
} |
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,31 @@ | ||
public class PlayState implements PuppyState | ||
{ | ||
/** | ||
* Function that increments the puppys plays (unless it's over 2 in which case it resets to default sleep state) and returns a string. | ||
* @return string detailing the current puppy status. | ||
*/ | ||
@Override | ||
public String play(Puppy p) | ||
{ | ||
int n = p.incPlays(); | ||
if (n > 2){ | ||
p.reset(); | ||
return "The puppy played so much it feel asleep!"; | ||
} | ||
return "The puppy bounds after the ball."; | ||
} | ||
/** | ||
* Function that increments the puppys feeds (unless that value is over 2 in which case it resets to default sleep state) and returns a string. | ||
* @return string detailing the current puppy status. | ||
*/ | ||
@Override | ||
public String feed(Puppy p) | ||
{ | ||
int n = p.incFeeds(); | ||
if (n > 2){ | ||
p.reset(); | ||
return "The puppy ate so much that it fell asleep"; | ||
} | ||
return "The puppy excitedly begins eating."; | ||
} | ||
} |
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,58 @@ | ||
public class Puppy { | ||
private PuppyState state; | ||
private int numFeeds; | ||
private int numPlays; | ||
public Puppy(){ | ||
numFeeds = 0; | ||
numPlays = 0; | ||
state = new AsleepState(); | ||
} | ||
|
||
/** | ||
set the state with the given paremeter s | ||
@param s the state being passed in | ||
*/ | ||
public void setState(PuppyState s){ | ||
state = s; | ||
} | ||
|
||
/** | ||
returns a message when giving food to the puppy | ||
@return a message based on the puppy's state | ||
*/ | ||
public String throwBall(){ | ||
return state.play(this); | ||
} | ||
/** | ||
returns a message when playing with the puppy | ||
@return a message based on the puppy's state | ||
*/ | ||
public String giveFood(){ | ||
return state.feed(this); | ||
} | ||
/** | ||
returns the count for numFeeds | ||
@return the count for numFeeds | ||
*/ | ||
public int incFeeds(){ | ||
numFeeds++; | ||
return numFeeds; | ||
} | ||
/** | ||
returns the count for numPlays | ||
@return the count for numPlays | ||
*/ | ||
public int incPlays(){ | ||
numPlays++; | ||
return numPlays; | ||
} | ||
|
||
|
||
/** resets the state and the counts for numPlays/numFeeds | ||
*/ | ||
public void reset(){ | ||
numFeeds = 0; | ||
numPlays = 0; | ||
state = new AsleepState(); | ||
} | ||
} |
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,12 @@ | ||
public interface PuppyState { | ||
/** | ||
* State will override this play function. | ||
* @return returns puppy status string based on state | ||
*/ | ||
public String play(Puppy p); | ||
/** | ||
* State will override this play function. | ||
* @return returns puppy status string based on state | ||
*/ | ||
public String feed(Puppy p); | ||
} |