MainApplication.java 2.3KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677
  1. import java.util.Scanner;
  2. import java.util.InputMismatchException;
  3. import java.util.Arrays;
  4. public class MainApplication {
  5. private static Pet pet;
  6. private static Pet[] petList;
  7. public MainApplication(){
  8. }
  9. public static void main(String[] args){
  10. process();
  11. }
  12. public static void process(){
  13. int numPets;
  14. System.out.println("How many pets do you have?");
  15. numPets = getNumberPets();
  16. System.out.println("You have " + numPets + " pet(s)!");
  17. petList = new Pet[numPets];
  18. Scanner input = new Scanner(System.in);
  19. if (numPets > 0){
  20. for (int i = 0; i < numPets; i++){
  21. System.out.println("What type is pet " + (i+1) + "?");
  22. String typeCheck = input.nextLine();
  23. System.out.println("What is it's name?");
  24. String name = input.nextLine();
  25. if (typeCheck.toLowerCase().equals("dog")){
  26. petList[i] = new Dog(name);
  27. } else if (typeCheck.toLowerCase().equals("cat")){
  28. petList[i] = new Cat(name);
  29. } else {
  30. petList[i] = new Chicken(name);
  31. }
  32. }
  33. }
  34. if (numPets > 0){
  35. StringBuilder list = new StringBuilder();
  36. for (Pet element: petList){
  37. list.append(element.getName() + " ");
  38. }
  39. System.out.println("Your pets are:\n " + list.toString());
  40. for (Pet element: petList){
  41. System.out.println(element.getName() + " says " + element.speak());
  42. }
  43. }
  44. }
  45. public static int getNumberPets(){
  46. int numberPets = 0;
  47. Scanner input = new Scanner(System.in);
  48. boolean error = true;
  49. do{
  50. try{
  51. numberPets = input.nextInt();
  52. error = false;
  53. } catch (InputMismatchException e){
  54. input.next();
  55. System.out.println("Error! Please type a number");
  56. }
  57. } while (error);
  58. if (numberPets == 0){
  59. return 0;
  60. }
  61. return numberPets;
  62. }
  63. public String setPetName(){
  64. Scanner input = new Scanner(System.in);
  65. String petName = input.nextLine();
  66. return petName;
  67. }
  68. }