I have to write a program to simulate a queue where students going to the library are given a token and requested to wait for their turns in a logical queue. The token number is automatically generated.
The program should display a menu that allows a user at any time to:
Insert a new student in the queue.
View the name and token no. of the student at the front of the queue.
Delete a student from the queue.
Find out how many students there are in the queue.
There should also be an option to exit the program.
I have been able to implement option 1, 2 and 4. Since it hasn't been mentioned as to how the student should be deleted, I suppose the deletion is to be done either by entering the name of the student or the token number but I can't figure out how to do it.
Here is what I have done so far:
Student.java Class
package queues;
import java.util.Random;
public class Student {
private String name;
private int tnum;
public Student(String name, int tnum){
this.name=name;
this.tnum=tnum;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getTnum() {
return tnum;
}
public void setTnum(int tnum) {
this.tnum = tnum;
}
public String toString(){
return "Student name: "+ name+ " Token num: "+tnum;
}
}
Student_Main.java Class
package queues;
import java.util.LinkedList;
import java.util.Queue;
import java.util.Scanner;
public class Student_Main{
public static void main(String[] args) {
Scanner sc= new Scanner(System.in);
int opt;
int tno;
Student stdt= new Student("Sophia", 1);
Student stdt2= new Student("Amelia", 2);
Student stdt3= new Student("Karxlina", 4);
Student stdt4= new Student("Rachel", 3);
Queue<Student> stdtQ= new LinkedList<Student>();
stdtQ.add(stdt);
stdtQ.add(stdt2);
stdtQ.add(stdt3);
stdtQ.add(stdt4);
System.out.println(stdtQ);
while(true){
System.out.println("Please choose an option. ");
System.out.println();
System.out.println("To insert new student, enter 1.");
System.out.println();
System.out.println("To view the name and token number of a student in front, enter 2.");
System.out.println();
System.out.println("To delete a student, enter 3.");
System.out.println();
System.out.println("To find out the number of students, enter 4.");
System.out.println();
System.out.println("To exit the system, enter 5.");
opt= sc.nextInt();
if(opt==1){
System.out.println("Enter student's name and token number.");
stdtQ.add(new Student(sc.next(), sc.nextInt()));
System.out.println("New queue: ");
System.out.println(stdtQ);
}
if(opt==2){
System.out.println(stdtQ.peek());
}
if(opt==3){
}
if(opt==4){
System.out.println("Number of students in the queue is: "+ stdtQ.size());
System.out.println(stdtQ);
}
if(opt==5){
System.exit(0);
}
}
}
}