2013-10-16 20 views
0

該程序模擬客戶服務操作的地方,例如呼叫中心,銀行,商店,機場,客戶由櫃員服務。客戶隨機到達並排隊等候,直到有一個出納員可以爲他們服務。等待線由隊列數據結構實現。然而,我得到兩個小錯誤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()); 
} 
+1

我們可以看到堆棧跟蹤嗎? –

回答

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

如果您打算在隊列中持有什麼,但整數,那麼你將需要更改參數類型/返回類型以反映這一點。

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();。在這裏,我們鑄造Customerint - line.dequeue()dequque方法回報我int所以基本上你說一個Customer等於和int,這是不可能的,除非Customer isint,它是不是

你想這樣的:

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