12.9k views
4 votes
Use the LinkedList class to simulate a queue, and use the add method to simulate the enqueue, and the remove method to simulate the dequeue method for a Queue. Remember to use FIFO. Create 3 queues, one called Animal Shelter, another called Cats, and another called Dogs. Use the following menu-driven application: 0. Add New Microchips 1. Donate a Cat 2. Donate a Dog 3. Adopt a Cat 4. Adopt a Dog 5. Adopt Oldest Pet 6. Exit

User Knock Yang
by
4.8k points

1 Answer

3 votes

Answer:

import java.util.*;

public class Q {

public static LinkedList<String> dogs = new LinkedList<String> ();

public static LinkedList<String> cats = new LinkedList<String> ();

public static LinkedList<String> animals = new LinkedList<String> ();

public static void enqueueCats(){

System.out.println("Enter name");

Scanner sc=new Scanner(System.in);

String name = sc.next();

cats.addLast(name);

animals.addLast(name);

}

public static void enqueueDogs(){

System.out.println("Enter name");

Scanner sc=new Scanner(System.in);

String name = sc.next();

dogs.addLast(name);

animals.addLast(name);

}

public static void removeFromQueue(char q,String name){

LinkedList<String> L = new LinkedList<String> ();

switch (q) {

case 'c':

L = cats;

break;

case 'd':

L = dogs;

break;

case 'a':

L = animals;

}

LinkedList<String> tmp = new LinkedList<String> ();

while (!L.isEmpty()){

if(!L.getFirst().equals(name)){

tmp.add(L.getFirst());

}

L.removeFirst();

}

while (!tmp.isEmpty()){

L.add(tmp.removeLast());

}

}

public static void dequeueCats(){

System.out.println("Enter name");

Scanner sc=new Scanner(System.in);

String name = sc.next();

removeFromQueue('c',name);

removeFromQueue('a',name);

}

public static void dequeueDogs(){

System.out.println("Enter name");

Scanner sc=new Scanner(System.in);

String name = sc.next();

removeFromQueue('d',name);

removeFromQueue('a',name);

}

public static void dequeueAnimals(){

System.out.println("Enter name");

Scanner sc=new Scanner(System.in);

String name = sc.next();

removeFromQueue('a',name);

removeFromQueue('d',name);

removeFromQueue('c',name);

}

public static void display(){

System.out.println("animals:");

for (String s : animals)

System.out.print(s);

System.out.println();

System.out.println("cats:");

for (String s : cats)

System.out.print(s);

System.out.println();

System.out.println("dogs:");

for (String s : dogs)

System.out.print(s);

System.out.println();

}

public static void display_choices(){

String [] choices = {"Donate a Cat","Donate a Dog","Adopt a Cat","Adopt a Dog","Adopt Oldest Pet","Exit"};

for (int i=0;i<choices.length ;i++ ) {

System.out.println((i+1)+"."+choices[i]);

}

}

public static void main(String[] args) {

Scanner sc=new Scanner(System.in);

while(true){

display_choices();

int choice=sc.nextInt();

switch (choice) {

case 1 :

enqueueCats();

display();

break;

case 2 :

enqueueDogs();

display();

break;

case 3 :

dequeueCats();

display();

break;

case 4 :

dequeueDogs();

display();

break;

case 5:

dequeueAnimals();

display();

case 6 :

System.exit(0);

}

}

}

}

User Thomas Morris
by
3.9k points