DEV Community

Discussion on: Daily Challenge #56 - Coffee Shop

ben profile image
Ben Halpern • Edited on

I feel it isn't super explicit that people can only order one drink here, so maybe the function should account for that, eh?

In Ruby

DRINK_COSTS = { "Americano" => 2.2, "Latte" => 2.3, "Flat white" => 2.4, "Filter" => 3.50 }.freeze

def buy(drinks, cash)
    costs = { |drink| DRINK_COSTS[drink] }
    response = "Sorry, exact change only, try again tomorrow!"
    response = if costs == cash && drinks.size > 1
                   "Here are your beverages, have a nice day!"
               elsif costs == cash
                   "Here is your #{drinks.first}, have a nice day!"

This would expect drinks to be an array of strings. We could also do something were we could accept either a string or an array with something like drinks = [drinks].flatten where we put the result in an array and then flatten it each time. That way we don't have to worry about the type we get (since we aren't checking for it due to this being Ruby).

dak425 profile image
Donald Feury

I had actually thought about that myself but chose not to consider it at the moment. I might go back and tweak mine to take a collection of drinks as well.