Giter Club home page Giter Club logo

node_shopping_cart's Introduction

Node Shopping Cart

A basic shopping cart implementation as command-line application using NodeJS. This application executes multiple shopping carts and prints the total amount for each cart based on the given JSON file which contains details:

  • Inventory: List of unique items available in the shop
  • Rules: List of unique discount rules applicable on any possible item
  • Carts: List of multiple carts which will be checked out

This application has been built upon the requirements detailed in the following gist: DiUS Shopping Coding Exercise.

Using the application

To run the application use the following command:

node index.js data_set/test.json

Based on the given test.json, you can write your own data set to execute on this application.

To run the test cases use the following command:

npm run test

Implemenation Details

For the implementation I have used a TDD style of development, where I have written unit test cases for each feature I have implemented.

Design Approach

The application contains 3 main entities/objects:

  • Inventory: The inventory object contains a map of key value pairs, where the key is the sku of a given item and value is the item object derived from the data set input. The inventory maintains the list of unique items only.
  • Rules: The rules objects contains a map of key value pairs, where the key is the sku of the item on which a specific discount rule needs to applied and the value is the object with an applyRule function for the specific discount rule. Here I have tried to apply a Strategy Type Design Pattern to make sure checkout is unaware of the rule specifics. Same rule can be applied to multiple items, but multiple rules cannot be applied to same item. There are 3 types of rules that has been defined:
    • Bulk Order Rule: If quantity of the defined item in cart is more than minimum quantity in the rule then discounted price is applicable.
    • Free Item Rule: If an item is to be given for free with another item and if both items are in cart, then total amount of free item will be 0 if quantity <= another item, otherwise total amount of free item will be (free item quantity - item quantity) * (free item price).
    • Three for Two Rule: If quanitiy of the defined item in cart is 3, then total amount applicable for that item will be that of 2 only.
  • Checkout: The checkout object takes the rules and inventory objects as arguments and scans the items in cart and maintains an internal cart where total amount of each item is calculated and discount rules applied. When total amount is asked it prints the total amount and flushes the internal cart.

Environment Details

  • For the development of this application, I have used Javascript with ES5 conventions using the NodeJS environment.
  • For writing and executing test cases for the developed code, I have used mocha, chai and sinon. I have used mocha for setting and executing test cases, chai for expect rules and sinon for sniffing console logs.

node_shopping_cart's People

Contributors

dhirajchawla2000 avatar

Watchers

 avatar

Forkers

vikassach

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.