Bladeren bron

Completed lab, wrote and passed all of the test.

William Brown 6 jaren geleden
bovenliggende
commit
a51e04cfe2

+ 9
- 0
pom.xml Bestand weergeven

@@ -8,5 +8,14 @@
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>
11 19
 
20
+    </dependencies>
12 21
 </project>

+ 13
- 0
src/main/java/io/zipcoder/polymorphism/Cat.java Bestand weergeven

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

+ 74
- 0
src/main/java/io/zipcoder/polymorphism/Console.java Bestand weergeven

@@ -0,0 +1,74 @@
1
+package io.zipcoder.polymorphism;
2
+
3
+import java.util.ArrayList;
4
+import java.util.InputMismatchException;
5
+import java.util.Scanner;
6
+
7
+public class Console {
8
+
9
+    static ArrayList<Pet> pets = new ArrayList<Pet>();
10
+
11
+    public static void getInformation(){
12
+        String type = getPetType();
13
+        String name = getName(type);
14
+        int age = getAge(name);
15
+        createNewPet(type, name, age);
16
+    }
17
+
18
+    public static int getAmountOfPets(){
19
+        Scanner scanner = new Scanner(System.in);
20
+        System.out.println("How many pets do you have?");
21
+        try{
22
+            int numOfPets = scanner.nextInt();
23
+            return numOfPets;
24
+        }catch (InputMismatchException E){
25
+            System.out.println("Not a number, try again.");
26
+            getAmountOfPets();
27
+            return 0;
28
+        }
29
+    }
30
+
31
+    public static String getPetType(){
32
+        Scanner scanner = new Scanner(System.in);
33
+        System.out.println("What kind of pet?");
34
+        String petType = scanner.nextLine();
35
+        return petType;
36
+    }
37
+
38
+    public static int getAge(String name){
39
+        Scanner scanner = new Scanner(System.in);
40
+        System.out.println("How old is " + name + "?");
41
+        int petAge = scanner.nextInt();
42
+        return petAge;
43
+    }
44
+
45
+    public static String getName(String type){
46
+        Scanner scanner = new Scanner(System.in);
47
+        System.out.println("Whats your " + type + "'s name?");
48
+        String petName = scanner.nextLine();
49
+        return petName;
50
+    }
51
+
52
+    public static void petForLoop(int numOfPets){
53
+        for(int i = 0; i < numOfPets; i++){
54
+            getInformation();
55
+        }
56
+    }
57
+
58
+    public static void createNewPet(String petType, String name, int age){
59
+        if(petType.equalsIgnoreCase("Dog")){
60
+            pets.add(new Dog(name, age));
61
+        }else if(petType.equalsIgnoreCase("Cat")){
62
+            pets.add(new Cat(name, age));
63
+        }else if(petType.equalsIgnoreCase("Horse")){
64
+            pets.add(new Horse(name, age));
65
+        }
66
+    }
67
+
68
+    public static void petList(){
69
+        for (Pet pet: pets) {
70
+            System.out.println("Your " + pet.getClass().getSimpleName() + " is named " + pet.getName() + " says " + pet.speak() + " and is " + pet.getAge() + " year old.");
71
+        }
72
+    }
73
+
74
+}

+ 13
- 0
src/main/java/io/zipcoder/polymorphism/Dog.java Bestand weergeven

@@ -0,0 +1,13 @@
1
+package io.zipcoder.polymorphism;
2
+
3
+public class Dog extends Pet{
4
+
5
+    public Dog(String name, int age) {
6
+        super(name, age);
7
+    }
8
+
9
+    @Override
10
+    public String speak(){
11
+        return "Bark!!";
12
+    }
13
+}

+ 13
- 0
src/main/java/io/zipcoder/polymorphism/Horse.java Bestand weergeven

@@ -0,0 +1,13 @@
1
+package io.zipcoder.polymorphism;
2
+
3
+public class Horse extends Pet{
4
+
5
+    public Horse(String name, int age) {
6
+        super(name, age);
7
+    }
8
+
9
+    @Override
10
+    public String speak() {
11
+        return "Nayyy";
12
+    }
13
+}

+ 9
- 2
src/main/java/io/zipcoder/polymorphism/MainApplication.java Bestand weergeven

@@ -1,7 +1,14 @@
1 1
 package io.zipcoder.polymorphism;
2 2
 
3 3
 /**
4
- * Created by leon on 11/6/17.
4
+ * Created by William on 10/24/2018.
5 5
  */
6
+
6 7
 public class MainApplication {
7
-}
8
+
9
+    public static void main(String[] args) {
10
+        Console.petForLoop(Console.getAmountOfPets());
11
+        Console.petList();
12
+    }
13
+
14
+}

+ 28
- 0
src/main/java/io/zipcoder/polymorphism/Pet.java Bestand weergeven

@@ -0,0 +1,28 @@
1
+package io.zipcoder.polymorphism;
2
+
3
+public abstract class Pet {
4
+
5
+    private String name;
6
+    private int age;
7
+
8
+    public Pet(){
9
+
10
+    }
11
+
12
+    public Pet(String name, int age) {
13
+        this.name = name;
14
+        this.age = age;
15
+    }
16
+
17
+    public String getName() {
18
+        return name;
19
+    }
20
+
21
+
22
+    public int getAge() {
23
+        return age;
24
+    }
25
+
26
+    public abstract String speak();
27
+
28
+}

+ 43
- 3
src/test/java/io/zipcoder/polymorphism/MainApplicationTest.java Bestand weergeven

@@ -1,7 +1,47 @@
1 1
 package io.zipcoder.polymorphism;
2
-
2
+import org.junit.*;
3 3
 /**
4
- * Created by leon on 11/6/17.
4
+ * Created by William on 10/24/2018.
5 5
  */
6 6
 public class MainApplicationTest {
7
-}
7
+
8
+    @Test
9
+    public void testDogSpeak(){
10
+        // Given
11
+        Dog dog = new Dog("Dogo", 12);
12
+        String expectedNumber = "Bark!!";
13
+
14
+        //When
15
+        String output = dog.speak();
16
+
17
+        //then
18
+        Assert.assertEquals(expectedNumber, output);
19
+    }
20
+
21
+    @Test
22
+    public void testCatSpeak(){
23
+        // Given
24
+        Cat cat = new Cat("Kuddles", 7);
25
+        String expectedNumber = "Meow";
26
+
27
+        //When
28
+        String output = cat.speak();
29
+
30
+        //then
31
+        Assert.assertEquals(expectedNumber, output);
32
+    }
33
+
34
+    @Test
35
+    public void testHorseSpeak(){
36
+        // Given
37
+        Horse horse = new Horse("Bucky", 23);
38
+        String expectedNumber = "Nayyy";
39
+
40
+        //When
41
+        String output = horse.speak();
42
+
43
+        //then
44
+        Assert.assertEquals(expectedNumber, output);
45
+    }
46
+
47
+}