Coder Social home page Coder Social logo

green_grocer-nyc-web-071618's Introduction

Objectives

Create a checkout method to calculate the total cost of a cart of items and apply discounts and coupons as necessary.

Dr. Steve Bruhle, your green grocer, isn't ready, but you are!

Instructions

Implement a method checkout to calculate total cost of a cart of items and apply discounts and coupons as necessary. The checkout method will rely on the consolidate_cart, apply_coupons, and the apply_clearance methods.

The consolidate_cart method

The cart starts as an array of individual items. Translate it into a hash that includes the counts for each item with the consolidate_cart method.

For instance, if the method is given the array below:

[
  {"AVOCADO" => {:price => 3.0, :clearance => true }},
  {"AVOCADO" => {:price => 3.0, :clearance => true }},
  {"KALE"    => {:price => 3.0, :clearance => false}}
]

then the method should return the hash below:

{
  "AVOCADO" => {:price => 3.0, :clearance => true, :count => 2},
  "KALE"    => {:price => 3.0, :clearance => false, :count => 1}
}

The apply_coupons method

If the method is given a cart that looks like this:

{
  "AVOCADO" => {:price => 3.0, :clearance => true, :count => 3},
  "KALE"    => {:price => 3.0, :clearance => false, :count => 1}
}

and a coupon for avocados that looks like this:

{:item => "AVOCADO", :num => 2, :cost => 5.0}

then apply_coupons should return the following hash:

{
  "AVOCADO" => {:price => 3.0, :clearance => true, :count => 1},
  "KALE"    => {:price => 3.0, :clearance => false, :count => 1},
  "AVOCADO W/COUPON" => {:price => 5.0, :clearance => true, :count => 1},
}

Notice how there were three avocados in the cart, but the coupon only applied to two of them. This left one un-couponed avocado in the cart at $3.00 and one "bundle" of discounted avocados totalling $5.00.

The apply_clearance method

This method should discount the price of every item on clearance by twenty percent.

For instance, if this method was given this cart:

{
  "PEANUTBUTTER" => {:price => 3.00, :clearance => true,  :count => 2},
  "KALE"         => {:price => 3.00, :clearance => false, :count => 3}
  "SOY MILK"     => {:price => 4.50, :clearance => true,  :count => 1}
}

it should return a cart with clearance applied to peanutbutter and soy milk:

{
  "PEANUTBUTTER" => {:price => 2.40, :clearance => true,  :count => 2},
  "KALE"         => {:price => 3.00, :clearance => false, :count => 3}
  "SOY MILK"     => {:price => 3.60, :clearance => true,  :count => 1}
}

The checkout method

Create a checkout method that calculates the total cost of the consolidated cart.

When checking out, follow these steps in order:

  • Apply coupon discounts if the proper number of items are present.

  • Apply 20% discount if items are on clearance.

  • If, after applying the coupon discounts and the clearance discounts, the cart's total is over $100, then apply a 10% discount.

Named Parameters

The method signature for the checkout method is consolidate_cart(cart:[]). This, along with the checkout method uses a ruby 2.0 feature called Named Parameters.

Named parameters give you more expressive code since you are specifying what each parameter is for. Another benefit is the order you pass your parameters doesn't matter! checkout(cart: [], coupons: []) is the same as checkout(coupons: [], cart: [])

Resources

Reward

View Green Grocer on Learn.co and start learning to code for free.

green_grocer-nyc-web-071618's People

Contributors

kthffmn avatar octosteve avatar ahimmelstoss avatar annjohn avatar fs-lms-test-bot avatar pletcher avatar deniznida avatar gj avatar sarogers avatar fislabstest avatar gs2589 avatar

Watchers

 avatar Rishikesh Tirumala avatar James Cloos avatar  avatar Victoria Thevenot avatar  avatar Joe Cardarelli avatar Katie Burke avatar Sara Tibbetts avatar  avatar Sophie DeBenedetto avatar  avatar Antoin avatar Alex Griffith avatar  avatar Amanda D'Avria avatar  avatar Nicole Kroese  avatar Lisa Jiang 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.