我的队列模拟器出错

时间:2013-10-16 01:54:34

标签: java queue

该程序模拟客户服务操作,例如呼叫中心,银行,商店,机场,客户由柜员服务。客户随机到达并排队等候,直到柜员可以为他们服务。等待线用队列数据结构实现。但是我得到两个小错误1.)我的enqueue方法不适用于参数和2.)无法从int转换为客户。这是代码。错误出现在朝向末尾的粗体线中

import java.util.Random;
class MyQueue<E> { 
private int maxSize; 
private int[] queArray;
private int front;              
private int rear;
public MyQueue(int s) // constructor
{
maxSize = s+1; // array is 1 cell larger
queArray = new int[maxSize]; // than requested
front = 0;
rear = -1;
}

public void enqueue(int j) // put item at rear of queue
{
if(rear == maxSize-1)
rear = -1;
queArray[++rear] = j;
}

public int dequeue() // take item from front of queue
{
int temp = queArray[front++];
if(front == maxSize)
front = 0;
return temp;
}

public int peek() // peek at front of queue
{
return queArray[front];
}

public boolean isEmpty() // true if queue is empty
{
return ( rear+1==front || (front+maxSize-1==rear) );
}

public boolean isFull() // true if queue is full
{
return ( rear+2==front || (front+maxSize-2==rear) );
}

public int size() // (assumes queue not empty)
{
if(rear >= front) // contiguous sequence
return rear-front+1;
else // broken sequence
return (maxSize-front) + (rear+1);
}
}
class Customer { int arrive; // Time point that the customer arrived. int processTime; // Time duration that the customer will need to be served.
/**
 * Default constructor
 */
public Customer() {
    arrive = 0;
    processTime = 0;
}

/**
 * Set the arrival time point of the customer.

 * 
 * @param Time
 *            point
 */
public Customer(int arrivalTime) {
    arrive = arrivalTime;

    // We set the processing time as a random integer between 1 and 3.
    processTime = (int) (Math.random() * 3 + 1);
}

/**
 * @return the arrival time point of the customer.
 */
public int getArrivalTime() {
    return arrive;
}

/**
 * @return the processing time of the customer.
 */
public int getProcTime() {
    return processTime;
}
}
public class Simulator { /** * The main method of the class. * * @param args * Command line arguments. */ public static void main(String[] args) {
if (args.length != 3) {
    System.out.println("usage: " + Simulator.class.getSimpleName()
            + " qCapacity simHours customPerHour");
    System.out.println("Example: " + Simulator.class.getSimpleName()
            + " 10 1 30");
    System.exit(-1);
}
// maximum size of queue
int qCapacity = Integer.parseInt(args[0]);

// number of simulation hours
int simHours = Integer.parseInt(args[1]);

// average number of customers per hour
int custPerHour = Integer.parseInt(args[2]);

// Run simulation
simulation(qCapacity, simHours, custPerHour);
}

private static void simulation(int qCapacity, int simHours, int custPerHour) {
    // Constant
    final int MIN_PER_HR = 60;

    // A queue that will hold and manage objects of type Customer.
    MyQueue<Customer> line = new MyQueue<Customer>(qCapacity);

    // For how many cycles should the simulation run. We assume that each
    // cycle takes one minute.
    int cycleLimit = MIN_PER_HR * simHours;

    // The average number of customers can arrive per minute
    float custPerMin = ((float) custPerHour) / MIN_PER_HR;

    // The number of customers that were turned away because the line
    // (queue)
    // was full at the time they arrived.
    int turnAways = 0;

    // Number of customers that arrived.
    int customers = 0;

    // Number of customers that were served.
    int served = 0;

    // Total number of customers that entered the line (queue).
    int sumLine = 0;

    // Waiting time until the next customer is served.
    int waitTime = 0;

    // Total time that all the customers waited in the line.
    int lineWait = 0;

    // Simulation
    for (int cycle = 0; cycle < cycleLimit; cycle++) {
        float j = custPerMin;
        while (j > 0) {
            if (newCustomer(j)) {
                if (line.isFull()) {
                    turnAways++;
                } else {
                    customers++;
                    Customer customer = new Customer(cycle);
                    **line.enqueue(customer);**
                }
            }
            j = j - 1;              
        }

        if (waitTime <= 0 && !line.isEmpty()) 

{
        **Customer customer = (Customer) line.dequeue();**
        waitTime = customer.getProcTime();
        lineWait += cycle - customer.getArrivalTime();
        served++;
    }

    if (waitTime > 0) {
        waitTime--;
    }

    sumLine += line.size();
}

// Print the simulation results.
if (customers > 0) {
    System.out.println("\nCustomers accepted: " + customers);
    System.out.println("  Customers served: " + served);
    System.out.println(" Customers waiting: " + line.size());
    System.out.println("         Turnaways: " + turnAways);
    System.out.println("Average queue size: " + (float) sumLine
            / cycleLimit);
    System.out.println(" Average wait time: " + (float) lineWait
            / served + " minutes");
} else {
    System.out.println("No customers!");
}
}

private static boolean newCustomer(float j) {
    if(j > 1)
    return true;
else
    return (j > Math.random() );
}

2 个答案:

答案 0 :(得分:0)

看起来您的问题在于这两种方法:

public void enqueue(int j) // put item at rear of queue
{
if(rear == maxSize-1)
rear = -1;
queArray[++rear] = j;
}

public int dequeue() // take item from front of queue
{
int temp = queArray[front++];
if(front == maxSize)
front = 0;
return temp;
}

如果您打算在Queue上保留除整数之外的任何内容,那么您需要更改参数类型/返回类型以反映该内容。

答案 1 :(得分:0)

**line.enqueue(customer);**
    // 1.) my enqueue method is not applicable for the argument 

您的enqueue方法需要int argmuent,但您尝试将Customer类型传递给它。也许你想要这样的东西:line.enqueue(customer.getSomething());。我无法从你的代码中说出来。

**Customer customer = (Customer) line.dequeue();**
    // 2.)cannot cast from int to customers

(Customer) line.dequeue();。在这里,您将Customer投射到int - line.dequeue() 您的dequque方法返回时间int所以基本上您说的是Customer等于int,除非Customer isint,否则这是不可能的。吨

你想要这个:

Customer customer = new Customer(line.dequeue) 
   // Customer constructor takes an int value