0

该程序模拟呼叫中心、银行、商店、机场等场所的客户服务操作,由柜员服务客户。顾客随机到达并排队等候,直到有柜员为他们服务。等待线是用队列数据结构实现的。但是我遇到了两个小错误 1.) 我的入队方法不适用于该参数,并且 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() );
}
4

2 回答 2

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 上保存除整数以外的任何内容,那么您需要更改参数类型/返回类型以反映这一点。

于 2013-10-16T01:58:34.160 回答
0
**line.enqueue(customer);**
    // 1.) my enqueue method is not applicable for the argument 

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

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

(Customer) line.dequeue();. 在这里你要转换Customerint-line.dequeue() 你的dequque方法 return amint所以基本上你是在说一个Customer等于 and int,这是不可能的,除非Customerisint,它不是

你要这个:

Customer customer = new Customer(line.dequeue) 
   // Customer constructor takes an int value
于 2013-10-16T02:23:23.303 回答