/
Program.cs
288 lines (266 loc) · 8.73 KB
/
Program.cs
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
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
// As an avid animal lover, you have started your very own collection of creatures in your home. You can use the code from the lesson as a starting point to have interfaces for walking and swimming animals, but you want to have several other kinds in your collection.
// This is the list of animals you want to own and care for.
// Parakeets
// Earthworms
// Terrapins
// Timber Rattlesnake
// Mice
// Ants
// Finches
// Betta Fish
// Copperhead snake
// Gerbils
// Each month, you clean out all of the habitats in a single day for efficiency. On that day, all animals need to be put into temporary containers. Each container will hold animals of similar similar, but different, types.
// Animals that dig and live in the ground
// Animals that move about on the ground
// Animals that swim in water
// Animals that fly above the ground
// Before you write any classes for the above animals, determine the common properties and behaviors that some of them share and define interfaces first.
// Once you believe you have a good set of interfaces, then start creating your specific animal classes and have them implement the appropriate interface.
// Then define classes to represent the containers that will hold various animals. Each container class should have a single property - a list to hold animal instances.
// Lastly, in Main() create one (or more if you like) instances of each type of animal and each container. Then add the animals to their corresponding container.
using System;
using System.Collections.Generic;
namespace diggers_and_fliers
{
// Interfaces
public interface IDigger
{
string Name {get; set;}
void dig();
}
public interface IWalker
{
string Name {get; set;}
void walk();
void run();
}
public interface ISwimmer
{
string Name {get; set;}
void swim();
int MaxDepth {get;}
}
public interface IFlier
{
string Name {get; set;}
void fly();
void land();
}
// Animals
public class Parakeet : IFlier
{
public string Name {get; set;}
public Parakeet(string name) {
Name = name;
}
public void fly()
{
Console.WriteLine("The parakeet is flying");
}
public void land()
{
Console.WriteLine("The parakeet has landed");
}
}
public class Earthworm : IDigger
{
public string Name {get; set;}
public Earthworm(string name) {
Name = name;
}
public void dig()
{
Console.WriteLine("The earthworm is digging");
}
}
public class Terrapin : IWalker, ISwimmer
{
public string Name {get; set;}
public Terrapin(string name) {
Name = name;
}
public void walk()
{
Console.WriteLine("The terrapin is walking");
}
public void run()
{
Console.WriteLine("The terrapin is running");
}
public void swim()
{
Console.WriteLine("The terrapin is swimming");
}
public int MaxDepth {get;} = 5;
}
public class TimberRattlesnake : IWalker
{
public string Name {get; set;}
public TimberRattlesnake(string name) {
Name = name;
}
public void walk()
{
Console.WriteLine("The timber rattlesnake is slithering slowly");
}
public void run()
{
Console.WriteLine("The timber rattlesnake is slithering quickly");
}
}
public class Mouse : IDigger, IWalker
{
public string Name {get; set;}
public Mouse(string name) {
Name = name;
}
public void dig()
{
Console.WriteLine("The mouse is burrowing");
}
public void walk()
{
Console.WriteLine("The mouse is walking");
}
public void run()
{
Console.WriteLine("The mouse is running");
}
}
public class Ant : IDigger, IWalker
{
public string Name {get; set;}
public Ant(string name) {
Name = name;
}
public void dig()
{
Console.WriteLine("The ant is tunneling");
}
public void walk()
{
Console.WriteLine("The ant is walking");
}
public void run()
{
Console.WriteLine("The ant is walking faster");
}
}
public class Finch : IFlier, IWalker
{
public string Name {get; set;}
public Finch(string name) {
Name = name;
}
public void fly()
{
Console.WriteLine("The finch is flying");
}
public void land()
{
Console.WriteLine("The finch has landed");
}
public void walk()
{
Console.WriteLine("The finch is hopping slowly");
}
public void run()
{
Console.WriteLine("The finch is hopping quickly");
}
}
public class Betta : ISwimmer
{
public string Name {get; set;}
public Betta(string name) {
Name = name;
}
public void swim()
{
Console.WriteLine("The betta is swimming");
}
public int MaxDepth {get;} = 10;
}
public class Copperhead : IWalker
{
public string Name {get; set;}
public Copperhead(string name) {
Name = name;
}
public void walk()
{
Console.WriteLine("The copperhead is slithering slowly");
}
public void run()
{
Console.WriteLine("The copperhead is slithering quickly");
}
}
public class Gerbil : IDigger, IWalker
{
public string Name {get; set;}
public Gerbil(string name) {
Name = name;
}
public void dig()
{
Console.WriteLine("The gerbil is burrowing");
}
public void walk()
{
Console.WriteLine("The gerbil is walking");
}
public void run()
{
Console.WriteLine("The gerbil is running");
}
}
// Containers
public class DiggerContainer
{
public List<IDigger> ContainedAnimals = new List<IDigger>();
}
public class WalkerContainer
{
public List<IWalker> ContainedAnimals = new List<IWalker>();
}
public class SwimmerContainer
{
public List<ISwimmer> ContainedAnimals = new List<ISwimmer>();
}
public class FlierContainer
{
public List<IFlier> ContainedAnimals = new List<IFlier>();
}
class Program
{
static void Main(string[] args)
{
// Create one of each container
DiggerContainer dirtBox = new DiggerContainer();
WalkerContainer fencedArea = new WalkerContainer();
SwimmerContainer aquarium = new SwimmerContainer();
FlierContainer aviary = new FlierContainer();
// Create one of each animal, add to appropriate container
aviary.ContainedAnimals.Add(new Parakeet("Simon the Parakeet"));
dirtBox.ContainedAnimals.Add(new Earthworm("Jorah the Earthworm"));
aquarium.ContainedAnimals.Add(new Terrapin("Calamy the Terrapin"));
fencedArea.ContainedAnimals.Add(new TimberRattlesnake("Broderick the Timber Rattlesnake"));
dirtBox.ContainedAnimals.Add(new Mouse("Sawyer the Mouse"));
dirtBox.ContainedAnimals.Add(new Ant("Finn the Ant"));
aviary.ContainedAnimals.Add(new Finch("Odin the Finch"));
aquarium.ContainedAnimals.Add(new Betta("Thor the Betta"));
fencedArea.ContainedAnimals.Add(new Copperhead("Martin the Copperhead"));
dirtBox.ContainedAnimals.Add(new Gerbil("Gertie the Gerbil"));
Console.WriteLine("Dirt Box contains:");
dirtBox.ContainedAnimals.ForEach(animal => Console.WriteLine($" - {animal.Name}"));
Console.WriteLine("Fenced Area contains:");
fencedArea.ContainedAnimals.ForEach(animal => Console.WriteLine($" - {animal.Name}"));
Console.WriteLine("Aquarium contains:");
aquarium.ContainedAnimals.ForEach(animal => Console.WriteLine($" - {animal.Name}"));
Console.WriteLine("Aviary contains:");
aviary.ContainedAnimals.ForEach(animal => Console.WriteLine($" - {animal.Name}"));
}
}
}