GithubHelp home page GithubHelp logo

shopping_basket's Introduction

Shopping Cart Programming Exercise

ProductStore

Products are stored in a ProductStore object. This can either be created from a list of product dictionaries or initialized from a JSON file.

from product import ProductStore

products = [
	{"name": "Baked Beans" ,"price": 0.99},
	{"name": "Biscuits" ,"price": 1.2},
	{"name": "Sardines" ,"price": 1.89},
	{"name": "Shampoo Small" ,"price": 2},
	{"name": "Shampoo Medium" ,"price": 2.5},
	{"name": "Shampoo Large" ,"price": 3.5}
]

product_store = ProductStore(products)

# or

product_store = ProductStore.load_products(json_products_file)

get_product_price() to get the product price

price = product_store.get_product_price('product')

Cart

Carts can be created by passing the ProductStore Instance as an argument

from cart import Cart

my_cart = Cart(product_store)

Products can be added to a cart by name

cart.add('Biscuits')
cart.add('Sardines', 3)

get_total() to get the cart total Also a list of Offer Class objects can be passed to the method to apply offers on Cart total

total = cart.get_total()

# with offers
total_with_offers = cart.get_total(offers=[offer_one, offer_two, offer_three])

Offers

Offer classes inherit from AbstractOffer and must implement the calculate_line_total() method.

Two example offer classes are provided; MultiBuyOffer, and DependentDiscountOffer.

MultiBuyOffer

But a certain quantity of products and get one free

# buy one get one free on strawberries
buy_one_free = MultiBuyOffer(product, 1, 1)

# buy two get third free on product
multibuy = MultiBuyOffer(product, 2, 1)

DependentDiscountOffer

Discount can be applied to a product on purchase of another product. Discount can also be applied on a single product

dep_discount_offer = DependentDiscountOffer('snickers bar', 'mars bar', Decimal('0.2'))
discount_product = DependentDiscountOffer('snickers bar', 'snickers bar', Decimal('0.2'))

To run unittest cases for the shopping cart, run the below command from command prompt python -m unittest tests.py

shopping_basket's People

Contributors

vengalrao-eppa avatar

Watchers

 avatar  avatar

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.