// D. Scott // CCI - Problem 3.6 // Animal shelter queuing system using LinkedList import java.util.LinkedList; class Animal { String type; int id; public Animal(String t, int id) { type = t; this.id = id; } } class AdoptionQueue { LinkedList all; LinkedList dogs; LinkedList cats; public AdoptionQueue() { all = new LinkedList(); dogs = new LinkedList() cats = new LinkedList() } void enqueue(Animal a) { all.addLast(a); if (a.type == "dog") dogs.addLast(a); if(a.type == "cat") cats.addLast(a); System.out.println("Added " + a.type + " " + a.id + " to the adoption queue."); } Animal dequeueAny() { if (all.peek() == null) { System.out.println("\tNo animals available for adoption."); return; } Animal a = all.removeFirst(); dogs.remove(a); cats.remove(a); return a; } Animal dequeueDog() { if (dogs.peek() == null) { System.out.println("\tNo dogs available for adoption."); return; } Animal d = dogs.removeFirst(); all.remove(d); return d; } Animal dequeueCat() { if (cats.peek() == null) { System.out.println("\tNo cats available for adoption."); return; } Animal c = cats.removeFirst(); all.remove(c); return c; } } public static void main (String[] args) { AdoptionQueue a_queue = new AdoptionQueue(); Animal a1 = new Animal("dog",4); Animal a2 = new Animal("dog",32); Animal a3 = new Animal("cat",18); Animal a4 = new Animal("dog",26); Animal a5 = new Animal("cat",9); Animal a6 = new Animal("cat",21); Animal a7 = new Animal("dog",14); a_queue.enqueue(a1); a_queue.enqueue(a2); a_queue.enqueue(a3); a_queue.enqueue(a4); a_queue.enqueue(a5); a_queue.enqueue(a6); a_queue.enqueue(a7); System.out.println("\n Select cat:"); Animal cat1 = a_queue.dequeueCat(); System.out.println("\t" + cat1.type + " " + cat1.id + " selected for adoption."); System.out.println("\n Select any:"); Animal any1 = a_queue.dequeueAny(); System.out.println("\t" + any1.type + " " + any1.id + " selected for adoption."); System.out.println("\n Select any:"); any1 = a_queue.dequeueAny(); System.out.println("\t" + any1.type + " " + any1.id + " selected for adoption."); System.out.println("\n Select any:"); any1 = a_queue.dequeueAny(); System.out.println("\t" + any1.type + " " + any1.id + " selected for adoption."); System.out.println("\n Select dog:"); Animal dog1 = a_queue.dequeueDog(); System.out.println("\t" + dog1.type + " " + dog1.id + " selected for adoption."); System.out.println("\n Select any:"); any1 = a_queue.dequeueAny(); System.out.println("\t" + any1.type + " " + any1.id + " selected for adoption."); System.out.println("\n Select any:"); any1 = a_queue.dequeueAny(); System.out.println("\t" + any1.type + " " + any1.id + " selected for adoption."); System.out.println("\n Select dog:"); dog1 = a_queue.dequeueDog(); System.out.println("\n Select cat:"); cat1 = a_queue.dequeueCat(); System.out.println("\n Select any:"); any1 = a_queue.dequeueAny(); }