-
Notifications
You must be signed in to change notification settings - Fork 95
Expand file tree
/
Copy pathAnimalShelter.java
More file actions
91 lines (78 loc) · 1.87 KB
/
AnimalShelter.java
File metadata and controls
91 lines (78 loc) · 1.87 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
83
84
85
86
87
88
89
90
91
package chapter03StacksAndQueues;
import java.util.LinkedList;
/**
* Problem: 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 the data structures to maintain this system and implement
* operations such as enqueue, dequeueAny, dequeueDog, and dequeueCat. You may
* use the built in Linked list data structure.
*
*/
abstract class Animal {
private int order;
protected String name;
public Animal(String name) {
this.name = name;
}
public void setOrder(int order) {
this.order = order;
}
public int getOrder() {
return order;
}
public boolean isOlderThan(Animal a) {
return this.order < a.getOrder();
}
}
class Dog extends Animal {
public Dog(String name) {
super(name);
}
}
class Cat extends Animal {
public Cat(String name) {
super(name);
}
}
public class AnimalShelter {
LinkedList<Dog> dogs = new LinkedList<>();
LinkedList<Cat> cats = new LinkedList<>();
private int order = 0;
public void enqueue(Animal a) {
a.setOrder(order);
order++;
if (a instanceof Dog) {
dogs.addLast((Dog) a);
}
if (a instanceof Cat) {
cats.addLast((Cat) a);
}
}
public Animal dequeueAny() {
if (dogs.size() == 0 && cats.size() == 0) {
return null;
}
if (dogs.size() == 0) {
return dequeueCats();
}
if (cats.size() == 0) {
return dequeueDogs();
}
Dog dog = dogs.peek();
Cat cat = cats.peek();
if (dog.isOlderThan(cat)) {
return dequeueDogs();
} else {
return dequeueCats();
}
}
public Animal dequeueDogs() {
return dogs.poll();
}
public Animal dequeueCats() {
return cats.poll();
}
}