Giter VIP home page Giter VIP logo

dsc-1-07-07-object-oriented-shopping-cart-lab-online-ds-pt-041519's Introduction

Object Oriented Shopping Cart - Lab

Introduction

In this lab we will be mimicing the functionality of a shopping cart with our knowledge of object oriented Python. Our shopping cart will be able to add items of different quantities and prices to our cart, calculate discounts, keep track of what items have been added, and void transactions.

Objectives

You will be able to:

  • Build a class with instance methods
  • Call instance methods inside of other instance methods
  • Use instance methods to track information pertinent to an instance of a class

Instructions

In this lab, you'll practice your object orientated programming skills by modifying the shopping_cart.py file.

To start, we'll set this notebook to autoreload packages so that when you update shopping_cart.py, you can experiment with the effects here. Remember that while the package will be reloaded, you will need to reinitialize your class instance. Here's an example to get you started:

%load_ext autoreload
%autoreload 2
#Import our custom package
from shopping_cart import ShoppingCart
#Initialize an instance of our shopping cart class
shopping_cart = ShoppingCart()

Add an Initialization Behavior to the ShoppingCart Class

Update your shopping_cart.py file to include an init method. This should define three default attributes: 'total', which should be set to 0, 'employee_discount', set to None and 'items', set to a blank list. The line of code below should work and produce the previewed output once you do this.

shopping_cart = ShoppingCart() #Add a line to reinitialize an instance of the class
print(shopping_cart.total)
print(shopping_cart.employee_discount)
print(shopping_cart.items)
0
None
[]

Add and add_item() method.

Define an instance method called add_item that will add an item to our cart. It should take in the name of an item, its price and an optional quantity. The method should increase the shopping cart's total by the appropriate amount and return the new total for the shopping cart.

hint: think about how you would like to keep this information in your list of items. Can we imagine wanting to ever check the price of an individual item after we've added it to our cart? What data type do we know of that can associate the item name with it's price?

shopping_cart.add_item("rainbow sandals", 45.99) # 45.99
45.99
shopping_cart.add_item("agyle socks", 10.50) # 56.49
56.49
shopping_cart.add_item("jeans", 50.00, 3) # 206.49
206.49

Add Summary Methods mean_item_price() and median_item_price()

Define two more instance methods: mean_item_price and median_item_price, which should return the average price per item and the median price of the items in your cart, respectively.

Remember: the mean is the average price per item and to find the median we must do three things:

  • First put all numbers in our list in ascending order (smallest to greatest)
  • Then check to see if there is an odd number of elements in our list. If so, the middle number is the median
  • Finally, if there is an even number of elements in the list, the median will be the average or mean of the two center elements (e.g. given the list [1,2,3,4] the elements 2 and 3 are the two center elements and the median would be (2 + 3)/2 or 2.5).
shopping_cart.mean_item_price() # 41.29
41.298
shopping_cart.median_item_price() # 50.00
50.0

Add an apply_discount method

Now, let's define an instance method called apply_discount that applies a discount if one is provided and returns the discounted total. For example, if we initialize a new shopping cart with a discount of 20% then our total should be discounted in the amount of 20%. So, if our total were $100, after the discount we only would owe $80.

If our shopping cart does not have an employee discount, then it should return a string saying: "Sorry, there is no discount to apply to your cart :("

discount_shopping_cart = ShoppingCart(20)
print(discount_shopping_cart.add_item("rainbow sandals", 45.00)) # 45.00
print(discount_shopping_cart.add_item("agyle socks", 15.00)) # 60.00
print(discount_shopping_cart.apply_discount()) # 48.00
print(discount_shopping_cart.add_item("macbook air", 1000.00)) # 1060.00
print(discount_shopping_cart.apply_discount()) # 848.00
print(shopping_cart.apply_discount()) # Sorry, there is no discount to apply to your cart :(
45.0
60.0
48.0
1060.0
848.0
Sorry, there is no discount to apply to your cart :(

Add a void_last_item() method

Finally, we are missing one piece of functionality. What if we just accidentally added something to our cart or decided that this item is too expensive for our budget? Let's define a method called void_last_item that removes the last item from our shopping cart and updates its total. If there are no items in the shopping cart, void_last_item should return "There are no items in your cart!".

shopping_cart.void_last_item()
shopping_cart.total # 156.49
156.49

Summary

In this lab, we practiced using instance methods to mimic the functionality of a shopping cart as well as defined methods that give us the mean and median prices of all the items in our cart.

dsc-1-07-07-object-oriented-shopping-cart-lab-online-ds-pt-041519's People

Contributors

mathymitchell avatar loredirick avatar sanpietro 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.