我无法弄清楚如何从Stack中打印Hire和Fire

时间:2015-01-30 02:06:05

标签: java stack queue

import java.util.*;
import java.util.Stack;

class Person {

private String name;
private String SS;

public Person(String N, String S) {
    this.name = N;
    this.SS = S;
}
}

class Manager {

Scanner keyboard = new Scanner(System.in);
private Queue<Person> app = new Queue<Person>();
private Stack<Person> hire = new Stack<Person>();
private Stack<Person> fire = new Stack<Person>();

public void Apply() throws QueueException {
    System.out.print("Applicant Name: ");
    String appName = keyboard.nextLine();
    System.out.print("SSN: ");
    String appSS = keyboard.nextLine();

    Person apply = new Person(appName, appSS);
    app.enqueue(apply);

}

public void hire() throws QueueException {
    if (!app.isEmpty()) {
        hire.push(app.dequeue());
    } else {
        System.out.println("Nobody to hire.");
    }
     }

public void fire() throws StackException {
    if (!hire.isEmpty()) {
        fire.push(hire.pop());
    } else {
        System.out.println("Nobody to fire");
    }

}
}

public class Management {
public static void main(String[] args) throws QueueException,     StackException{
    Scanner keyboard = new Scanner (System.in);
    Manager user = new Manager();
    boolean test = true;

    while (test){
        System.out.print("Press \n\t1 ACCEPT APPLICANT");
        System.out.print("\t2 Hire \n\t3 Fire \n\t4 Quit:");
        int action = keyboard.nextInt();
        String space = keyboard.nextLine();

        if (action == 1){
            user.Apply();
        }else if (action == 2){
            user.hire();
        }else if (action == 3){
            user.fire();
        } else if (action == 4){
            System.exit(0);
        }
        else{
            System.out.println("Please try again.");
        }

    }
}

}

我无法弄清楚如何打印出我刚雇用或解雇的人的名字和ssn。我尝试使用peek,但它没有工作。基本上,整个计划是关于是否接受和申请,雇用或解雇。如果我接受申请,它将提示用户输入他们的姓名和ss,如果我按雇用/火,它应该打印出该人的姓名和名称。

1 个答案:

答案 0 :(得分:0)

你错误地定义了队列。

更像是:Queue<Person> app = new LinkedList<Person>();

你需要一个打印方法。这是Manager类。打印在管理类中调用,如果你定义Manager user = new Manager();,你很可能看起来很有趣。

删除所有投掷。它们没有定义,也没有使用....

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

class Manager
{

    Scanner keyboard = new Scanner(System.in);

    //private Stack<Person> app = new Stack<Person>();

    private Stack<Person> hire = new Stack<Person>();

    private Stack<Person> fire = new Stack<Person>();


Queue<Person> app = new LinkedList<Person>();

    public void Apply() 
    {
        System.out.print("Applicant Name: ");
        String appName = keyboard.nextLine();
        System.out.print("SSN: ");
        String appSS = keyboard.nextLine();

        Person apply = new Person(appName, appSS);
        //app.enqueue(apply);
        app.add(apply);
    }

    public void hire() 
    {
        if (!app.isEmpty())
        {
            hire.push(app.remove());
        }
        else
        {
            System.out.println("Nobody to hire.");
        }
    }

    public void fire() 
    {
        if (!hire.isEmpty())
        {
            fire.push(hire.pop());
        }
        else
        {
            System.out.println("Nobody to fire");
        }

    }

    public void dump()
    {
        while(!app.isEmpty())
        {
           Person p = app.remove();
           System.out.println(p.getName()+"  "+ p.getSS());
        }
    }

}