The main data structure for this week's project was doubly-linked link based queue (basically based on nodes).  Queue was implemented for this project because of its data process mechanism: FIFO (first-in-first-out).  FIFO is essential to this project because we want to calculate how much time it takes for customers to check out their items after waiting in the line of queue.  Whoever entered the line, i.e. queue, first will check out the first.  Therefore, we use queue to store customers and calculate how long it takes for their shopping depending on their strategies.  The overview of this project is similar to the last three projects, where we ran simulations using different data structures such as 2D-array, stack and linked-lists.  Agent class will play as a parent class for customer, checkout, and spawner.  Customer class will be the most part of visualization: squares moving around to shop, wait in line, and check out at the end.  Check-out class will be rectangles and won't move.  However, they play an important role in updating customer's phases and moving them in and out of queue.  Spawner class is simple: they add customers to the landscape.  Landscape class will hold all these classes together and call their updateState and draw methods.  In addition, they will hold a secondary arraylist of customers in order to calculate mean and standard deviation of time taken for checked-out customers.  Lastly, CheckOutSimulation class will bring these classes together and visualize them through java swings.

Main Code:


This week's Agent class is identical to that of last week.  It holds fields for x and y coordinates so that agents can be displayed on the landscape.  I also added a String type parameter, type, for landscape class to call appropriate methods depending on the type of the agent.  Its updateState and draw methods will be implemented in its subclasses: Customer, Checkout, and Spawner.


Customer class had its own four fields to hold information about number of items it has, what phase it is in, what strategy it uses, and the time it has taken to check out.

I used enumeration Phase(SHOP, SELECT, WAIT, CHECKOUT, DONE) for customer and checkout class's updateState() methods.  It was easier to access and modify phases using enumeration.  Within updateState() method, customer class modified its own phase up to WAIT.  Checkout class will take from there and modify customer's phase to CHECKOUT and DONE.  For more detailed information about what happens in each phase, refer to my codes and comments.  One thing to note is that I used Random's nextInt() method to obtain a random integer and used it to select a random checkout lines for SELECT phase.


arry is an arraylist of checkout classes, obtained by landscape class's method getCheckoutAgents().  CheckoutLine is the chosen queue, and customer adds itself to that queue using Checkout class's add method.

Once customer selects a queue, it places itself next to the counter.  I modified x and y coordinates of customer to visualize the change.

Lastly, I created methods such as getItems(), decrementItem(), incrementTime(), getTime(), setPhase(), and getPhase() to allow Checkout class to modify customer's instance variables.


As mentioned in the introduction, Checkout class is essentially a queue of customers and thus has a field of queue.  I created numCustomers(): tells how many customers are in queue, size(): returns the total number of items in queue, and add(): adds customer to queue methods for customer class's updateState() method.

Checkout class updates customers to check out and done phase.  One thing to note is that checkout class removes customers from the landscape when they are done by changing their x&y coordinates and updates the rest of the customers' positions.


Spawner is the simplest agent class because it only has one function: add customers to the landscape.  It holds two fields int numAgents: number of customers to add every iteration of simulation and int control: strategy for customer agents.  The second field was added for one of my extensions.

I simply used for loop and landscape's addAgent method to insert customers into the landscape.

I am able to modify customer's strategy after initializing it because I created a method setStrategy() in Customer class.


Landscape class holds four instance variables: width and height of the scape, arraylist for all agents - customers, checkout, and spawner, and another arraylist just for customers.  In order to move checked-out customers to the arraylist checkout, I created moveCustomer() method which simply adds a customer to the list.  It is used in Checkout class's updateState() method when it updates customer's phase to done.

Another thing to mention is that I used regular for loop, instead of for each loop, in updateAgents() method in order to avoid ConcurrentModification error.  For the same reason, I used for loop in draw method.

For statistics data, I created getMean() and getSD() methods that use Math package.  The methods will be used in simulation class.


CheckOutSimulation is a GUI, which uses java Swings package and brings all the agent classes and landscape together to run a simulation.  It is almost identical to AgentSimulation class from the last week's project.  Below is the animation of running CheckOutSimulation with command line arguments: 500 1.  I will explain what the arguments are in the extension.


Because my spawner agent class generates 5 customers every iteration, I ran my simulation 400 times (count up to 2000 customers) and calculated statistics for each strategy.  The results are as below.





In order to make my visualization more interesting, I colored customers agents with black and checkout agents with green.  When customers agent's phase shifts to checkout, its color becomes pink.

Moreover, I added two JLabels to show mean and sd of time taken to check out after every 50 step of simulation.  To do so, I added two private static JLabel fields to the CheckOutSimulation class and modified them in the main method.

Below is an image of my simulation.

I implemented a node-based arraylist.  Please refer to and run its main method to see if it has the same functionality as array-based arraylist.


Please refer to and run its main methods.  The results should be identical with those of


I enabled user to control the number of iterations for simulation and which strategy to use for customers using command line argument.  The first command line argument refers to the number of iterations and the second arguments, which should be 1, 2, or 3, refers to the strategy.


I learned how queue can be useful as a data structure.  More importantly, I realized the importance of designing before starting to code and understanding the purpose of each Java class.  

For this project, I received help from professor Maxwell and Layton and used Nhi's report as a reference.