基于LinkedQueue的多队列购物等待线模拟程序开发思路求助
Great job getting the single-queue simulation up and running! Let's walk through how to extend this to three queues with separate cashiers—this is pretty straightforward once you break down the changes needed.
1. Split State Variables Per Queue
Right now, you've got shared variables like numberOfArrivals and transactionTimeLeft that only track one queue. We need to give each queue its own set of these stats. Replace your current instance variables with arrays (or a helper class, but arrays are simpler for starters):
private QueueInterface<Customer>[] lines; private int[] numberOfArrivals; private int[] numberServed; private int[] totalTimeWaited; private int[] transactionTimeLeft; private int globalCustomerId; // Optional: For unique customer IDs across all queues
Update your constructor to initialize these arrays for three queues:
public WaitLine() { lines = new QueueInterface[3]; lines[0] = new LinkedQueue<>(); lines[1] = new LinkedQueue<>(); lines[2] = new LinkedQueue<>(); numberOfArrivals = new int[3]; numberServed = new int[3]; totalTimeWaited = new int[3]; transactionTimeLeft = new int[3]; globalCustomerId = 0; reset(); }
2. Add Logic to Assign Customers to the Shortest Queue
When a new customer arrives, you'll want to send them to the shortest line (a common real-world strategy). Add a helper method to find which queue is shortest:
private int findShortestQueue() { int shortestIndex = 0; int minSize = lines[0].getSize(); // Make sure your QueueInterface has a `getSize()` method; if not, track sizes separately for (int i = 1; i < lines.length; i++) { int currentSize = lines[i].getSize(); if (currentSize < minSize) { minSize = currentSize; shortestIndex = i; } } return shortestIndex; }
Then, in your simulate method's arrival block, use this to assign the customer:
if (Math.random() < arrivalProbability) { globalCustomerId++; // Use global ID for unique customer numbers int transactionTime = (int)(Math.random() * maxTransactionTime + 1); int targetQueue = findShortestQueue(); numberOfArrivals[targetQueue]++; Customer nextArrival = new Customer(clock, transactionTime, globalCustomerId); lines[targetQueue].enqueue(nextArrival); System.out.println("Customer " + globalCustomerId + " enters line " + (targetQueue + 1) + " at time " + clock + ". Transaction time is " + transactionTime); }
3. Update the Simulation Loop to Handle All 3 Queues
Instead of only checking one queue for service, iterate through all three to manage each cashier's state:
for (int clock = 0; clock < duration; clock++) { // Handle new arrivals (as above) // Process each queue's service state for (int i = 0; i < lines.length; i++) { if (transactionTimeLeft[i] > 0) { // Cashier is still serving a customer transactionTimeLeft[i]--; } else if (!lines[i].isEmpty()) { // Cashier is free; start serving the next customer in line Customer nextCustomer = lines[i].dequeue(); transactionTimeLeft[i] = nextCustomer.getTransactionTime() - 1; int timeWaited = clock - nextCustomer.getArrivalTime(); totalTimeWaited[i] += timeWaited; numberServed[i]++; System.out.println("Customer " + nextCustomer.getCustomerNumber() + " begins service at line " + (i + 1) + " at time " + clock + ". Time waited is " + timeWaited); } } }
4. Fix the displayResults Method
Your current display code repeats the same stats for all three lines—we need to show each queue's unique data:
public void displayResults() { for (int i = 0; i < lines.length; i++) { System.out.println("\nLINE " + (i + 1) + ":"); System.out.println("Number served = " + numberServed[i]); System.out.println("Total time waited = " + totalTimeWaited[i]); // Avoid division by zero if no customers were served double averageTimeWaited = numberServed[i] > 0 ? ((double)totalTimeWaited[i]) / numberServed[i] : 0.0; System.out.println("Average time waited = " + averageTimeWaited); int leftInLine = numberOfArrivals[i] - numberServed[i]; System.out.println("Number left in line = " + leftInLine); } }
5. Update the reset Method to Clear All Queue Data
Make sure you reset every queue's stats and clear each line:
public final void reset() { for (int i = 0; i < lines.length; i++) { lines[i].clear(); numberOfArrivals[i] = 0; numberServed[i] = 0; totalTimeWaited[i] = 0; transactionTimeLeft[i] = 0; } globalCustomerId = 0; }
Quick Notes to Watch For
- If your
QueueInterfacedoesn't have agetSize()method, you'll need to track each queue's size separately with anint[] queueSizesarray that you update when enqueuing/dequeuing. - The
Customerclass doesn't need any changes unless you want to track which line they were in (not required for basic simulation).
That's all the core changes you need! Start with updating the state variables, then build the customer assignment logic, and finally adjust the simulation and display loops to handle three queues. You'll be up and running with a multi-queue simulation in no time.
内容的提问来源于stack exchange,提问作者SWAGALERT9000

