design patterns - Data structure to represent a shopping cart -


what correct data structure use represent shopping cart contents?

  1. linked list
  2. array
  3. array list
  4. something else

im not thinking in terms of convenient i.e. use array, more in terms of correct.

this standard idea of shopping cart contain following:

  1. basic meta data cart such subtotal etc
  2. collection of cart items each containing properties cart information such qty , price , product property contains access underlying product

also following important:

  1. items should able added , removed
  2. the cart should able emptied in single action
  3. certain parts of meta data should updated when items added/removed such subtotal, qty etc.

  • array: wouldn't because have know how many items go in shopping cart before hand , force resize/re-initialize array many times.
  • linkedlist (which don't use array in implementation): go fits rest of requirements mentioned.
  • hashed collection: used not suitable situation given fact fast access contents of basket key element not required.

bottom of line, model shopping cart developers order list of order items, have shopping cart class getter total number of items, total price, etc , model cotents using linked list.

of course might want consider distribution if factor situations above should more enough.


Comments

Popular posts from this blog

image - ClassNotFoundException when add a prebuilt apk into system.img in android -

I need to import mysql 5.1 to 5.5? -

Java, Hibernate, MySQL - store UTC date-time -