1

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:

  1. Insert a new student in the queue.

  2. View the name and token no. of the student at the front of the queue.

  3. Delete a student from the queue.

  4. 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);
            }


       }

    }
 }

2 Answers 2

1
if(opt==3){             
    System.out.println("Enter token num to delete");
    int to_num = sc.nextInt();
    Iterator<Student> itr = stdtQ.iterator();  

    while(itr.hasNext()){  
        Student obj = itr.next();
        if( obj.getTnum() == to_num)
        {
            stdtQ.remove(obj);
            break;
        }
    }       
 }
Sign up to request clarification or add additional context in comments.

Comments

0

If you wish to delete the Student by number (assuming you would have unique number of each student) from queue then you need to implement equals method in your student object like:

public boolean equals(Object student) {
     ....
     return this.tNum.equals(student.getTnum());
}

And if you wish to delete the student by number, you could do something like:

if(opt==3){
      //read number
      ((Collection)stdtQ).remove(new Student("", number));
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.