-
Notifications
You must be signed in to change notification settings - Fork 64
Expand file tree
/
Copy pathApplication.java
More file actions
83 lines (65 loc) · 2.1 KB
/
Application.java
File metadata and controls
83 lines (65 loc) · 2.1 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
package io.zipcoder;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Scanner;
public class Application {
private ArrayList<Pet> pets = new ArrayList<>();
private Scanner scanner = new Scanner(System.in);
public static void main(String[] args) {
Application application = new Application();
application.start();
}
public void start() {
System.out.println("How many pets do you have?");
String numberOfPetsString = scanner.nextLine();
int numberOfPets = Integer.parseInt(numberOfPetsString);
for(int i=0;i<numberOfPets; i++) {
System.out.println("What is the type of pet?");
String typeOfPet = scanner.nextLine();
System.out.println("What is the name of the pet?");
String petName = scanner.nextLine();
Pet pet = createPet(typeOfPet, petName);
this.add(pet);
}
printPets();
}
public void printPets() {
for(Pet pet : pets) {
String petName = pet.getName();
String petType = pet.getClass().getSimpleName();
System.out.println("Pet name = " + petName);
System.out.println("Pet type = " + petType);
}
}
//method stubs do not return logic
public Pet[] getPets() {
int size = pets.size();
Pet[] petArray = new Pet[size];
for(int i=0; i<pets.size(); i++) {
Pet currentPet = pets.get(i);
petArray[i] = currentPet;
}
return petArray;
}
public void add(Pet pet) {
pets.add(pet);
}
public Pet createPet(String petType, String petName) {
Pet pet = null;
switch (petType.toLowerCase()) {
case "bird":
pet = new Bird(petName);
break;
case "dog":
pet = new Dog(petName);
break;
case "snake":
pet = new Snake(petName);
break;
case "cat":
pet = new Cat(petName);
break;
}
return pet;
}
}