Objective

  • Practicing String, Array, user-defined objects, array of objects
  • Working with Data

Specification

Given an array of objects with a strictly formatted set of sample data (in the provided file a2.js), serving as "database", create a user-defined object called orderDB based on the following specifications. Test this object orderDB using the given testing statements (in the section of TEST DATA in the provided file a2.js). A complete result/output is available in file a2_outpu.pdf for your reference. You may need your own necessary testing statements to accomplish this task.

File a2.js includes two blocks of code:

1) data, array of objects (var allData)
2) testing statements (/* TEST DATA */)

  • you will write your orderDB object between them.
  • The block labeled TEST DATA at the bottom is currently commented out, but you must uncomment it gradually to test your solution step by step. After running your code with the TEST Data uncommented, the output in the web console should look like the sample file (a2_test_output.pdf). In this document, whenever the term "output" is used, that's output to the web console with console.log().
  • The block of code at the top of the file (labeled ALL DATA) is an array of objects called "allData". All the objects in this array follow the same format:
{ type: "customer" /* or "order" or "product"*/, data: { /* object with properties related to the "type" */ }

The "type" is one of "customer", "order", or "product", and the "data" is an object with properties that belong to the "customer", "order", or "product". The properties in the "data" object will always have the same names and follow the following convention for each of the following "types":

type: "customer"
customerId A primary key used to uniquely identify each customer.
firstName The customer's first name.
lastName The customer's last name.
email The customer's email address.

type: "order"
orderId A primary key that uniquely identifies the order.
customerId A foreign key identifying the customer who placed this order
orderDate The date of the order is placed.

type: "product"
productId A primary key used to uniquely identify each product.
orderId A foreign identifying the order with this product
amount The amount of the product in the order
price The price of the product

You can imagine it's a customer-order-product system. The relationships are like:
One customer can place many orders.
Each order can include many products.

Construct Object orderDB

In a2.js file, underneath the "allData" array, declare an object called orderDB using "Object Literal Notation". This object will contain the following properties & methods that need you to complete based on the following specifications.

Properties (/attributes/data):

The following are internal arrays that will serve as the primary storage mechanism within the orderDB object for all of the sample data.

1) customers:
This is an array that will contain all the sample data objects that are of type "customer". It is initialized as an empty array (ie, [] ) and will be manipulated using the methods in the Object OrderDB (define below).

2) orders:
This is an array that will contain all the sample data objects that are of type "order". It is initialized as an empty array ( ie, [] ) and will be manipulated using the methods in the Object OrderDB (define below).

3) products:
This is an array that will contain all the sample data objects that are of type "product". It is initialized as an empty array ( ie, [] ) and will be manipulated using the methods in the Object OrderDB (define below).

Methods (/functions/logic):

You are going to define 10 methods (functions) associated with the object orderDB. Please label each method with numbers in comment for convenience. e.g.,
// 1) constructArrays(allData)

Note: You can add more regular functions (not associated with the object) if needed.

1) constructArrays(allData)

It is the first method that will be invoked on your OrderDB Object. It is this method that takes all of the sample data (array "allData") and inserts it into the correct arrays (ie: "customers", "orders", or "products"). It takes one parameter, the array allData from the top part of file a2.js and processes it into three arrays of objects (i.e., customers, orders, products, which are properties of object orderDB) following the rules:

  • if type is "customer", insert the "data" object into the array "customers" . (HINT: call function addCustomer(customer), which will be defined below)
  • if type is "order", retrieve the "data" object and set its "orderDate" property to the current date and add it into the array "orders" . (HINT: call function addOrder(order), which will be defined below)
  • if type is "product", insert the "data" object into the array "products". (HINT: call function addProduct(product) that will be defined below) Once this method constructArrays(allData) has run, your "database" is built. The result will be:
  • array "customers" should contain 4 "customer" data objects,
  • array "orders" should contain 10 "order" data objects,
  • array "product" should contain 16 "product" data objects

HINT: The following methods/ functions also belong to the object orderDB. To refer to the property arrays of object orderDB (e.g., "customers") from within these methods, use "this" keyword, i.e.: "this.customers".

2) addCustomer (customer )

This method takes one parameter of object type "customer", and adds it to the array "customers".

3) addOrder (order)

  • This method takes one parameter of object type "order".
  • Sets the property orderDate of parameter (order) to the current date.
  • Add object order to the array "orders".

4) addProduct (product)

This method takes one parameter of object type "product", and adds it to the array "products".

5) displayCustomerById(custId)

This method takes a number representing a customerId and outputs the customer data for the corresponding customerId from the array "customers". Display in the following format:

For example, with the testing statement:

orderDB.displayCustomerById(1);

The display/result on web console is as follows. see image.

6) displayAllCustomers ( )

This method takes no parameters and simply outputs all customers in the same format as above 5), including a header at the top of the output stating "Display All Customers". The display/result on web console is as follows. see image.

7) displayAllOrders( )

This method takes no parameters and outputs all orders in the following format, including a header at the top of the output stating "Display All Orders". You need to refer to the array "customers" via the foreign key customerId to get the customers' first name and last name information. The display/result on web console is as follows. see image.

8) displayAllProducts()

This method takes no parameters and outputs all products in the following format, including a header at the top of the output stating "Display All Products". The display/result on web console is as follows. see image.

9) displaySales()

This method searches all the arrays to collect the order information for all customers. The display format is as follows:

Customer < firstName lastName> placed orders. Total cost is $< total cost for all the orders>

Where the above information in < > is retrieved from the arrays, i.e.: customers, orders, and products. Hint: the cost for each product is amount * price from the array product. The total cost is total costs of all the products of all orders for the specific customer.

The display/result is as follows. see image.

10) removeOrderById(orderId)

This method takes a number representing an orderId and searches through the orders array and removes the order with the matching "orderId" property from the array.

Also, this method must ensure that all the products with "orderId" are removed from the array products.

If orderId does not exist in the array orders, your program reports:

OrderId < orderId> is not found

Where < orderId> is the orderId you try to remove.

For example, if try to remove orderId 177, it's not found in the array. If remove orderId 101, the order with orderId 101 is removed from array orders. Also, the product with productID 100002 was removed from the array product because its orderId is 101.

The result/display on web console is as follows. see image.

TEST DATA Output

Once you're ready to test your solution, uncomment the "TEST DATA" block of code line by line and run your code. Do not recommend testing until you complete the whole assignment, which may put you in a very complex situation, which is hard to debug.

Academic Honesty!
It is not our intention to break the school's academic policy. Posted solutions are meant to be used as a reference and should not be submitted as is. We are not held liable for any misuse of the solutions. Please see the frequently asked questions page for further questions and inquiries.
Kindly complete the form. Please provide a valid email address and we will get back to you within 24 hours. Payment is through PayPal, Buy me a Coffee or Cryptocurrency. We are a nonprofit organization however we need funds to keep this organization operating and to be able to complete our research and development projects.