In: Computer Science
You and your closest friends have decided to go to see a private screening of a movie at a local movie theater where you can be socially distant. Friends will be added to the queue after their names are input by the user. Because of current restrictions, the queue (line) for tickets can only have a capacity of 7 people.
Write a Java program using a Queue (of size 7) that will display a menu:
1) add person to line
2) remove person from line
When (1) is chosen the user will be prompted to input the friend's name. Note that you must not add another person to the queue until there is an available space for them (you will need to check if the queue is full before adding people.) If there are already 7 people in the queue the program will output that "the line is full please try again later."
When (2) is chosen the person at the head/front of the queue is removed.
After each person is added or removed from the queue, output the contents of the queue and the name of the person at the head of the queue. The program will end after the removal of a person leaves the queue empty.
MUST BE JAVA. please include menu for queue operations.
Here is the completed code for this problem. Comments are included, go through it, learn how things work and let me know if you have any doubts or if you need anything to change. If you are satisfied with the solution, please rate the answer. If not, PLEASE let me know before you rate, I’ll help you fix whatever issues. Thanks
//MovieQueue.java
import java.util.Scanner;
public class MovieQueue {
// a simple class to represent a single node in the queue
private class Node {
String name;
Node next;
}
// attributes to first and last nodes
private Node front, tail;
// current size
private int size;
// constructor
public MovieQueue() {
front = tail = null;
size = 0;
}
// method to add a name to the queue
public void add(String name) {
// creating a node and setting name
Node n = new Node();
n.name = name;
// if front is null, adding n as both front and tail, else appending to
// tail and appending tail
if (front == null) {
front = tail = n;
} else {
tail.next = n;
tail = n;
}
// updating size
size++;
}
// method to remove and return person at front
public String remove() {
// returning null if queue is empty
if (size == 0) {
return null;
} else {
// otherwise removing front node, updating tail if needed, updating
// size, returning removed value
String name = front.name;
front = front.next;
if (front == null) {
tail = null;
}
size--;
return name;
}
}
// method to print queue contents
public void print() {
System.out.print("Queue: |");
// displaying queue elements from front to tail
for (Node n = front; n != null; n = n.next) {
System.out.print(n.name);
// appending a comma and space if there are more people
if (n.next != null) {
System.out.print(", ");
}
}
// displaying the name of person at the front, or 'No one' if queue is
// empty
System.out.println("|\nPerson at the front: "
+ ((front == null) ? "No one" : front.name));
}
// returns the size
public int getSize() {
return size;
}
// main method
public static void main(String[] args) {
// creating a queue
MovieQueue queue = new MovieQueue();
int CAPACITY = 7; // to indicate queue capacity
Scanner sc = new Scanner(System.in);
boolean done = false;
// looping until done is true
while (!done) {
// displaying menu and reading choice
System.out.println("\n1. add person to line");
System.out.println("2. remove person from line");
System.out.print("Your choice: ");
int ch = Integer.parseInt(sc.nextLine());
// finding choice
if (ch == 1) {
// if queue is full, displayng error
if (queue.getSize() == CAPACITY) {
System.out
.println("the line is full please try again later.");
} else {
// otherwise reading name of person, adding to queue
System.out.print("Enter name of person: ");
String name = sc.nextLine();
queue.add(name);
// printing queue
queue.print();
}
} else if (ch == 2) {
// displaying error if no one is added to the queue yet
if (queue.getSize() == 0) {
System.out.println("Queue is empty! Add some people first");
} else {
// else removing and displaying person at front
System.out.println(queue.remove()
+ " is removed from the line");
// displaying queue
queue.print();
// if queue is now empty, setting done to true
if (queue.getSize() == 0) {
System.out.println("Program is ending...");
done = true;
}
}
}
}
}
}
/*OUTPUT*/
1. add person to line
2. remove person from line
Your choice: 1
Enter name of person: Alice
Queue: |Alice|
Person at the front: Alice
1. add person to line
2. remove person from line
Your choice: 1
Enter name of person: Bob
Queue: |Alice, Bob|
Person at the front: Alice
1. add person to line
2. remove person from line
Your choice: 1
Enter name of person: Cait
Queue: |Alice, Bob, Cait|
Person at the front: Alice
1. add person to line
2. remove person from line
Your choice: 2
Alice is removed from the line
Queue: |Bob, Cait|
Person at the front: Bob
1. add person to line
2. remove person from line
Your choice: 1
Enter name of person: David
Queue: |Bob, Cait, David|
Person at the front: Bob
1. add person to line
2. remove person from line
Your choice: 1
Enter name of person: Erik
Queue: |Bob, Cait, David, Erik|
Person at the front: Bob
1. add person to line
2. remove person from line
Your choice: 1
Enter name of person: Ford
Queue: |Bob, Cait, David, Erik, Ford|
Person at the front: Bob
1. add person to line
2. remove person from line
Your choice: 1
Enter name of person: Gary
Queue: |Bob, Cait, David, Erik, Ford, Gary|
Person at the front: Bob
1. add person to line
2. remove person from line
Your choice: 1
Enter name of person: Harold
Queue: |Bob, Cait, David, Erik, Ford, Gary, Harold|
Person at the front: Bob
1. add person to line
2. remove person from line
Your choice: 1
the line is full please try again later.
1. add person to line
2. remove person from line
Your choice: 2
Bob is removed from the line
Queue: |Cait, David, Erik, Ford, Gary, Harold|
Person at the front: Cait
1. add person to line
2. remove person from line
Your choice: 2
Cait is removed from the line
Queue: |David, Erik, Ford, Gary, Harold|
Person at the front: David
1. add person to line
2. remove person from line
Your choice: 2
David is removed from the line
Queue: |Erik, Ford, Gary, Harold|
Person at the front: Erik
1. add person to line
2. remove person from line
Your choice: 2
Erik is removed from the line
Queue: |Ford, Gary, Harold|
Person at the front: Ford
1. add person to line
2. remove person from line
Your choice: 2
Ford is removed from the line
Queue: |Gary, Harold|
Person at the front: Gary
1. add person to line
2. remove person from line
Your choice: 2
Gary is removed from the line
Queue: |Harold|
Person at the front: Harold
1. add person to line
2. remove person from line
Your choice: 2
Harold is removed from the line
Queue: ||
Person at the front: No one
Program is ending...