-
Notifications
You must be signed in to change notification settings - Fork 33
/
Problem_06.java
193 lines (169 loc) · 3.71 KB
/
Problem_06.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
/**
* Cracking-The-Coding-Interview
* Problem_06.java
*/
package com.deepak.ctci.Ch03_Stacks_And_Queues;
import java.util.LinkedList;
/**
* <br> Problem Statement :
*
* An animal shelter, which holds only dogs and cats, operates
* on a strictly "first in, first out" basis. People must adopt
* either the "oldest" (based on arrival time) of all animals at
* the shelter, or they can select whether they would prefer a dog
* or a cat (and will receive the oldest animal of that type). They
* cannot select which specific animal they would like. Create a data
* structure to maintain this system and implement operations such as
* enqueue, dequeueAny, dequeueDog and dequeueCat. You may use the
* built in linked list data structure.
*
* </br>
*
* @author Deepak
*/
public class Problem_06 {
/**
* Class for Animal Shelter
*
* @author Deepak
*/
public static class AnimalShelter {
/* Two queues created, one each for dog and cat */
private static LinkedList<Dog> dogs = new LinkedList<>();
private static LinkedList<Cat> cats = new LinkedList<>();
/* Counter to assign order for each Animal */
private static int order = 0;
/**
* Method to enqueue the animal
*
* @param animal
*/
public void enqueue(Animal animal) {
/* If Animal is present, set order and increase the order size */
if (animal != null) {
animal.setOrder(order);
order++;
/* Check what type of Animal it is, and
* add to that specific queue */
if (animal instanceof Dog) {
dogs.addLast((Dog) animal);
} else if (animal instanceof Cat) {
cats.addLast((Cat) animal);
}
}
}
/**
* Method to dequeue any animal
*
* @return {@link Animal}
*/
public Animal dequeueAny() {
/* If there are no dogs, dequeue cats
* and vice versa */
if (dogs.size() == 0) {
dequeueCat();
} else if (cats.size() == 0) {
dequeueDog();
}
/* Find the first dog and cat in the queue */
Dog dog = dogs.peek();
Cat cat = cats.peek();
/* Whichever is older entry, return that */
if (dog.isOlderThen(cat)) {
return dogs.poll();
} else {
return cats.poll();
}
}
/**
* Method to dequeue dog
*
* @return {@link Dog}
*/
public Dog dequeueDog() {
return dogs.poll();
}
/**
* Method to dequeue cat
*
* @return {@link Cat}
*/
public Cat dequeueCat() {
return cats.poll();
}
/**
* Method to get the size of Animal shelter
*
* @return {@link int}
*/
public int size() {
return dogs.size() + cats.size();
}
/**
* Abstract class Animal
*
* @author Deepak
*/
public static abstract class Animal {
/* Order of the animal and name */
private int order;
protected String name;
/**
* Constructor
*
* @param name
*/
public Animal(String name) {
this.name = name;
}
public int getOrder() {
return order;
}
public void setOrder(int order) {
this.order = order;
}
/**
* Compare two animals and find out which is older
*/
public boolean isOlderThen(Animal animal) {
return this.order < animal.getOrder();
}
}
/**
* Class Cat which extends Animal
*
* @author Deepak
*/
public static class Cat extends Animal {
/**
* Constructor to add a cat
*
* @param name
*/
public Cat(String name) {
super(name);
}
public String name() {
return "Cat : " + name;
}
}
/**
* Class Dog which extends Animal
*
* @author Deepak
*/
public static class Dog extends Animal {
/**
* Constructor to add a Dog
*
* @param name
*/
public Dog(String name) {
super(name);
}
public String name() {
return "Dog : " + name;
}
}
}
}