-
Notifications
You must be signed in to change notification settings - Fork 0
/
ProblemSet3A.java
198 lines (162 loc) · 4.97 KB
/
ProblemSet3A.java
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
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
package ProblemSets;
import java.util.ArrayList;
import java.util.List;
import java.util.Objects;
public class ProblemSet3A {
public static void main(String[] args) {
Cat1 neko = new Cat1("Neko");
Dog1 fido = new Dog1("Fido");
Cow1 gyudon = new Cow1("Gyudon");
System.out.println(neko.getName());
System.out.println(fido.getName());
System.out.println(gyudon.getName());
System.out.println(neko.makeSound());
System.out.println(fido.makeSound());
System.out.println(gyudon.makeSound());
}
}
abstract class Animal1 {
private String name;
Animal1(String name){
this.name = name;
}
public String getName(){
return name;
}
public abstract String makeSound();
// TODO 1 : add toString()
@Override
public String toString(){
String name = getName();
return name;
}
// TODO 2 : add equals()
@Override
public boolean equals(Object o) {
if (this == o) return true;
if (o == null || getClass() != o.getClass()) return false;
Animal1 animal = (Animal1) o;
return Objects.equals(name, animal.name);
}
// TODO 3 : add hashCode()
@Override
public int hashCode() {
return Objects.hash(name);
}
}
// TODO 4 : Create 3 concrete classes Cat, Cow & Dog
class Cat1 extends Animal1 {
public Cat1(String name){
super(name);
}
@Override
public String getName(){
return "Cat:" + super.getName();
}
@Override
public String makeSound(){
return getName() + " says Meow";
}
}
class Cow1 extends Animal1 {
public Cow1(String name){
super(name);
}
@Override
public String getName() {
return "Cow:" + super.getName();
}
@Override
public String makeSound(){
return getName() + " says Moo Moo";
}
}
class Dog1 extends Animal1 {
public Dog1(String name){
super(name);
}
@Override
public String getName() {
return "Dog:" + super.getName();
}
@Override
public String makeSound(){
return getName() + " says Woof";
}
}
// Requirement 2
interface AnimalFactory {
Animal1 createAnimal(String type, String name);
}
// TODO 5 : Create a class FarmFactory which implements AnimalFactory
class FarmFactory implements AnimalFactory {
@Override
public Animal1 createAnimal(String type, String name) {
if (type.equals("Cat")) {
return new Cat1(name);
}
else if (type.equals("Dog")) {
return new Dog1(name);
}
else if (type.equals("Cow")) {
return new Cow1(name);
}
return null;
}
}
// Requirement 3
class Zoo {
private AnimalFactory animalFactory;
private List<Animal1> animal1List;
Zoo(AnimalFactory animalFactory) {
this.animalFactory = animalFactory;
animal1List = new ArrayList<>();
}
// TODO 7 : Invoke the createAnimal method on the abstract animalFactory instance variable
// which returns an Animal object based on inputs type and name
public void addAnimal(String type, String name){
Animal1 animal1 = animalFactory.createAnimal(type, name);
if (animal1 != null) {
animal1List.add(animal1);
}
}
public String performConcert(){
StringBuilder result = new StringBuilder();
for (Animal1 animal1 : animal1List) {
result.append(animal1.makeSound()).append("*");
}
return result.toString();
}
// TODO 6 : Override the toString() to return the toString() of animalList
@Override
public String toString(){
return animal1List.toString();
}
}
// Test Case for Zoo.
class TestZoo {
public static void main(String[] args) {
Animal1 cat1 = new Cat1("ichika");
System.out.println(cat1.getName()); // Cat:ichika
System.out.println(cat1.makeSound()); // Cat:ichika says Meow
Animal1 cat2 = new Cat1("nino");
Animal1 cat3 = new Cat1("ichika");
System.out.println(cat1.equals(cat3)); // true
System.out.println(cat2.equals(cat3)); // false
Animal1 dog1 = new Dog1("Fido");
System.out.println(dog1.getName()); // Dog:Fido
System.out.println(dog1.makeSound()); // Dog:Fido says Woof
AnimalFactory animalFactory = new FarmFactory();
Animal1 animal1 = animalFactory.createAnimal("Cat", "miku");
Animal1 animal2 = animalFactory.createAnimal("Dog", "hachiko");
System.out.println(animal1.getName()); // Cat:miku
System.out.println(animal2.getName()); // Dog:hachiko
Zoo zoo = new Zoo(new FarmFactory());
System.out.println(zoo); // []
zoo.addAnimal("Cat", "Kuroneko");
zoo.addAnimal("Dog", "Bond");
zoo.addAnimal("Cow", "Gyudon");
System.out.println(zoo.performConcert()); // Cat:Kuoneko says Meow*Dog:Bond says Woof*Cow:Gyudon says Moo Moo*
System.out.println(zoo);
}
}