Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.util.Scanner;
- public class Pet {
- private String name;
- private int mood, energy, health;
- public Pet(String name, int mood, int energy, int health) {
- this.name = name;
- this.mood = mood;
- this.energy = energy;
- this.health = health;
- }
- public Pet(String name) {
- // overloaded constructor that defaults values of happiness, energy
- // and hygiene to 100 each!
- this(name, 100, 100, 100);
- }
- public void setName(String n) {
- this.name = n;
- }
- public String getName() {
- return name;
- }
- public int getMood() {
- return mood;
- }
- public void setMood(int mood) {
- this.mood = mood;
- }
- public int getEnergy() {
- return energy;
- }
- public void setEnergy(int energy) {
- this.energy = energy;
- }
- public int getHealth() {
- return health;
- }
- public void setHealth(int hygiene) {
- this.health = hygiene;
- }
- public void walk() {
- if (this.energy <= 30) {
- this.mood -= 5;
- System.out.printf("%s does not have enough energy to walk.\n", name);
- } else {
- System.out.println("Walking the dog...");
- this.mood += 5;
- this.energy -= 20;
- this.health -= 5;
- }
- } // end walk()
- public void feed() {
- if (this.energy < 95) {
- //let user know dog does not need to be fed
- System.out.printf("%s does not need to be fed.\n", name);
- } else {
- System.out.println("Feeding the dog...");
- this.mood += 5;
- this.energy += 10;
- }
- } // end feed()
- public void clean() {
- if (this.energy < 80) {
- // let user know dog does not need to be cleaned
- System.out.printf("%s does not need to be cleaned.\n", name);
- } else {
- System.out.println("Cleaning the dog...");
- this.mood += 5;
- this.health += 30;
- }
- } // end clean()
- public static void main(String[] args) {
- Scanner input = new Scanner(System.in);
- int choiceMade;
- System.out.print("Enter dog's name: ");
- String dogName = input.nextLine();
- // Create a Pet object, using the 1-argument constructor!
- Pet myDog = new Pet(dogName);
- do {
- System.out.println("What action would you like to take? ");
- System.out.println(" 1 - Walk the pet" +
- "\n 2 - Feed the virtual pet" +
- "\n3 - Clean the virtual pet" +
- "\n4 - Exit the program");
- choiceMade = input.nextInt();
- if(choiceMade == 1) { // call walk() method
- myDog.walk();
- } else if(choiceMade == 2) { // call feed() method
- myDog.feed();
- } else if(choiceMade == 3) { // call clean() method
- myDog.clean();
- } else if(choiceMade == 4) { // print pet info then exit the program
- print(myDog);
- System.exit(0);
- } else {
- System.out.println("Invalid choice made. Choose between 1 to 4 only.");
- }
- } while(showMenuAgain(input));
- } // end main()
- public static void print(Pet pet) {
- System.out.println("The pet's name is " + pet.getName() + ".");
- System.out.println("The pet's mood status is : " + pet.getMood());
- System.out.println("The pet's energy status is : " + pet.getEnergy());
- System.out.printf("The pet's health status is : %,d\n",pet.getHealth());
- } // end print()
- private static boolean showMenuAgain(Scanner s) {
- System.out.print("\nShow menu again? (Y or N): ");
- char response = s.next().charAt(0);
- while(!((response == 'y')
- || (response == 'Y')
- || (response == 'N')
- || (response == 'n'))) {
- System.out.print("\nInvalid input. ");
- System.out.print("Do you want show menu? (Y or N): ");
- response = s.next().charAt(0);
- } // end while, input validation
- return response == 'y' || response == 'Y';
- } // end showMenuAgain()
- } // end Pet class
Advertisement
Add Comment
Please, Sign In to add comment