Kaynağa Gözat

FInal Changes

Elliott Stansbury 6 yıl önce
ebeveyn
işleme
b3638aebda

+ 28
- 0
pom.xml Dosyayı Görüntüle

@@ -7,6 +7,34 @@
7 7
     <groupId>io.zipcoder</groupId>
8 8
     <artifactId>polymorphism</artifactId>
9 9
     <version>1.0-SNAPSHOT</version>
10
+    <build>
11
+        <plugins>
12
+            <plugin>
13
+                <groupId>org.apache.maven.plugins</groupId>
14
+                <artifactId>maven-compiler-plugin</artifactId>
15
+                <configuration>
16
+                    <source>7</source>
17
+                    <target>7</target>
18
+                </configuration>
19
+            </plugin>
20
+        </plugins>
21
+    </build>
22
+
23
+    <dependencies>
24
+        <!-- https://mvnrepository.com/artifact/org.apache.commons/commons-collections4 -->
25
+        <dependency>
26
+            <groupId>org.apache.commons</groupId>
27
+            <artifactId>commons-collections4</artifactId>
28
+            <version>4.2</version>
29
+        </dependency>
30
+        <dependency>
31
+            <groupId>junit</groupId>
32
+            <artifactId>junit</artifactId>
33
+            <version>RELEASE</version>
34
+            <scope>test</scope>
35
+        </dependency>
36
+
37
+    </dependencies>
10 38
 
11 39
 
12 40
 </project>

+ 3
- 1
src/main/java/io/zipcoder/polymorphism/Cat.java Dosyayı Görüntüle

@@ -5,11 +5,13 @@ public class Cat extends Pet {
5 5
     //public static Speak()
6 6
 
7 7
     public Cat(String petName){
8
+
8 9
         super(petName);
9 10
     }
10 11
 
11 12
     @Override
12
-    public  String speak(){
13
+    public String speak(){
14
+        System.out.println("MEOW MEOW MEOW MEOW MEOW MEOW MEOW!");
13 15
         return "MEOW MEOW MEOW MEOW MEOW MEOW MEOW!";
14 16
     }
15 17
 }

+ 3
- 0
src/main/java/io/zipcoder/polymorphism/Dog.java Dosyayı Görüntüle

@@ -18,6 +18,7 @@ public class Dog extends Pet {
18 18
     private String petName;
19 19
 
20 20
     public Dog(String petName){
21
+
21 22
         super(petName);
22 23
     }
23 24
 
@@ -26,6 +27,8 @@ public class Dog extends Pet {
26 27
 
27 28
     @Override
28 29
     public String speak(){
30
+
31
+        System.out.println("BARK BARK BARK BARK BARK!!!");
29 32
         return "BARK BARK BARK BARK BARK!!!";
30 33
     }
31 34
 }

+ 90
- 36
src/main/java/io/zipcoder/polymorphism/MainApplication.java Dosyayı Görüntüle

@@ -1,5 +1,7 @@
1 1
 package io.zipcoder.polymorphism;
2 2
 
3
+import java.sql.SQLOutput;
4
+import java.util.ArrayList;
3 5
 import java.util.Scanner;
4 6
 
5 7
 /**
@@ -7,64 +9,116 @@ import java.util.Scanner;
7 9
  */
8 10
 public class MainApplication {
9 11
 
12
+    ArrayList<Pet> pets = new ArrayList<>();
13
+
14
+    int numberOfPets = 0;
15
+    String typeOfPet = "";
16
+    String nameOfPet = "";
17
+
18
+
19
+
20
+    Scanner scanner = new Scanner(System.in);
21
+
10 22
     public static void main(String[] args) {
11 23
 
24
+
25
+        MainApplication app = new MainApplication();
26
+
27
+        //int numberOfPets;
28
+
29
+
12 30
         Scanner scanner = new Scanner(System.in);
13 31
         StringBuilder builder1 = new StringBuilder();
14 32
         StringBuilder builder2 = new StringBuilder();
15 33
 
16
-        String typeOfPet = "";
17
-        String nameOfPet = "";
18 34
 
19
-        System.out.println("How many pets do you currently own? You can include your kids as well! :)");
20
-        int numberOfPets = Integer.parseInt(scanner.nextLine());
35
+        app.askAmount();
36
+        String petTypes = app.askType().toString();
37
+        String petNames = app.askName().toString();
21 38
 
39
+       System.out.println("Your pets seem amazing, cant believe you have " + app.numberOfPets + " that are " + petTypes + " named " + petNames);
22 40
 
23
-        for (int i = 0; i < numberOfPets; i++) {
24
-            typeOfPet = scanner.nextLine();
25
-            builder1.append(typeOfPet);
26
-            if (i < numberOfPets) {
27
-                System.out.println("Do you have more pets? If so what kind is the next?");
28
-                builder1.append(", ");
29
-            }
41
+        app.allPetsSpeak();
30 42
 
31 43
 
32
-            System.out.println("What's the first one's name?");
44
+    }
33 45
 
34
-            for (int j = 0; j < i; j++) {
35
-                nameOfPet = scanner.nextLine();
36
-                builder2.append(nameOfPet);
37
-                if (j < numberOfPets) {
38
-                    System.out.println("Do you have more pets? If so what kind is the next?");
39
-                    builder2.append(", ");
40 46
 
41
-                }
42
-            }
43 47
 
48
+    public void askAmount(){
49
+        System.out.println("How many pets do you have?");
50
+        numberOfPets = Integer.parseInt(scanner.nextLine());
51
+
52
+    }
53
+
54
+    public StringBuilder askType(){
55
+        StringBuilder builder = new StringBuilder();
56
+
57
+
58
+
59
+        for(int i = 0; i < numberOfPets; i++){
60
+
61
+            System.out.println("What kinds of pets do you have?");
62
+            String typeOfPet = scanner.nextLine();
63
+            builder.append(typeOfPet);
64
+            typeOfPet.trim();
65
+            typeOfPet.toLowerCase();
66
+            switch(typeOfPet){
67
+                case "dog":
68
+                    Dog dog = new Dog(nameOfPet);
69
+                    //dog.speak();
70
+                    pets.add(dog);
71
+                    break;
44 72
 
45
-            System.out.println(builder1.toString());
46
-            System.out.println(builder2.toString());
73
+                case "cat":
74
+                    Cat cat = new Cat(nameOfPet);
75
+                    //cat.speak();
76
+                    pets.add(cat);
77
+                    break;
78
+
79
+                case "pig":
80
+                    Pig pig = new Pig(nameOfPet);
81
+                    //pig.speak();
82
+                    pets.add(pig);
83
+                    break;
47 84
 
48
-            String[] petNameArray = builder2.toString().split(", ");
49
-            String[] petKindArray = builder1.toString().split(", ");
50 85
 
51
-            for (int k = 0; k < numberOfPets; k++) {
52
-                Dog dog = new Dog(petNameArray[k]);
53
-                Cat cat = new Cat(petNameArray[k]);
54
-                String sounds = "";
55 86
 
56
-                if (petKindArray[k].equals("cat")) {
57
-                    sounds = cat.speak();
58
-                } else if (petKindArray[k].equals("dog")) {
59
-                    sounds = dog.speak();
60
-                } else {
61
-                    sounds = "White Noise!!!";
62
-                }
63 87
             }
88
+
89
+            builder.append(" & ");
90
+
91
+        }
92
+
93
+
94
+
95
+        return builder;
96
+    }
97
+
98
+    public StringBuilder askName(){
99
+        StringBuilder builder = new StringBuilder();
100
+
101
+
102
+        for(int i = 0; i < numberOfPets; i++){
103
+            System.out.println("Whats the name of your pet?");
104
+            String nameOfPet = scanner.next();
105
+            builder.append(nameOfPet);
106
+            builder.append(", ");
107
+
64 108
         }
109
+
110
+        return builder;
65 111
     }
66
-}
67 112
 
113
+    public void allPetsSpeak() {
114
+        for (int j = 0; j < pets.size(); j++) {
115
+            pets.get(j).speak();
116
+        }
117
+    }
118
+
119
+ }
120
+
121
+//
68 122
 //    public class Pet {
69 123
 //        public void Speak(){
70 124
 //            //String sound

+ 3
- 0
src/main/java/io/zipcoder/polymorphism/Pet.java Dosyayı Görüntüle

@@ -5,16 +5,19 @@ public abstract class Pet{
5 5
     private String petName;
6 6
 
7 7
     public Pet(String petName){
8
+
8 9
         this.petName = petName;
9 10
     }
10 11
 
11 12
     public String getName(){
13
+
12 14
         return petName;
13 15
     }
14 16
 
15 17
     abstract String speak();
16 18
 
17 19
     public void setPetName(String petName){
20
+
18 21
         this.petName = petName;
19 22
     }
20 23
 }

+ 17
- 0
src/main/java/io/zipcoder/polymorphism/Pig.java Dosyayı Görüntüle

@@ -0,0 +1,17 @@
1
+package io.zipcoder.polymorphism;
2
+
3
+public class Pig extends Pet {
4
+
5
+    //public static Speak()
6
+
7
+    public Pig(String petName){
8
+
9
+        super(petName);
10
+    }
11
+
12
+    @Override
13
+    public String speak(){
14
+        System.out.println("Oink Oink Oink!");
15
+        return "Oink Oink Oink!";
16
+    }
17
+}

+ 42
- 0
src/test/java/io/zipcoder/polymorphism/MainApplicationTest.java Dosyayı Görüntüle

@@ -1,8 +1,50 @@
1 1
 package io.zipcoder.polymorphism;
2 2
 
3
+import org.junit.Assert;
4
+import org.junit.Test;
5
+
3 6
 /**
4 7
  * Created by leon on 11/6/17.
5 8
  */
6 9
 public class MainApplicationTest {
7 10
 
11
+    @Test
12
+    public void dogTest(){  //test dog speaking
13
+        ///GIVEN
14
+        Dog dog = new Dog("Anthony");
15
+
16
+        ///WHEN
17
+
18
+
19
+        ///RESULT
20
+        Assert.assertEquals("BARK BARK BARK BARK BARK!!!", dog.speak());
21
+
22
+    }
23
+
24
+    @Test
25
+    public void catTest(){  //test dog speaking
26
+        ///GIVEN
27
+        Cat cat = new Cat("Selina");
28
+
29
+        ///WHEN
30
+
31
+
32
+        ///RESULT
33
+        Assert.assertEquals("MEOW MEOW MEOW MEOW MEOW MEOW MEOW!", cat.speak());
34
+
35
+    }
36
+
37
+    @Test
38
+    public void pigTest(){  //test dog speaking
39
+        ///GIVEN
40
+        Pig pig = new Pig("Jester");
41
+
42
+        ///WHEN
43
+
44
+
45
+        ///RESULT
46
+        Assert.assertEquals("Oink Oink Oink!", pig.speak());
47
+
48
+    }
49
+
8 50
 }