MainApplication.java 1.8KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354
  1. /**
  2. * Created by leon on 11/6/17.
  3. */
  4. import java.util.Scanner;
  5. public class MainApplication {
  6. // int numOfPets;
  7. // String kindOfPets;
  8. public static void main(String[] args) {
  9. System.out.println("How many pets do you have?");
  10. Scanner scan = new Scanner(System.in);
  11. int numOfPets = scan.nextInt();
  12. Pet[] kind = new Pet[numOfPets];
  13. int counter = 1;
  14. for (int i = 0; i < numOfPets; i++) {
  15. System.out.println("What kind is animal #" + counter + "?");
  16. String animal = scan.next();
  17. if (animal.equalsIgnoreCase("Dog")) {
  18. Pet dog = new Dog();
  19. String name;
  20. System.out.println("What is their name?");
  21. name = scan.next();
  22. dog.setName(name);
  23. kind[i] = dog;
  24. //kind[i].speak();
  25. } else if (animal.equalsIgnoreCase("Cat") ) {
  26. Pet cat = new Cat();
  27. String name;
  28. System.out.println("What is their name?");
  29. name = scan.next();
  30. cat.setName(name);
  31. kind[i] = cat;
  32. } else if (animal.equalsIgnoreCase("Pig")) {
  33. Pet pig = new Pig();
  34. String name;
  35. System.out.println("What is their name?");
  36. name = scan.next();
  37. pig.setName(name);
  38. kind[i] = pig;
  39. } else {
  40. System.out.println("Please enter a valid type");
  41. }
  42. counter ++;
  43. //System.out.println(kind[i].getName());
  44. }
  45. System.out.println();
  46. for (int i = 0; i < numOfPets; i++){
  47. System.out.println("" + kind[i].getName() + " says "
  48. + kind[i].speak());
  49. }
  50. }
  51. }