尝试将值分配给队列对象时出错[Java OOP]

时间:2018-04-02 14:56:55

标签: java arrays oop queue

所以我有这项任务,旨在模拟在登机口等候的乘客,我们需要创建一个具有不同选项的菜单。为此,我们必须有两个类和一个主程序,整个过程基于一个队列。我的问题是,我在队列中添加乘客的程序中最基本的部分不起作用,并且在添加乘客后提示显示时显示以下内容:

队列: airport.Passenger@55f96302

忽略程序远未完成的事实,这是我到目前为止所做的:

主:

 public class Airport {

    /**
     * @param args the command line arguments
     */        
    public static void main(String[] args) {
        PassengerQueue queue1= new PassengerQueue();
        Passenger[]  ArrayOfPassenger = new Passenger[20];
        Scanner input= new Scanner (System.in);
        String optionMenu;

        for (int i = 0; i < 20; i++)
            ArrayOfPassenger[i]= new Passenger();

        char letter;
        do {
            System.out.println("Enter display option [A-add passenger, V- view passenger queue, D- delete passenger from queue, S- store passengerQueue data into file, L- load passengerQueue data from file] or 'X' to stop: ");
            optionMenu = input.next();
            letter = optionMenu.charAt(0);

            switch (letter) {
                case 'A':
                    queue1.AddPassenger();
                    break;
                case 'V':
                    queue1.display();
                case 'D':                       
                    break;
                case 'S':                      
                    break;
                case 'L':                       
                    break;                       
            }

        } while (letter != 'X');    
    }
}        

乘客等级:

public class Passenger {
    private String firstName;
    private String surname;
    private int secondsInQueue;
    Scanner input= new Scanner(System.in);

    /**
     *
     * @return
     */
    public String getname() {
        return firstName= " "+surname;
    }

    public void setName (String fName, String sName) {
        firstName = fName;
        surname = sName;
    }

    public Passenger () {
        firstName = "";
        surname = "";
    }
}

乘客队列类:

public class PassengerQueue {
    Scanner input= new Scanner(System.in);
    private Passenger[] qArray =new Passenger [20];
    private int first = 0;
    private int last = 0;
    private int maxStayInQueue;
    static final int MAX_QUEUE_SIZE = 20;
    public PassengerQueue() {
    for (int i = 0;i < 20; i++)
        qArray[i]= new Passenger();
}

public void AddPassenger() {
    System.out.println("Enter passenger's First Name:");
    String FirstName = input.next();
    System.out.println("Enter passenger's surname:");
    String Surname= input.next();
    qArray[last].setName(FirstName, Surname);
    last++;
    if(last == qArray.length){
        last = 0;
    }
}

public void display() {
    System.out.println("The queue: ");
    for(int i = first; i < last; i++)
        System.out.println(qArray[i]);
}

public boolean isEmpty() {
    return first == 0;
}

public boolean isFull() {
    return last == 19;
}

}

我的问题是我在这里做错了什么,在队列1中分配值时我该怎么做才能解决它?我现在已经尝试了解了一段时间,但我不知道。

1 个答案:

答案 0 :(得分:0)

我的猜测是,Java不知道Passenger类的正确字符串表示是什么。要告诉System.out.println()如何将Passenger表示为String,请覆盖toString()方法。所以在Passenger类中,添加如下内容:

public String toString() {
    return firstName + " " + surname;
}