#30 jaejoson Talking Pets

Öppen
jaejoson vill sammanfoga 1 incheckningar från s[2]s in i master

+ 10
- 1
pom.xml Visa fil

@@ -2,11 +2,20 @@
2 2
 <project xmlns="http://maven.apache.org/POM/4.0.0"
3 3
          xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
4 4
          xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
5
-    <modelVersion>4.0.0</modelVersion>
6 5
 
6
+    <modelVersion>4.0.0</modelVersion>
7 7
     <groupId>io.zipcoder</groupId>
8 8
     <artifactId>polymorphism</artifactId>
9 9
     <version>1.0-SNAPSHOT</version>
10 10
 
11
+    <dependencies>
12
+        <!-- https://mvnrepository.com/artifact/junit/junit -->
13
+        <dependency>
14
+            <groupId>junit</groupId>
15
+            <artifactId>junit</artifactId>
16
+            <version>4.12</version>
17
+            <scope>test</scope>
18
+        </dependency>
19
+    </dependencies>
11 20
 
12 21
 </project>

+ 13
- 0
src/main/java/io/zipcoder/polymorphism/Cat.java Visa fil

@@ -0,0 +1,13 @@
1
+package io.zipcoder.polymorphism;
2
+
3
+public class Cat extends Pet {
4
+    public Cat (String kindOfPet, String petName) {
5
+        super(kindOfPet, petName);
6
+        setKindOfPet(kindOfPet);
7
+        setPetName(petName);
8
+    }
9
+
10
+    public String speak() {
11
+        return "Meow";
12
+    }
13
+}

+ 21
- 0
src/main/java/io/zipcoder/polymorphism/Dog.java Visa fil

@@ -0,0 +1,21 @@
1
+package io.zipcoder.polymorphism;
2
+import java.util.Scanner;
3
+
4
+public class Dog extends Pet {
5
+
6
+    public Dog (String kindOfPet, String petName) {
7
+        super(kindOfPet, petName);
8
+        setKindOfPet(kindOfPet);
9
+        setPetName(petName);
10
+    }
11
+
12
+    @Override
13
+    public String speak() {
14
+        return "Woof";
15
+    }
16
+
17
+    @Override
18
+    public String getPetName() {
19
+        return super.getPetName();
20
+    }
21
+}

+ 14
- 0
src/main/java/io/zipcoder/polymorphism/Lizard.java Visa fil

@@ -0,0 +1,14 @@
1
+package io.zipcoder.polymorphism;
2
+
3
+public class Lizard extends Pet {
4
+    public Lizard(String kindOfPet, String petName) {
5
+        super(kindOfPet, petName);
6
+        setKindOfPet(kindOfPet);
7
+        setPetName(petName);
8
+    }
9
+
10
+        public String speak() {
11
+            return "Hiss";
12
+        }
13
+    }
14
+

+ 15
- 1
src/main/java/io/zipcoder/polymorphism/MainApplication.java Visa fil

@@ -1,7 +1,21 @@
1 1
 package io.zipcoder.polymorphism;
2 2
 
3
+
4
+
5
+import java.util.ArrayList;
6
+import java.util.Scanner;
7
+
3 8
 /**
4 9
  * Created by leon on 11/6/17.
5 10
  */
6 11
 public class MainApplication {
7
-}
12
+
13
+    public static void main (String [ ] args) {
14
+        PetCompiler pet = new PetCompiler();
15
+        pet.petListCombo();
16
+        pet.returnUserInfo();
17
+    }
18
+
19
+    }
20
+
21
+

+ 36
- 0
src/main/java/io/zipcoder/polymorphism/Pet.java Visa fil

@@ -0,0 +1,36 @@
1
+package io.zipcoder.polymorphism;
2
+
3
+public class Pet {
4
+    private String petName;
5
+    private String kindOfPet;
6
+
7
+    public Pet() {
8
+        setPetName(petName);
9
+        setKindOfPet(kindOfPet);
10
+
11
+    }
12
+
13
+    public Pet(String kindOfPet, String petName) {
14
+    }
15
+
16
+    public Object speak() {
17
+        return "Blurrrrrghhhhh";
18
+    }
19
+
20
+    public void setPetName (String petName){
21
+        this.petName = petName;
22
+        }
23
+
24
+        public void setKindOfPet (String kindOfPet){
25
+            this.kindOfPet = kindOfPet;
26
+        }
27
+
28
+        public String getPetName() {
29
+            return petName;
30
+        }
31
+        public String getKindOfPet() {
32
+            return kindOfPet;
33
+        }
34
+    }
35
+
36
+

+ 56
- 0
src/main/java/io/zipcoder/polymorphism/PetCompiler.java Visa fil

@@ -0,0 +1,56 @@
1
+package io.zipcoder.polymorphism;
2
+
3
+import java.util.ArrayList;
4
+import java.util.Scanner;
5
+
6
+
7
+public class PetCompiler {
8
+    ArrayList<Pet> petList = new ArrayList<>();
9
+    String petName;
10
+    String kindOfPet;
11
+
12
+    public void petListCombo() {
13
+        System.out.print("How many pets do you have?");
14
+        Scanner in = new Scanner(System.in);
15
+        int petNumber = in.nextInt();
16
+        in.nextLine();
17
+
18
+        for (int i = 0; i < petNumber; i++) {
19
+            System.out.print("Oh yay! What kind do you have?");
20
+            kindOfPet = in.next();
21
+            //kindOfPet = userPetType;
22
+
23
+            System.out.print("What is the name of your pet?");
24
+            petName = in.next();
25
+            //petName = userPetName;
26
+
27
+            getUserPets();
28
+
29
+        }
30
+    }
31
+
32
+    public void getUserPets() {
33
+        if (kindOfPet.equalsIgnoreCase("dog")) {
34
+            Dog dog = new Dog(kindOfPet, petName);
35
+            petList.add(dog);
36
+        } else if (kindOfPet.equalsIgnoreCase("cat")) {
37
+            Cat cat = new Cat(kindOfPet, petName);
38
+            petList.add(cat);
39
+        } else if (kindOfPet.equalsIgnoreCase("lizard")) {
40
+            Lizard lizard = new Lizard(kindOfPet, petName);
41
+            petList.add(lizard);
42
+        } else {
43
+            Pet pet = new Pet(kindOfPet, petName);
44
+            petList.add(pet);
45
+        }
46
+    }
47
+
48
+
49
+    public void returnUserInfo() {
50
+        for (int i = 0; i < petList.size(); i++) {
51
+            String name = petList.get(i).getPetName();
52
+            String speak = (String) petList.get(i).speak();
53
+            System.out.println(name + " says " + speak);
54
+        }
55
+    }
56
+}

+ 10
- 1
src/test/java/io/zipcoder/polymorphism/MainApplicationTest.java Visa fil

@@ -1,7 +1,16 @@
1 1
 package io.zipcoder.polymorphism;
2
+import org.junit.Assert;
3
+import org.junit.Test;
2 4
 
3 5
 /**
4 6
  * Created by leon on 11/6/17.
5 7
  */
6 8
 public class MainApplicationTest {
7
-}
9
+    }
10
+
11
+
12
+
13
+
14
+
15
+
16
+