Giter Site home page Giter Site logo

ruby-enumerables-hash-practice-green-grocer-lab-atlanta-web-021720's Introduction

Green Grocer

Learning Goals

  • Access and iterate over hashes
  • Translate data from arrays to hashes
  • Translate data from hashes to other hashes
  • Count repeat items in a hash
  • Perform calculations based on hash data

Introduction

In this lab, we're going to simulate a grocery store checkout process. In most modern grocery stores, a customer adds to a grocery cart as they walk through the store. A cart can be thought of as a collection of grocery items. Each grocery item has specific attributes, such as a sale price, or whether or not its on clearance. There may be multiples of the same item in the cart, mixed together in no particular order.

When rung up at checkout, however, the customer would expect to get a receipt with all of their items listed, the quantity of each item purchased, any coupons or discounts that were applied, and the total of all items in the cart.

Grocery Receipt

Your task in this lab is to write a set of methods to handle the different pieces of the checkout process.

Instructions

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

Write 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.00, :clearance => true }},
  {"AVOCADO" => {:price => 3.00, :clearance => true }},
  {"KALE"    => {:price => 3.00, :clearance => false}}
]

then the method should return the hash below:

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

Write the apply_coupons Method

Now that we have a way to consolidate carts, we can write some additional methods to work with the consolidated cart data, starting with apply_coupons. This method takes in two arguments, a consolidated cart and an array of coupons, and applies the coupons to items in the cart, if appropriate. So, for instance, the apply_coupons method might take in a consolidated cart that looks like this:

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

and an array containing a single coupon for avocados that looks like this:

[{:item => "AVOCADO", :num => 2, :cost => 5.00}]

then apply_coupons should return the following hash:

{
  "AVOCADO" => {:price => 3.00, :clearance => true, :count => 1},
  "KALE"    => {:price => 3.00, :clearance => false, :count => 1},
  "AVOCADO W/COUPON" => {:price => 2.50, :clearance => true, :count => 2},
}

In this case, we have a 2 for $5.00 coupon, but 3 avocados counted in the consolidated cart. Since the coupon only applies to 2 avocados, the cart shows there is one remaining avocado at $3.00 and a count of 2 discounted avocados.

As we want to be consistent in the way our data is structured, each item in the consolidated cart should include the price of one of that item. Even though the coupon states $5.00, since there are 2 avocados, the price is listed as $2.50.

Write the apply_clearance Method

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

For instance, if the apply_clearance method was given this cart:

{
  "PEANUT BUTTER" => {: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 peanut butter and soy milk:

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

Hint: you may find the Float class' built in round method to be helpful here to make sure your values align.

Write the checkout Method

Create a checkout method that calculates the total cost of the consolidated cart. Just like a customer arriving at a register with mixed up cart, this method is given an array of unsorted items. In addition to an array of items, checkout is also given an array of coupons.

The checkout method will need to utilize all the previous methods we've written. It consolidates the cart, applies coupons, and applies discounts. Then, it totals the cost of the entire cart, accounting for each item and their prices, and returns this value.

When writing this method, make sure to address each step in the proper order:

  • Consolidate the cart array into a hash

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

  • Apply 20% discount if items are on clearance

In addition to coupons and clearance, our grocer store offers a deal for customers buying lots of items: if, after all coupons and discounts, the cart's total is over $100, the customer gets an additional 10% off. Apply this discount when appropriate.

Conclusion

Utilizing arrays and hashes is key when working with a lot of data. With our knowledge of iteration and data manipulation, we can do all sorts of things with this data. We can build methods that access that data and modify only what we want. We can extract additional information, as we did here calculating a total. We can take data that isn't helpful to us and restructure it to be exactly what we need.

Reward

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

ruby-enumerables-hash-practice-green-grocer-lab-atlanta-web-021720's People

Contributors

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

Watchers

Kevin McAlear avatar  avatar  avatar Victoria Thevenot avatar Belinda Black avatar Bernard Mordan avatar raza jafri avatar  avatar Joe Cardarelli avatar  avatar Sophie DeBenedetto avatar  avatar Antoin avatar Alex Griffith avatar  avatar Amanda D'Avria avatar  avatar Nicole Kroese  avatar Kaeland Chatman avatar Lisa Jiang avatar Vicki Aubin avatar  avatar  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.