Browse Source

Finished for if loop for pets array

Trinh Tong 6 years ago
parent
commit
14268f54d9
1 changed files with 36 additions and 32 deletions
  1. 36
    32
      src/main/java/io/zipcoder/polymorphism/MainApplication.java

+ 36
- 32
src/main/java/io/zipcoder/polymorphism/MainApplication.java View File

15
 
15
 
16
         int numOfPets = keyboard.nextInt();
16
         int numOfPets = keyboard.nextInt();
17
         // Create array of objects [numOfPets]
17
         // Create array of objects [numOfPets]
18
+        if (numOfPets > 0) {
19
+            Pet[] pets = new Pet[numOfPets];
18
 
20
 
19
-        Pet[] pets = new Pet[numOfPets];
21
+            // What kind of pet each one is using a loop
22
+            for (int i = 0; i < pets.length; i++) {
23
+                System.out.print("What kind of pet do you have?\n\n");
24
+                System.out.print("1: Dog\n2: Cat\n3: Duck\n4: Frog\n(Enter the key for the pet)\n");
25
+                int petType = keyboard.nextInt();
26
+                // Takes in the type and creates that object
20
 
27
 
21
-        // What kind of pet each one is using a loop
22
-        for (int i = 0; i < pets.length; i++) {
23
-            System.out.print("What kind of pet do you have?\n\n");
24
-            System.out.print("1: Dog\n2: Cat\n3: Duck\n4: Frog\n(Enter the key for the pet)\n");
25
-            int petType = keyboard.nextInt();
26
-            // Takes in the type and creates that object
28
+                if (petType == 1) {
29
+                    pets[i] = new Dog();
30
+                    System.out.print("What is your dog's name?\n");
31
+                    String petName = keyboard.next();
32
+                    System.out.println("Pet Name: " + petName);
33
+                    pets[i].setName(petName);
27
 
34
 
28
-            if (petType == 1) {
29
-                pets[i] = new Dog();
30
-                System.out.print("What is your dog's name?\n");
31
-                String petName = keyboard.next();
32
-                System.out.println("Pet Name: " + petName);
33
-                pets[i].setName(petName);
35
+                } else if (petType == 2) {
36
+                    pets[i] = new Cat();
37
+                    System.out.print("What is your cat's name?\n");
38
+                    String petName = keyboard.next();
39
+                    pets[i].setName(petName);
34
 
40
 
35
-            } else if (petType == 2) {
36
-                pets[i] = new Cat();
37
-                System.out.print("What is your cat's name?\n");
38
-                String petName = keyboard.next();
39
-                pets[i].setName(petName);
41
+                } else if (petType == 3) {
42
+                    pets[i] = new Duck();
43
+                    System.out.print("What is your duck's name?\n");
44
+                    String petName = keyboard.next();
45
+                    pets[i].setName(petName);
40
 
46
 
41
-            } else if (petType == 3) {
42
-                pets[i] = new Duck();
43
-                System.out.print("What is your duck's name?\n");
44
-                String petName = keyboard.next();
45
-                pets[i].setName(petName);
46
-
47
-            } else if (petType == 4) {
48
-                pets[i] = new Frog();
49
-                System.out.print("What is your frog's name?\n");
50
-                String petName = keyboard.next();
51
-                pets[i].setName(petName);
47
+                } else if (petType == 4) {
48
+                    pets[i] = new Frog();
49
+                    System.out.print("What is your frog's name?\n");
50
+                    String petName = keyboard.next();
51
+                    pets[i].setName(petName);
52
+                }
52
             }
53
             }
53
-        }
54
-        for (int i = 0; i < pets.length; i++) {
55
-
54
+            for (int i = 0; i < pets.length; i++) {
55
+                System.out.print((i + 1) + ": " + pets[i].getName() + " says, \"" + pets[i].speak() + "\"\n");
56
+            }
57
+        } else {
58
+            System.out.print("Invalid input. Exiting program.");
59
+            System.exit(1);
56
         }
60
         }
57
         // Ask what kind of pet
61
         // Ask what kind of pet
58
         // Option menu for types of pets.
62
         // Option menu for types of pets.