Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /************************************************************************
- WEB222 – Assignment 02
- * I declare that this assignment is my own work in accordance with Seneca Academic Policy.
- * Nopart of this assignment has been copied manually or electronically from any other source
- * (including web sites) or distributed to other students.
- *
- * Name: Dexter John Agunoy Student ID: 113988174 Date: 09/27/18
- *
- ********************************************************************************/
- var allData = [
- {type:"store", data:{store_id: 297, name: "Scotiabank - Main Branch", address_id: 1023}},
- {type:"store", data:{store_id: 614, name: "Scotiabank - Hamilton", address_id: 1984}},
- {type:"store", data:{store_id: 193, name: "Scotiabank - Mississauga", address_id: 1757}},
- {type:"customer", data:{customer_id: 26, store_id:297, first_name: "Dave", last_name: "Bennett", email: "dbennett@gmail.com", address_id: 4536, add_date: null}},
- {type:"customer", data:{customer_id: 59, store_id:193, first_name: "John", last_name: "Stevens", email: "jstevens22@hotmail.com", address_id: 2473, add_date: null}},
- {type:"customer", data:{customer_id: 29, store_id:614, first_name: "Sarah", last_name: "Pym", email: "spym99@hotmail.com", address_id: 1611, add_date: null}},
- {type:"customer", data:{customer_id: 63, store_id:297, first_name: "Steven", last_name: "Edwards", email: "steven2231@hotmail.com", address_id: 1836, add_date: null}},
- {type:"customer", data:{customer_id: 71, store_id:614, first_name: "Martin", last_name: "Scott", email: "mdog33@gmail.com", address_id: 2727, add_date: null}},
- {type:"customer", data:{customer_id: 24, store_id:614, first_name: "Jonathan", last_name: "Pym", email: "jjpym@yahoo.ca", address_id: 1611, add_date: null}},
- {type:"customer", data:{customer_id: 36, store_id:193, first_name: "Kaitlyn", last_name: "Adams", email: "katy38@hotmail.com", address_id: 5464, add_date: null}},
- {type:"customer", data:{customer_id: 73, store_id:297, first_name: "Melissa", last_name: "Bennett", email: "mbennett@gmail.com", address_id: 4536, add_date: null}},
- {type:"address", data:{address_id: 1023, address: "2895 Yonge St.", city:"Toronto", province:"ON", postal_code:"L4C02G"}},
- {type:"address", data:{address_id: 1984, address: "3611 Main St. West", city:"Hamilton", province:"ON", postal_code:"R5O8H5"}},
- {type:"address", data:{address_id: 1757, address: "1177 Ontario St. Unit 8", city:"Mississauga", province:"ON", postal_code:"L9H6B3"}},
- {type:"address", data:{address_id: 4536, address: "3945 John St.", city: "Ajax", province: "ON", postal_code: "L7M4T9"}},
- {type:"address", data:{address_id: 2473, address: "391 Baker St. Apt 231", city: "Mississauga", province: "ON", postal_code: "M4T8S3"}},
- {type:"address", data:{address_id: 1611, address: "183 City Ct.", city: "Hamilton", province: "ON", postal_code: "J3T9V2"}},
- {type:"address", data:{address_id: 1836, address: "67 Rhymer Ave.", city: "Stouffville", province: "ON", postal_code: "L3C8H4"}},
- {type:"address", data:{address_id: 2727, address: "287 Brant St. Apt 4A", city: "Waterdown", province: "ON", postal_code: "R93G3P"}},
- {type:"address", data:{address_id: 5464, address: "11 New St. Apt 2B", city: "Brampton", province: "ON", postal_code: "L694R7"}},
- ];
- /**********************************
- * ALL DATA *
- * write your CustomerDB Object *
- * BELOW this Object *
- **********************************/
- /* Write your CustomerDB Object Here. Do not forget to uncomment the "TEST DATA" section
- when you're ready. Your code is required to run against these tests before you submit */
- //var block developed by assignments
- var CustomerDB = {
- //
- store: [],
- customer: [],
- address: [],
- insertData : function(object)
- {
- for (var a in object)
- {
- switch(object[a].type)
- {
- case 'store':
- this.store.push(object[a].data);
- break;
- case 'customer':
- this.addCustomer(object[a]);
- break;
- case 'address':
- this.address.push(object[a].data);
- break;
- }
- }
- },
- addCustomer : function(customerObj)
- {
- //adds it to the lsat value of the list
- this.customer.push(customerObj.data);
- //add date to the newest object added to the array
- this.customer[this.customer.length-1].add_date=Date();
- },
- outputCustomerById : function(customer_id)
- {
- var AddressInfo;
- for (var i=0; i<this.customer.length;i++)
- {
- if(this.customer[i].customer_id==customer_id)
- {
- console.log("Customer " + this.customer[i].customer_id + ": " + this.customer[i].first_name + " " + this.customer[i].last_name + " (" + this.customer[i].email+")");
- AddressInfo = this.getAddressById(this.customer[i].address_id);
- console.log("Home Address: " + AddressInfo.address + " " + AddressInfo.city + ", " + AddressInfo.province + ". " + AddressInfo.postal_code);
- console.log("Joined: " + this.customer[i].add_date + "\n");
- }
- }
- },
- outputAllCustomers : function ()
- {
- for (var i=0; i < this.customer.length; i++)
- {
- this.outputCustomerById(this.customer[i].customer_id);
- }
- },
- outputCustomersByStore : function(store_id)
- {
- console.log("Customers in Store: "+ this.getStoreById(store_id).name + "\n");
- for (var i=0; i<this.customer.length; i++)
- {
- if (this.customer[i].store_id==store_id)
- {
- this.outputCustomerById(this.customer[i].customer_id)
- }
- }
- },
- removeCustomerById : function (customer_id)
- {
- for(var i=0;this.customer.length;i++)
- {
- if(this.customer[i].customer_id == customer_id)
- {
- this.customer.splice(i,1);
- this(removeAddressById(this.customer[i].address_id))
- }
- }
- },
- addAddress : function( addressObj )
- {
- this.address.push(addressObj);
- },
- getAddressById : function(address_id)
- {
- //this value returns the current address
- var ReturnAddress;
- //for loop statement to have return value
- for (var i=0; i<this.address.length;i++)
- {
- if (this.address[i].address_id == ReturnAddress)
- {
- ReturnAddress = this.address[i];
- }
- }
- return ReturnAddress;
- },
- outputAllAddresses : function()
- {
- for(var i=0; i<this.address.length; i++)
- {
- console.log("Address "+this.address[i].address_id+": "+this.address[i].address + " "+ this.address[i].city+ " " + this.adress[i].province + ". "+this.address[i].postal_code)
- }
- },
- removeAddressById: function(address_id)
- {
- // make sure it does not exist in any other tables
- var found = 0;
- for(var i = 0; i < this.customers.length; i++)
- {
- if(this.customers[i].address_id == address_id)
- {
- found = 1;
- }
- }
- for(var i = 0; i < this.stores.length; i++)
- {
- if(this.stores[i].address_id == address_id)
- {
- found = true;
- }
- }
- if(found!=1){ // if it was not found
- var newArray = [];
- for(var i = 0; i < this.addresses.length; i++){
- if(this.addresses[i].address_id != address_id){
- newArray.push(this.addresses[i]);
- }
- }
- this.addresses = newArray;
- }
- },
- addStore : function( storeObj )
- {
- this.address.push(storeObj);
- },
- getStoreById : function( store_id )
- {
- //this value returns the current address
- var ReturnStore;
- //for loop statement to have return value
- for (var i=0; i<this.address.length;i++)
- {
- if (this.address[i].address_id == ReturnStore)
- {
- ReturnStore = this.store[i];
- }
- }
- return ReturnStore;
- },
- outputAllStores()
- {
- for (var i=0; i<this.address.length; i++)
- {
- var addressInfo=this.getAddressById(this.store[i].address_id);
- var storeInfo=this.getStoreById(this.store[i].address_id);
- console.log ("Store "+ storeInfo.store_id + " - "+storeInfo.name);
- console.log ("Location: "+addressInfo.address_id+", "+addressInfo.province+". "+addressInfo.postal_code);
- }
- }
- };
- /**********************************
- * TEST DATA *
- * write your CustomerDB Object *
- * ABOVE this code *
- * *
- * Uncomment this block of code *
- * when you're ready to test *
- * your CustomerDB Object *
- * *
- * You MUST Hand in your code *
- * with the test data *
- * uncommented, as this will *
- * help check your code for *
- * correctness *
- **********************************/
- // Insert all Data into the Database
- CustomerDB.insertData(allData);
- // output all customers
- console.log("CustomerDB.outputAllCustomers();\n\n--------------------------\n\n");
- CustomerDB.outputAllCustomers();
- console.log("--------------------------\n\n");
- // output all addresses
- console.log("CustomerDB.outputAllAddresses();\n\n--------------------------\n\n");
- CustomerDB.outputAllAddresses();
- console.log("--------------------------\n\n");
- // output all stores
- console.log("CustomerDB.outputAllStores();\n\n--------------------------\n\n");
- CustomerDB.outputAllStores();
- console.log("--------------------------\n\n");
- // output all customers in the "Main Branch"
- console.log("CustomerDB.outputCustomersByStore(297);\n\n--------------------------\n\n");
- CustomerDB.outputCustomersByStore(297);
- console.log("--------------------------\n\n");
- // remove Customer Dave Bennett (customer_id 26) and Martin Scott (customer_id 71)
- console.log("CustomerDB.removeCustomerById(26);\nCustomerDB.removeCustomerById(71);\n\n");
- CustomerDB.removeCustomerById(26);
- CustomerDB.removeCustomerById(71);
- console.log("--------------------------\n\n");
- // output all customers again
- // NOTE: Dave Bennett and Martin Scott should be missing
- console.log("CustomerDB.outputAllCustomers();\n\n--------------------------\n\n");
- CustomerDB.outputAllCustomers();
- console.log("--------------------------\n\n");
- // output all addresses again
- // NOTE: only addrss 287 Brant St. Apt 4A Waterdown, ON. R93G3P should be missing
- console.log("CustomerDB.outputAllAddresses();\n\n--------------------------\n\n");
- CustomerDB.outputAllAddresses();
- console.log("--------------------------\n\n");
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement