I am trying to
1. determine the max line (only displays 1):cursing::cursing:
2. randomize arrival time and service time:cursing::cursing:
3. dequeue when service complete
note: sorry my queue class is cleaner than the simulator class(where the issue is)
public class LineSimulator {
private static final int SIMULATION_TIME = 1440;
private static int timePassed = 0;
private static int custNum;
private static int totalCustomers;
private static int serviceTime;
private static int maxLineLength;
private static int longestWait;
private static double avgWaitTime;
private static String avgLineLength;
private static int nextArrival;
public static void main(String[] args) {
int i = 1;
int firstCustArrival = (int) Math.random() * SIMULATION_TIME + 1;
serviceTime = (int) Math.random() * SIMULATION_TIME + 1;
ShoppingLineQueue line = new ShoppingLineQueue();
System.out.println("Little's Law Test\nA Shopping Line\n");
while ((SIMULATION_TIME - timePassed) != 0) {
if (timePassed == firstCustArrival) {
custNum = i;
line.addCustomer(custNum, serviceTime);
System.out.println("Customer: " + custNum + " Arrived after "
+ "opening: " + firstCustArrival + "minutes Service "
+ "time: " + serviceTime);
maxLineLength = line.size();
nextArrival = (int) Math.random()
* 100 + 1;
}
nextArrival = (int) Math.random()
* 100 + 1;
if (timePassed != firstCustArrival) {
i++;
custNum = i;
line.addCustomer(custNum, serviceTime);
System.out.println("Customer: " + custNum + " Arrived after "
+ "opening: " + nextArrival + "minutes Service "
+ "time: " + serviceTime);
nextArrival = (int) Math.random()
* (SIMULATION_TIME - timePassed) + 1;
if (maxLineLength < line.size()) {
maxLineLength = line.size();
}
}
if (timePassed == firstCustArrival) {
if (line.seeFirstInLine() == timePassed) {
if (line.removeCustomer() == "UnderFlow") {
System.out.println("No Arrival Yet");
} else {
line.removeCustomer();
}
}
}
serviceTime += ((int) Math.random()
* 100);
timePassed++;
}
avgWaitTime = SIMULATION_TIME / custNum;
System.out.println("Max number of customers in line at "
+ "any one time: " + maxLineLength + "\n"
+ "Longest wait time for one customer: " + longestWait
+ "\nAverage wait time for all customers: " + avgWaitTime
+ "\nAverage line length: " + avgLineLength + "\n");
}
}
Here is the queue:
public class ShoppingLineQueue<Customer> implements Iterable<Customer> {
private int lineSize;
private ShopperNode firstPerson;
private ShopperNode lastPerson;
/**
* Constructor
* Big O(1)
*/
public ShoppingLineQueue(){
firstPerson = null;
lastPerson = null;
}// end of constructor
/**
* Check if queue empty
* Big O(1)
* @return boolean
*/
public boolean isEmpty(){
return firstPerson == null;
}// end of isEmpty
/**
* Get the size of queue
* Big O(1)
* @return size of line
*/
public int size(){
return lineSize;
}// end of size
/**
* Look at first person in line
* Big O(1)
* @return first person
*/
public int seeFirstInLine(){
if(isEmpty()){
throw new RuntimeException("Underflow");
}
return firstPerson.timeInLine;
}// end of seeFirstInLine
/**
* Add customer to end of line
* (Enqueue)
* Big O(1)
* @param customer
*/
public void addCustomer(Customer customer, int time){
ShopperNode shopper = new ShopperNode();
shopper.cust = customer;
shopper.timeInLine = time;
if(isEmpty()){
firstPerson = shopper;
lastPerson = shopper;
} else {
lastPerson.nextInLine = shopper;
lastPerson = shopper;
}// end of if else
lineSize++;
}// end of addCustomer
/**
* Remove first customer after service complete
* (Dequeue)
* Big O(1)
* @return customer
*/
public Customer removeCustomer(){
if(isEmpty()){
throw new RuntimeException("Line is empty");
}
Customer customer = firstPerson.cust;
firstPerson = firstPerson.nextInLine;
lineSize--;
if(isEmpty()){
lastPerson = null;
}
return customer;
}// end of removeCustomer
/**
* Iterates through list
* Big O(1) but is really Big O(n) when returning
* @return new FIFOIterator
*/
public Iterator<Customer> iterator() {
return new FIFOLineIterator();
}// end of Iterator
/**
* Standard Iterator
* Big O(n) worst case
*/
private class FIFOLineIterator implements Iterator<Customer>{
private ShopperNode pos = firstPerson;
public boolean hasNext(){
return pos != null;
}
/* not implemented */
public void remove(){
throw new UnsupportedOperationException();
}
public Customer next(){
if(!hasNext()){
throw new NoSuchElementException();
}// end of if
Customer customer = pos.cust;
pos = pos.nextInLine;
return customer;
}// end of next
}// end of FIFOLineIterator private class
/**
* Node for shopper
*/
private class ShopperNode{
private Customer cust;
private int timeInLine;
private ShopperNode nextInLine;
}// end of shopper Node private class
}// end of ShoppingLineQueue class
The code displays this(basically):Customer: 1 Arrived after opening: 1... .... .... ... ... ... Customer: 1433 Arrived after opening: 1minutes Service time: 1 Customer: 1439 Arrived after opening: 1minutes Service time: 1 Customer: 1440 Arrived after opening: 1minutes Service time: 1 Max number of customers in line at any one time: 1438 Longest wait time for one customer: 0 Average wait time for all customers: 1.0 Average line length: nullThanks


Sign In
Create Account


Back to top









