Advertisement
Guest User

Untitled

a guest
Oct 11th, 2018
599
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 10.69 KB | None | 0 0
  1.  
  2. /************************************************************************
  3. WEB222 – Assignment 02
  4. * I declare that this assignment is my own work in accordance with Seneca Academic Policy.
  5. * Nopart of this assignment has been copied manually or electronically from any other source
  6. * (including web sites) or distributed to other students.
  7. *
  8. * Name: Dexter John Agunoy Student ID: 113988174 Date: 09/27/18
  9. *
  10. ********************************************************************************/
  11. var allData = [
  12. {type:"store", data:{store_id: 297, name: "Scotiabank - Main Branch", address_id: 1023}},
  13. {type:"store", data:{store_id: 614, name: "Scotiabank - Hamilton", address_id: 1984}},
  14. {type:"store", data:{store_id: 193, name: "Scotiabank - Mississauga", address_id: 1757}},
  15. {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}},
  16. {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}},
  17. {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}},
  18. {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}},
  19. {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}},
  20. {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}},
  21. {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}},
  22. {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}},
  23. {type:"address", data:{address_id: 1023, address: "2895 Yonge St.", city:"Toronto", province:"ON", postal_code:"L4C02G"}},
  24. {type:"address", data:{address_id: 1984, address: "3611 Main St. West", city:"Hamilton", province:"ON", postal_code:"R5O8H5"}},
  25. {type:"address", data:{address_id: 1757, address: "1177 Ontario St. Unit 8", city:"Mississauga", province:"ON", postal_code:"L9H6B3"}},
  26. {type:"address", data:{address_id: 4536, address: "3945 John St.", city: "Ajax", province: "ON", postal_code: "L7M4T9"}},
  27. {type:"address", data:{address_id: 2473, address: "391 Baker St. Apt 231", city: "Mississauga", province: "ON", postal_code: "M4T8S3"}},
  28. {type:"address", data:{address_id: 1611, address: "183 City Ct.", city: "Hamilton", province: "ON", postal_code: "J3T9V2"}},
  29. {type:"address", data:{address_id: 1836, address: "67 Rhymer Ave.", city: "Stouffville", province: "ON", postal_code: "L3C8H4"}},
  30. {type:"address", data:{address_id: 2727, address: "287 Brant St. Apt 4A", city: "Waterdown", province: "ON", postal_code: "R93G3P"}},
  31. {type:"address", data:{address_id: 5464, address: "11 New St. Apt 2B", city: "Brampton", province: "ON", postal_code: "L694R7"}},
  32. ];
  33.  
  34. /**********************************
  35. * ALL DATA *
  36. * write your CustomerDB Object *
  37. * BELOW this Object *
  38. **********************************/
  39.  
  40. /* Write your CustomerDB Object Here. Do not forget to uncomment the "TEST DATA" section
  41. when you're ready. Your code is required to run against these tests before you submit */
  42.  
  43.  
  44. //var block developed by assignments
  45. var CustomerDB = {
  46. //
  47. store: [],
  48. customer: [],
  49. address: [],
  50.  
  51. insertData : function(object)
  52. {
  53. for (var a in object)
  54. {
  55. switch(object[a].type)
  56. {
  57. case 'store':
  58. this.store.push(object[a].data);
  59. break;
  60. case 'customer':
  61. this.addCustomer(object[a]);
  62. break;
  63. case 'address':
  64. this.address.push(object[a].data);
  65. break;
  66. }
  67. }
  68. },
  69. addCustomer : function(customerObj)
  70. {
  71. //adds it to the lsat value of the list
  72. this.customer.push(customerObj.data);
  73.  
  74. //add date to the newest object added to the array
  75. this.customer[this.customer.length-1].add_date=Date();
  76. },
  77. outputCustomerById : function(customer_id)
  78. {
  79. var AddressInfo;
  80. for (var i=0; i<this.customer.length;i++)
  81. {
  82. if(this.customer[i].customer_id==customer_id)
  83. {
  84. console.log("Customer " + this.customer[i].customer_id + ": " + this.customer[i].first_name + " " + this.customer[i].last_name + " (" + this.customer[i].email+")");
  85.  
  86. AddressInfo = this.getAddressById(this.customer[i].address_id);
  87.  
  88. console.log("Home Address: " + AddressInfo.address + " " + AddressInfo.city + ", " + AddressInfo.province + ". " + AddressInfo.postal_code);
  89. console.log("Joined: " + this.customer[i].add_date + "\n");
  90. }
  91. }
  92. },
  93. outputAllCustomers : function ()
  94. {
  95. for (var i=0; i < this.customer.length; i++)
  96. {
  97. this.outputCustomerById(this.customer[i].customer_id);
  98. }
  99. },
  100. outputCustomersByStore : function(store_id)
  101. {
  102. console.log("Customers in Store: "+ this.getStoreById(store_id).name + "\n");
  103.  
  104. for (var i=0; i<this.customer.length; i++)
  105. {
  106. if (this.customer[i].store_id==store_id)
  107. {
  108. this.outputCustomerById(this.customer[i].customer_id)
  109. }
  110. }
  111. },
  112. removeCustomerById : function (customer_id)
  113. {
  114. for(var i=0;this.customer.length;i++)
  115. {
  116. if(this.customer[i].customer_id == customer_id)
  117. {
  118. this.customer.splice(i,1);
  119. this(removeAddressById(this.customer[i].address_id))
  120. }
  121. }
  122. },
  123. addAddress : function( addressObj )
  124. {
  125. this.address.push(addressObj);
  126. },
  127. getAddressById : function(address_id)
  128. {
  129. //this value returns the current address
  130. var ReturnAddress;
  131.  
  132. //for loop statement to have return value
  133. for (var i=0; i<this.address.length;i++)
  134. {
  135. if (this.address[i].address_id == ReturnAddress)
  136. {
  137. ReturnAddress = this.address[i];
  138. }
  139. }
  140. return ReturnAddress;
  141. },
  142. outputAllAddresses : function()
  143. {
  144. for(var i=0; i<this.address.length; i++)
  145. {
  146. 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)
  147. }
  148. },
  149. removeAddressById: function(address_id)
  150. {
  151. // make sure it does not exist in any other tables
  152. var found = 0;
  153.  
  154. for(var i = 0; i < this.customers.length; i++)
  155. {
  156. if(this.customers[i].address_id == address_id)
  157. {
  158. found = 1;
  159. }
  160. }
  161.  
  162. for(var i = 0; i < this.stores.length; i++)
  163. {
  164. if(this.stores[i].address_id == address_id)
  165. {
  166. found = true;
  167. }
  168. }
  169.  
  170. if(found!=1){ // if it was not found
  171. var newArray = [];
  172. for(var i = 0; i < this.addresses.length; i++){
  173. if(this.addresses[i].address_id != address_id){
  174. newArray.push(this.addresses[i]);
  175. }
  176. }
  177. this.addresses = newArray;
  178. }
  179. },
  180. addStore : function( storeObj )
  181. {
  182. this.address.push(storeObj);
  183. },
  184. getStoreById : function( store_id )
  185. {
  186. //this value returns the current address
  187. var ReturnStore;
  188.  
  189. //for loop statement to have return value
  190. for (var i=0; i<this.address.length;i++)
  191. {
  192. if (this.address[i].address_id == ReturnStore)
  193. {
  194. ReturnStore = this.store[i];
  195. }
  196. }
  197. return ReturnStore;
  198. },
  199. outputAllStores()
  200. {
  201. for (var i=0; i<this.address.length; i++)
  202. {
  203. var addressInfo=this.getAddressById(this.store[i].address_id);
  204. var storeInfo=this.getStoreById(this.store[i].address_id);
  205. console.log ("Store "+ storeInfo.store_id + " - "+storeInfo.name);
  206. console.log ("Location: "+addressInfo.address_id+", "+addressInfo.province+". "+addressInfo.postal_code);
  207. }
  208. }
  209.  
  210. };
  211.  
  212. /**********************************
  213. * TEST DATA *
  214. * write your CustomerDB Object *
  215. * ABOVE this code *
  216. * *
  217. * Uncomment this block of code *
  218. * when you're ready to test *
  219. * your CustomerDB Object *
  220. * *
  221. * You MUST Hand in your code *
  222. * with the test data *
  223. * uncommented, as this will *
  224. * help check your code for *
  225. * correctness *
  226. **********************************/
  227.  
  228.  
  229.  
  230.  
  231. // Insert all Data into the Database
  232.  
  233. CustomerDB.insertData(allData);
  234.  
  235. // output all customers
  236.  
  237. console.log("CustomerDB.outputAllCustomers();\n\n--------------------------\n\n");
  238. CustomerDB.outputAllCustomers();
  239. console.log("--------------------------\n\n");
  240.  
  241. // output all addresses
  242.  
  243. console.log("CustomerDB.outputAllAddresses();\n\n--------------------------\n\n");
  244. CustomerDB.outputAllAddresses();
  245. console.log("--------------------------\n\n");
  246.  
  247. // output all stores
  248.  
  249. console.log("CustomerDB.outputAllStores();\n\n--------------------------\n\n");
  250. CustomerDB.outputAllStores();
  251. console.log("--------------------------\n\n");
  252.  
  253. // output all customers in the "Main Branch"
  254.  
  255. console.log("CustomerDB.outputCustomersByStore(297);\n\n--------------------------\n\n");
  256. CustomerDB.outputCustomersByStore(297);
  257. console.log("--------------------------\n\n");
  258.  
  259. // remove Customer Dave Bennett (customer_id 26) and Martin Scott (customer_id 71)
  260.  
  261. console.log("CustomerDB.removeCustomerById(26);\nCustomerDB.removeCustomerById(71);\n\n");
  262. CustomerDB.removeCustomerById(26);
  263. CustomerDB.removeCustomerById(71);
  264. console.log("--------------------------\n\n");
  265.  
  266. // output all customers again
  267. // NOTE: Dave Bennett and Martin Scott should be missing
  268.  
  269. console.log("CustomerDB.outputAllCustomers();\n\n--------------------------\n\n");
  270. CustomerDB.outputAllCustomers();
  271. console.log("--------------------------\n\n");
  272.  
  273. // output all addresses again
  274. // NOTE: only addrss 287 Brant St. Apt 4A Waterdown, ON. R93G3P should be missing
  275.  
  276. console.log("CustomerDB.outputAllAddresses();\n\n--------------------------\n\n");
  277. CustomerDB.outputAllAddresses();
  278. console.log("--------------------------\n\n");
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement