Skip to content

S24_AmazonOrderProcessing_Hard

Wil Simpson edited this page Jul 30, 2021 · 8 revisions

Portfolio Home Page

Problem Statement

Using ArrayBlockingQueue as a buffer, you will construct a program that will emulate the processing of an order from Amazon.com (or any online order) from when you place the order to when it gets put on the truck for delivery to your home.

Your program will be structured as shown in the tree below: Diagram

Each node must:

  • Run in its own thread
  • Contain one input buffer and 1-2 output buffers. Two exceptions:
    • The Web Server node reads from a file and does not have an input buffer
    • The Delivery Truck Nodes print to the screen and do not have output buffers.

Additional Specifications:

  • The buffer between two nodes must be shared.
  • The only means of communication between nodes is the buffer between them.

Functionality of Each Node

Amazon Web Server

Receives orders containing the following information:

  • Delivery address
  • Name on order
  • Item ordered
  • Item category

This node must read these orders from the S24_AmazonOrderProcessing_OrdersFile.csv the document file which resembles the following table:

address city state zip name item category
1234 Iowa Avenue Denver Iowa 52234 Jane Doe Laptop Computer
.......plus 19 more entries.......

For each order received, it decides which Shipping Center to send it to based on the following logic:

  • Orders to the following cities got to Shipping Center 1:
    • Los Angeles
    • San Francisco
    • Seattle
    • Denver
  • Orders to the following cities go to Shipping Center 2
    • Des Moines
    • Chicago
    • St. Louis
    • Any other city not already mentioned

Also, when this node reaches the end of orders in the .csv file, it must send some sort of indication to all the Shipping Center nodes through the shared buffer that the end has been reached and then terminate.

Shipping Center

The Shipping Center must decide which part of the warehouse the item is in and send a command to the appropriate forklift to go get it and deliver it to the shipping dock.

The Shipping Center organizes items based on the first letter of their category. It will also append the Shipping Center number to the order for tracking purposes.

The processing is as follows:

  • Orders with categories beginning with A-P go to Section 1
  • Orders with categories beginning with Q-Z go to Section 2
Shipping Center Sections

The Shipping Center Sections will do the following:

  1. Receive order
  2. Append the Shipping Center Section number to the order for tracking purposes
  3. Pause for 0-5 seconds. This period must be decided randomly
  4. Put order on the Shipping Dock's buffer
Shipping Dock

The Shipping Dock nodes will fill up the buffers to the trucks as there is room available. They will start with truck 1. If the buffer for Truck 1 is full, then try truck 2. If they are all full, then wait until one buffer becomes available.

Trucks

The trucks perform the following sequence:

  1. Read from their input buffer until they have received 4 deliveries or receive notification that there are no more deliveries. On each delivery, they must append the truck number for tracking purposes.
  2. For each of the four deliveries (or less if the "no more deliveries" notification was received), do the following:
    1. Sleep for 0-10 seconds. This period must be decided randomly.
    2. Print out the information on the shipping order to the screen

The truck also must also print out a message to the screen after it has received the “no more deliveries” notification and delivered its last order indicating that it is done delivering. This message should specify the truck’s number and the number of its associated shipping center.

The Shipping order information printed to the screen must include the following information:

  • Delivery address
  • Name on order
  • Item ordered
  • Item category
  • Shipping Center the order went through
  • Shipping Center Section the order went through
  • Delivery Truck the order was delivered on

Important Requirements

  • The program must be multi-threaded.
  • The program must not continue on indefinitely. All threads must stop on their own once all orders have been processed. All orders received must be delivered.
  • Only the Delivery Truck nodes should print to the screen and they should print ONLY what is specified in their specifications. This requirement is to allow us to grade this problem easily.

User Documentation

To start the simulation compile and run AmazonOrderProcessor. The contents of the delivery being made will print to the screen as they are done. Once all the orders are done being processed and delivered the program will immediately stop.

Developer Documentation

The nodes above also implement a Node which implements Runnable and connects a single input to an array of output buffers. The run() method is implemented as follows:

Nodes with input buffers:

  1. Infinitely call doOperations() until the input buffer sets the upstream to finished.
  2. Infinitely call doOperations() until the input buffer is empty
  3. Run doFinally()
  4. Sets all output buffers to upstream finished
  5. Stop processing

Nodes without input buffers:

  1. Run doOperations()
  2. Run doFinally()
  3. Sets all output buffers to upstream finished
  4. Stop processing

A Buffer stores all elements T in an ArrayBlockingQueue where calls to putBlocking() and getBlocking() wait until an item can be put or taken into the queue respectively. It also implements a boolean state called upstreamFinished which allows all nodes connected to the Buffer to know if the upstream node has finished processing data into the queue.

Each non-abstract Node is implemented as described above.

CSVReader has methods getNextLine() that returns the next line in the CSV file and finishedReading() which returns true if the CSV file is finished being parsed which allows for easy parsing of CSV files.

Both Node and Buffer implement anonymous types which are used to processes items through the Buffer. In this case Order's are being passed through the Buffer and Node's.

AmazonOrderProcessor sets up buffer and node ordering as described in the problem statement and adds each node to a ExecutorServer thread pool which processes each Node on multiple threads.

UML Diagram: UML Diagram

JavaDocs

The java documents are served from a local web server on this machine. To start the web server, navigate to the directory immediately above where the source code is checked out (i.e. ~/git ) and then use "python -m SimpleHTTPServer" in that directory.

cd ~/git
python -m SimpleHTTPServer&

Note: if you are running python 3 (which you can check via opening a terminal and typing: python --version), then the command is:

python3 -m http.server

Click Here to View JavaDocs

Source Code

Link to the source code