GithubHelp home page GithubHelp logo

programming-univbasics-nds-green-grocer-part-2-sgharms-test-webdev-fund's Introduction

Green Grocer

Learning Goals

  • Translate data from AoH to AoH
  • Perform calculations based on AoH data

Introduction

In the last lab, we built out a couple of methods for simulating a grocery shopping experience. We built a method for finding items in an Array of Hashes, find_item_by_name_in_collection, then a second method, consolidated_cart. consolidated_cart takes an Array of Hashes of single grocery items and creates a new AoH that includes quantities for each item.

In this lab, we're going to finish the work we've started by writing three more methods. The first two methods, apply_coupons and apply_clearance, will apply discounts to a cart that has already been consolidated - one applies coupons, and the other applies discounts for clearance items.

The final method, checkout, will bring the entire process together - it will run the consolidated_cart method we wrote in part 1 of this lab as well as apply_coupons and apply_clearance. The checkout method will need to take in an unconsolidated cart, consolidate it, apply coupons, and apply discounts. Finally, the checkout method will total up the cost of all items, apply a 'total price' discount if applicable, and return the final cost.

Recalling from the previous lab, when you pay for all your items at the checkout, you expect to get a "consolidated" receipt that:

  • lists all of the items you bought
  • lists how many of each item you bought
  • accounts for any coupons or discounts per item
  • applies any "total price" discounts

We've covered the first two items in this list with consolidated_cart. Let's get started on the last two.

Instructions

Complete your solution in lib/grocer_part_2.rb. Since this is part 2 of two labs, a solution to part 1 is provided in lib/part_1_solution.rb. This file is required at the top of lib/grocer_part_2.rb, so you will be able to access and use the existing consolidated_cart method in your solution code. The tests from the previous lab are included and currently passing.

Write the apply_coupons Method

  • Arguments:
    • Array: a collection of item Hashes
    • Array: a collection of coupon Hashes
  • Returns:
    • A new Array. Its members will be a mix of the item Hashes and, where applicable, the "ITEM W/COUPON" Hash. Rules for application are described below.

Example:

Given the following consolidated cart:

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

and an Array with a single coupon:

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

then apply_coupons should change the first Array to look like:

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

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

Note: We want to be consistent in the way our data is structured, so each item in the consolidated cart should include the price of one of that item. For example, even though the coupon states $5.00—because there are 2 avocados—the price is listed as $2.50.

Write the apply_clearance Method

  • Arguments:
    • Array: a collection of item Hashes
  • Returns:
    • a new Array where every unique item in the original is present but with its price reduced by 20% if its :clearance value is true

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

Example:

Given the following consolidated cart:

[
  {:item => "PEANUT BUTTER", :price => 3.00, :clearance => true,  :count => 2},
  {:item => "KALE", :price => 3.00, :clearance => false, :count => 3},
  {:item => "SOY MILK", :price => 4.50, :clearance => true,  :count => 1}
]

apply_clearance should update the cart with clearance applied to PEANUT BUTTER and SOY MILK:

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

Sometimes, these operations can lead to numbers with many decimal places. The Float class' built-in round method will be helpful here to make sure your values align. 2.4900923090909029304 becomes 2.5 if we use it like so: 2.4900923090909029304.round(2)

Write the checkout Method

  • Arguments:
    • Array: a collection of item Hashes
    • Array: a collection of coupon Hashes
  • Returns:
    • Float: a total of the cart

Here's where we stitch all our work together. Given a "cart" Array, the first argument, we should first create a new consolidated cart using the consolidate_cart method.

We should pass the newly consolidated cart to apply_coupons and then send it to apply_clearance. With all the discounts applied, we should loop through the "consolidated and discounts-applied" cart and multiply each item Hash's price by its count and accumulate that to a grand total.

As one last wrinkle, our grocery store offers a deal for customers buying lots of items. If, after the coupons and discounts are applied, the cart's total is over $100, the customer gets an additional 10% off. Apply this discount when appropriate.

Conclusion

When working with a lot of data, utilizing arrays and hashes is key. 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. Most importantly, we can process this data in a way that lets us extract relevant insights that have meaning in the real world. The better we can structure our programs to represent people and the actions they need to perform, the easier we can make our programs necessary to users.

Resources

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

programming-univbasics-nds-green-grocer-part-2-sgharms-test-webdev-fund's People

Contributors

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

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  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.