Java:如何添加或创建带参数的新对象并将其添加到队列中?

时间:2016-01-07 23:07:57

标签: java constructor queue

我正在编写一个程序,让学生前往特定部门或教职员工的收银员获得一个自动生成的代号,然后他们排队等待轮到他们进入。该程序应允许用户通过在系统中添加其名称和令牌编号以及其他功能,将新学生插入队列中。

我不知道如何将新学生添加到队列中。

这是我到目前为止所得到的:

学生班

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;
    }

}

主要课程

package queues;

import java.util.LinkedList;
import java.util.Queue;
import java.util.Random;
import java.util.Scanner;

public class Student_Main{


    public static void main(String[] args) {

    Scanner sc= new Scanner(System.in);

    int opt;

    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);

    System.out.println("Please choose an option. ");
    System.out.println("To insert new student, enter 1.");

    opt= sc.nextInt();

    if(opt==1){
        stdtQ.add(Student(sc.hasNext(), sc.nextInt())); /*this doesn't work of course*/
      }



    }


}

2 个答案:

答案 0 :(得分:2)

你应该写:

var productType = [];
productType["Banner"] = ["Grommets", "UV Protection", "Gloss Finish", "Matte Finish"];
productType["Mangets"] = ["Rounded Corners", "UV Protection", "Contour Cut"];
productType["Sticker"] = ["Contour Cut", "UV Protection"];
productType["Decal"] = ["Contour Cut", "UV Protection"];
productType["Yard Sign"] = ["Step Stakes", "UV Protection", "Double Sided", "Rounded Corners", "Contour Cut"];
productType["Digital Print"] = ["Grommets", "UV Protection", "Gloss Finish", "Matte Finish"];
productType["Business Signs"] = ["Double Sided", "Drilled Holes", "Rounded Corners", "Reflective"];

答案 1 :(得分:1)

队列通常以FIFO方式对元素进行排序。因此,如果您有兴趣使用令牌编号对队列进行排序,则应检查优先级队列。

通过调用cmd.exe /k start "MyProgram" /WAIT /RealTime "C:\Users\Me\bin\Release\MyProgram.exe" " option" 方法,您已经在add队列中添加了新元素。如果你想让其他学生加入该队列,你必须先创建对象(例如通过从输入中获取值)并使用stdtQ方法添加它。

您可以打印队列中的所有学生:

add