GithubHelp home page GithubHelp logo

programming-univbasics-nds-green-grocer-part-1-atx01-seng-ft-040521's Introduction

Green Grocer

Learning Goals

  • Translate data from AoH to AoH

Introduction

Over the last several lessons, we've explored nested data structures and how they can model complex real-world situations. Now, in this lab and the next, we're going to take everything we've learned and use it to program a grocery store checkout process.

You're going to have to integrate everything you've learned to this point, repetition, conditional flow control, analyzing NDS, processing NDS, producing insights. On the other side of this challenge you're going to know that you know how to construct and write procedural style programs in Ruby.

In fact, and we're hesitant to say it, you're going to be irritated with us. "Why is there so much boilerplate code, why are there so many while...do...ends?" That's exactly where we want you to be. In the coming lessons, we'll show you refinements to clunky work that will make you and your tired fingers so happy...but you'll appreciate them and what they do in light of this one last challenge.

So let's get to it!

Shopping

Think for a moment about what it's like to shop at a grocery store. As you walk through the store, you put the items you want to buy into your cart. Your cart, then, becomes a collection of grocery items. Every one of those grocery items has specific attributes: for example, its price and whether or not it's on clearance. You can also have multiples of the same item in your cart, and chances are they will be all mixed in no particular order or structure.

Stop and Reflect: When you read the paragraph above, did you hear the suggestion of an Array for a collection? Did the word attributes suggest to you an attribute/value pair, like a Hash? If that's not happening for you at this moment, you should go back and review the first 4 lessons of this section.

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

Stop and Reflect: You should recognize that there's a transformation happening between the NDS that represents "things in the cart" to the NDS that represents a "consolidated" receipt." You should be thinking about Steps 2, 3, and 4 of the NDS process we taught you. If that's not sounding familiar, review the lessons where we practice transforming NDS. Make sure your foundational skills are ready for this challenge!

In this lab and the next, your task is to write a set of methods to handle the different pieces of the checkout process. For this first lab, we'll focus on two methods first - find_item_by_name_in_collection and consolidate_cart.

find_item_by_name_in_collection

The first method, find_item_by_name_in_collection, simulates the act of adding items to your shopping cart. Again, if we think of grocery items as having attributes like a Hash, a shelf in a grocery store might contain a collection of various items. We could represent this as an Array of Hashes:

grocery_shelf = [
  { :item => "CANNED BEANS", :price => 3.00, :clearance => true },
  { :item => "CANNED CORN", :price => 2.50, :clearance => false },
  { :item => "SALSA", :price => 1.50, :clearance => false },
  { :item => "TORTILLAS", :price => 2.00, :clearance => false },
  { :item => "HOT SAUCE", :price => 1.75, :clearance => false }
]

find_item_by_name_in_collection should be able to take the string name of a particular item and a collection of items like we have above. It should then iterate over the collection and check the value of :item in each nested hash. If a match is found, the method should return that entire hash. For instance, if we ran:

find_item_by_name_in_collection("CANNED CORN", grocery_shelf)

We would expect the following in return:

{ :item => "CANNED CORN", :price => 2.50, :clearance => false}

If an item is not found, find_item_by_name_in_collection should return nil.

find_item_by_name_in_collection("CHOLULA", grocery_shelf)
 # => nil

Get this method passing before moving on - the later tests use this method to build example carts for testing.

consolidate_cart

Imagine you've arrived at the checkout stand with your cart - the first thing your cashier will do (or you will do in a self-checkout) is take items one at a time and scan them. Every item you select and scan an item, it will be added to a record that will eventually be your receipt.

Regardless of the order of items you scan, the checkout machine will tally up the total count of each item, displaying a quantity alongside the item information. The consolidate_cart method should do something similar - it should take in a 'cart', an array of hashes of individual items and return a new array of hashes, but with each item containing a quantity. So, for example, given the following cart:

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

If we passed this into consolidate_cart:

consolidate_cart(unconsolidated_cart)

We should expect the following in return:

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

Notice that instead of listing the Avocado hash twice, we now have a single hash for Avocado with a :count set to 2. Since we only had one Kale hash, it has a :count set to 1.

Hint: The find_item_by_name_in_collection can be used to check if items exist in any array of hashes, whether it be a 'grocery shelf' or the cart your are in the process of consolidating.

Check in with tests often to make sure your method is on track.

Note: If you want to run the tests about consolidate_cart, you can run them by invoking rspec spec/grocer_spec.rb:27. If you look at the spec/grocer_spec.rb file, you'll see that all the consolidate_cart tests are in a describe block starting on line 27. This can help your output come out in a digestible form.

Conclusion

With these two methods, find_item_by_name_in_collection and consolidate_cart, we have designed the first half our program. Already, it can do some neat things - we can use find_item_by_name_in_collection to build out an array from existing hash data, and then use consolidate_cart to restructure that data into a more useful format.

With our carts consolidated, we can move on to part two. In the second Green Grocer lab, we will be implementng functionality to apply coupons and clearance, and finally to tally up a total cost.

Resources

programming-univbasics-nds-green-grocer-part-1-atx01-seng-ft-040521's People

Contributors

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

Watchers

Kaitlin Vignali avatar  avatar Victoria Thevenot avatar raza jafri avatar  avatar Joe Cardarelli avatar  avatar  avatar Ben Oren avatar Matt avatar Alex Griffith avatar  avatar Amanda D'Avria avatar  avatar Ahmed avatar Nicole Kroese  avatar Dominique De León avatar  avatar Vicki Aubin 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.