MainApplication.java 1.5KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657
  1. /**
  2. * Created by leon on 11/6/17.
  3. */
  4. import java.util.Scanner;
  5. import java.util.*;
  6. public class MainApplication {
  7. private enum petTypes {DOG, CAT, FISH};
  8. public static void Main(String [] args) {
  9. Scanner in = new Scanner(System.in);
  10. System.out.println("How many pets do you have?");
  11. int numPet = in.nextInt();
  12. Pet[] arr = new Pet[numPet];//get size of the array of pets
  13. for (int i=0;i<numPet;i++){
  14. System.out.println("What type of pet do you have?");
  15. String typePet = in.nextLine();
  16. System.out.println("Name of the pets?");
  17. String petName = in.nextLine();
  18. switch (petTypes.valueOf(typePet.toUpperCase())){
  19. //access enum to get value
  20. case CAT:
  21. arr[i] = new Cat(petName);
  22. break;
  23. case DOG:
  24. arr[i] = new Dog(petName);
  25. break;
  26. case FISH:
  27. arr[i] = new Fish(petName);
  28. break;
  29. }
  30. //arr [i] = (typePet + ":" + petName);
  31. }
  32. for(Pet i: arr){
  33. System.out.println(i.getName() + ":"+ i.getType() + "says" + i.speak());
  34. }
  35. //String output = Arrays.toString(arr);
  36. }
  37. }
  38. /*System.out.println("What pets do you have?");
  39. String petType = in.nextLine();
  40. if (numPet == 1){
  41. System.out.println();
  42. else if (numPet >1)
  43. {
  44. }
  45. }
  46. System.out.println("What are their name?");
  47. */