-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathAnimalShelter.java
115 lines (100 loc) · 2.17 KB
/
AnimalShelter.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
import java.util.LinkedList;
/*
* Animal shelter holds only cats and dogs. Operates on FIFO basis.
* People can adopt oldest overall animal, or choose oldest cat/dog.
* Create data structure to maintain this system using built in
* LinkedList data structure.
*/
public class AnimalShelter {
public static class Animal {
private boolean dog;
private int age;
/*
* d == true if Animal is a dog,
* false if Animal is a cat
*/
public Animal(boolean d) {
dog = d;
}
public boolean isDog() {
return dog;
}
public void setAge(int x) {
age = x;
}
public int getAge() {
return age;
}
}
private LinkedList<Animal> dogs;
private LinkedList<Animal> cats;
private int count;
public AnimalShelter() {
dogs = new LinkedList<Animal>();
cats = new LinkedList<Animal>();
count = 0;
}
public void enque(Animal a) {
count++;
a.setAge(count);
if (a.isDog()) {
dogs.add(a);
} else {
cats.add(a);
}
}
public Animal dequeueDog() {
if (dogs.size() != 0) {
return dogs.removeFirst();
} else {
return null;
}
}
public Animal dequeueCat() {
if (cats.size() != 0) {
return cats.removeFirst();
} else {
return null;
}
}
public Animal dequeueAny() {
if (dogs.size() == 0 && cats.size() != 0) {
return dequeueCat();
} else if (cats.size() == 0 && dogs.size() != 0) {
return dequeueDog();
} else if (cats.size() == 0 && dogs.size() == 0) {
return null;
}
Animal d = dogs.peekFirst();
Animal c = cats.peekFirst();
if (d.getAge() < c.getAge()) {
return dequeueDog();
} else {
return dequeueCat();
}
}
public static void main(String[] args) {
AnimalShelter test = new AnimalShelter();
for (int i = 0; i < 10; i++) {
Animal a;
if (i%3 == 0) {
a = new Animal(true);
} else {
a = new Animal(false);
}
test.enque(a);
}
System.out.println();
Animal x;
// Selectively comment-out to test as needed
while ((x = test.dequeueCat()) != null) {
System.out.println(x.getAge());
}
while ((x = test.dequeueDog()) != null) {
System.out.println(x.getAge());
}
while ((x = test.dequeueAny()) != null) {
System.out.println(x.getAge());
}
}
}