binibiningtinamoran

Pet.java

Nov 24th, 2019
388
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 4.34 KB | None | 0 0
  1. import java.util.Scanner;
  2.  
  3. public class Pet {
  4.  
  5.     private String name;
  6.     private int mood, energy, health;
  7.  
  8.     public Pet(String name, int mood, int energy, int health) {
  9.         this.name = name;
  10.         this.mood = mood;
  11.         this.energy = energy;
  12.         this.health = health;
  13.     }
  14.  
  15.     public Pet(String name) {
  16.         // overloaded constructor that defaults values of happiness, energy
  17.         // and hygiene to 100 each!
  18.         this(name, 100, 100, 100);
  19.     }
  20.  
  21.     public void setName(String n) {
  22.         this.name = n;
  23.     }
  24.  
  25.     public String getName() {
  26.         return name;
  27.     }
  28.  
  29.     public int getMood() {
  30.         return mood;
  31.     }
  32.  
  33.     public void setMood(int mood) {
  34.         this.mood = mood;
  35.     }
  36.  
  37.     public int getEnergy() {
  38.         return energy;
  39.     }
  40.  
  41.     public void setEnergy(int energy) {
  42.         this.energy = energy;
  43.     }
  44.  
  45.     public int getHealth() {
  46.         return health;
  47.     }
  48.  
  49.     public void setHealth(int hygiene) {
  50.         this.health = hygiene;
  51.     }
  52.  
  53.     public void walk() {
  54.         if (this.energy <= 30) {
  55.             this.mood -= 5;
  56.             System.out.printf("%s does not have enough energy to walk.\n", name);
  57.         } else {
  58.             System.out.println("Walking the dog...");
  59.             this.mood += 5;
  60.             this.energy -= 20;
  61.             this.health -= 5;
  62.         }
  63.     } // end walk()
  64.    
  65.     public void feed() {
  66.         if (this.energy < 95) {
  67.             //let user know dog does not need to be fed
  68.             System.out.printf("%s does not need to be fed.\n", name);
  69.         } else {
  70.             System.out.println("Feeding the dog...");
  71.             this.mood += 5;
  72.             this.energy += 10;
  73.         }
  74.     } // end feed()
  75.    
  76.     public void clean() {
  77.         if (this.energy < 80) {
  78.             // let user know dog does not need to be cleaned
  79.             System.out.printf("%s does not need to be cleaned.\n", name);
  80.         } else {
  81.             System.out.println("Cleaning the dog...");
  82.             this.mood += 5;
  83.             this.health += 30;
  84.         }
  85.     } // end clean()
  86.  
  87.  
  88.     public static void main(String[] args) {
  89.         Scanner input = new Scanner(System.in);
  90.         int choiceMade;
  91.  
  92.         System.out.print("Enter dog's name: ");
  93.         String dogName = input.nextLine();
  94.  
  95.         // Create a Pet object, using the 1-argument constructor!
  96.         Pet myDog = new Pet(dogName);
  97.  
  98.         do {
  99.             System.out.println("What action would you like to take? ");
  100.             System.out.println(" 1 - Walk the pet" +
  101.                     "\n 2 - Feed the virtual pet" +
  102.                     "\n3 - Clean the virtual pet" +
  103.                     "\n4 - Exit the program");
  104.             choiceMade = input.nextInt();
  105.  
  106.             if(choiceMade == 1) { // call walk() method
  107.                 myDog.walk();
  108.             } else if(choiceMade == 2) { // call feed() method
  109.                 myDog.feed();
  110.             } else if(choiceMade == 3) { // call clean() method
  111.                 myDog.clean();
  112.             } else if(choiceMade == 4) { // print pet info then exit the program
  113.                 print(myDog);
  114.                 System.exit(0);
  115.             } else {
  116.                 System.out.println("Invalid choice made. Choose between 1 to 4 only.");
  117.             }
  118.        } while(showMenuAgain(input));
  119.     } // end main()
  120.  
  121.     public static void print(Pet pet) {
  122.         System.out.println("The pet's name is " + pet.getName() + ".");
  123.         System.out.println("The pet's mood status is : " + pet.getMood());
  124.         System.out.println("The pet's energy status is : " + pet.getEnergy());
  125.         System.out.printf("The pet's health status is : %,d\n",pet.getHealth());
  126.     } // end print()
  127.  
  128.     private static boolean showMenuAgain(Scanner s) {
  129.         System.out.print("\nShow menu again? (Y or N): ");
  130.         char response = s.next().charAt(0);
  131.  
  132.         while(!((response == 'y')
  133.                 || (response == 'Y')
  134.                 || (response == 'N')
  135.                 || (response == 'n'))) {
  136.             System.out.print("\nInvalid input. ");
  137.             System.out.print("Do you want show menu? (Y or N): ");
  138.             response = s.next().charAt(0);
  139.         } // end while, input validation
  140.         return response == 'y' || response == 'Y';
  141.     } // end showMenuAgain()
  142.    
  143.    
  144. } // end Pet class
Advertisement
Add Comment
Please, Sign In to add comment