-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathAnimalShelter.java
86 lines (71 loc) · 1.73 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
/*
Animal Shelter: 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 Linkedlist data structure.
*/
import java.util.LinkedList;
class Animal{
String name;
int num;
Animal(String name){
this.name = name;
}
boolean isOlderAnimal(Animal a){
return this.num > a.num;
}
}
class Dog extends Animal{
Dog(String name){
super(name);
}
}
class Cat extends Animal{
Cat(String name){
super(name);
}
}
class AnimalShelterQueue{
LinkedList<Dog> dogs;
LinkedList<Cat> cats;
int insertionOrder;
AnimalShelterQueue(){
dogs = new LinkedList<>();
cats = new LinkedList<>();
insertionOrder = 0;
}
public void enQueue(Animal animal){
if(animal instanceof Dog){
dogs.addLast((Dog)animal);
}
if(animal instanceof Cat){
cats.addLast((Cat)animal);
}
animal.num = insertionOrder;
insertionOrder++;
}
public Animal deQueueDogs(){
return dogs.poll();
}
public Animal deQueueCats(){
return cats.poll();
}
public Animal deQueueAny(){
if(cats.size() == 0){
return dogs.poll();
}
if(dogs.size() == 0){
return cats.poll();
}
Dog d = dogs.peek();
Cat c = cats.peek();
if(d.isOlderAnimal(c)){
return cats.poll();
}
else{
return dogs.poll();
}
}
}