Parcourir la source

completed lab

Xcuello il y a 6 ans
Parent
révision
d87cb3be4f

+ 9
- 0
src/main/java/io/zipcoder/polymorphism/Cat.java Voir le fichier

@@ -0,0 +1,9 @@
1
+package io.zipcoder.polymorphism;
2
+
3
+public class Cat extends Pet {
4
+
5
+    public String speak() {
6
+
7
+        return "meow";
8
+    }
9
+}

+ 9
- 0
src/main/java/io/zipcoder/polymorphism/Dog.java Voir le fichier

@@ -0,0 +1,9 @@
1
+package io.zipcoder.polymorphism;
2
+
3
+public class Dog extends Pet {
4
+
5
+    public String speak() {
6
+
7
+        return "woof";
8
+    }
9
+}

+ 8
- 0
src/main/java/io/zipcoder/polymorphism/Lion.java Voir le fichier

@@ -0,0 +1,8 @@
1
+package io.zipcoder.polymorphism;
2
+
3
+public class Lion extends Pet {
4
+    public String speak() {
5
+
6
+        return "roar";
7
+    }
8
+}

+ 25
- 0
src/main/java/io/zipcoder/polymorphism/MainApplication.java Voir le fichier

@@ -1,7 +1,32 @@
1 1
 package io.zipcoder.polymorphism;
2 2
 
3
+import java.sql.SQLOutput;
4
+import java.util.List;
5
+import java.util.Scanner;
6
+
3 7
 /**
4 8
  * Created by leon on 11/6/17.
5 9
  */
6 10
 public class MainApplication {
11
+
12
+
13
+    public static void main (String[] args) {
14
+
15
+        UserInput ui = new UserInput(); // object instantiation
16
+
17
+        ui.setNumOfPets();
18
+        ui.setPetNames();
19
+        System.out.println(ui.getListOfPets());
20
+        List<Pet> pets = ui.getListOfPets();
21
+
22
+        for (Pet p : pets) {
23
+            p.speak();
24
+            p.getNameOfPet();
25
+
26
+            System.out.println(p.getNameOfPet() + ": " + p.speak());
27
+
28
+
29
+        }
30
+
31
+    }
7 32
 }

+ 17
- 0
src/main/java/io/zipcoder/polymorphism/Pet.java Voir le fichier

@@ -0,0 +1,17 @@
1
+package io.zipcoder.polymorphism;
2
+
3
+public abstract class Pet {
4
+
5
+    private String nameOfPet;
6
+
7
+    public String getNameOfPet() {
8
+        return nameOfPet;
9
+    }
10
+
11
+    public void setNameOfPet(String nameOfPet) {
12
+        this.nameOfPet = nameOfPet;
13
+    }
14
+
15
+    public abstract String speak();
16
+
17
+}

+ 67
- 0
src/main/java/io/zipcoder/polymorphism/UserInput.java Voir le fichier

@@ -0,0 +1,67 @@
1
+package io.zipcoder.polymorphism;
2
+
3
+import java.util.ArrayList;
4
+import java.util.List;
5
+import java.util.Scanner;
6
+
7
+public class UserInput {
8
+
9
+    List<Pet> listOfPets;
10
+    Scanner scanner = new Scanner(System.in);
11
+
12
+    private int numOfPets;
13
+
14
+    public void setNumOfPets() {
15
+
16
+        System.out.println("How many pets do you have?");
17
+        this.numOfPets = scanner.nextInt();
18
+    }
19
+
20
+    public int getNumOfPets() {
21
+
22
+        return numOfPets;
23
+
24
+    }
25
+
26
+    public void setPetNames() {
27
+
28
+        Pet pet;
29
+
30
+        listOfPets = new ArrayList<Pet>();
31
+
32
+        for (int i = 1; i <= numOfPets; i++) {
33
+            System.out.println("What kind of pet is it?");
34
+            String petType = scanner.next();
35
+
36
+            if (petType.toLowerCase().equals("dog")) {
37
+                pet = new Dog();
38
+
39
+                System.out.println("what is it's name?");
40
+                pet.setNameOfPet(scanner.next());
41
+                listOfPets.add(pet);
42
+            } else if(petType.toLowerCase().equals("cat")) {
43
+                pet = new Cat();
44
+
45
+                System.out.println("what is it's name?");
46
+                pet.setNameOfPet(scanner.next());
47
+                listOfPets.add(pet);
48
+
49
+            } else if (petType.toLowerCase().equals("lion")) {
50
+                pet = new Lion();
51
+                System.out.println("what is it's name?");
52
+                pet.setNameOfPet(scanner.next());
53
+                listOfPets.add(pet);
54
+            }
55
+        }
56
+    }
57
+
58
+//        public Pet petMaker (String petName){
59
+//
60
+//            return null;
61
+//        }
62
+
63
+        public List<Pet> getListOfPets () {
64
+            return listOfPets;
65
+        }
66
+
67
+    }