package chapter4; import java.util.Random; import java.util.Scanner; public class SimplePet { //while the pet is still alive, you have options of what to do with it. public static void main(String[] args) { //pet statistics int health = 5; //0 is dead, 5 is average, 10 is healthy int happiness = 5; //0 is sad, 10 is happy //actions final int FEED = 1; final int PLAY = 2; //others to add later //foods final int MEAT = 1; final int SALAD = 2; final int CANDY = 3; //games final int SWIMMING = 1; final int BASKETBALL = 2; final int FRISBEE = 3; //Java objects Random rng = new Random(); Scanner in = new Scanner(System.in); //modify later: choose type of pet to have System.out.println("Congratulations! Here is your new pet wolf!"); System.out.println();//a blank line at the top System.out.println(" * * "); System.out.println(" * ****** * "); System.out.println(" * # # * ");//# are the eyes System.out.println(" * * "); System.out.println(" *** * "); System.out.println(" * * "); System.out.println(" * * "); System.out.println(" * * * *"); System.out.println(" * * * ** "); System.out.println(" * * * ** "); System.out.println(" ***************** "); System.out.println(); //as long as the pet is alive while (health > 0) { //ask what activity they want to do int activityChoice = 0; do { System.out.println("What activity would you like to do today? 1 Feed, 2 Play:"); if (in.hasNextInt()) { activityChoice = in.nextInt(); } else { System.out.println("Your input was not recognized. Please try again: "); } }while (activityChoice != 1 && activityChoice != 2); //perform the activity they chose switch (activityChoice) { case FEED: //what food do they wish to feed their pet int foodChoice = 0; do { System.out.println("What food would your pet like to eat? 1 Meat, 2 Salad, 3 Candy:"); if (in.hasNextInt()) foodChoice = in.nextInt(); }while (foodChoice <1 || foodChoice > 3); //give the desired food and modify the health and happiness if (foodChoice == MEAT) { happiness ++; //Wolves are carnivores health ++; } else if (foodChoice == SALAD) { health += 2; //salad is healthy happiness --; //wolves do not like salad } else //candy was fed { health --; //candy is not healthy happiness++; } break; case PLAY: //whatgame do they wish to play int gameChoice = 0; do { System.out.println("What game would you like to play? 1 Swim, 2 Basketball, 3 Frisbee"); if (in.hasNextInt()) { gameChoice = in.nextInt(); } }while (gameChoice != 1 && gameChoice != 2 && gameChoice != 3); //play the chosen game if (gameChoice == SWIMMING) { happiness ++; health ++; } else if (gameChoice == BASKETBALL) { happiness --; //wolves sharp teeth puncture the ball health ++; } else //frisbee { happiness += 2; health ++; } break; } //end switch }//end while System.out.println(":( Sorry your pet has died."); }//end main }//end class