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 */)
{ 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. |
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.
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:
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)
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> placedorders. 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.
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.