Skip to content

Instantly share code, notes, and snippets.

@Joejhorn
Created May 28, 2019 03:51
Show Gist options
  • Save Joejhorn/1e02e4d2f6796e528bce29ae25acd114 to your computer and use it in GitHub Desktop.
Save Joejhorn/1e02e4d2f6796e528bce29ae25acd114 to your computer and use it in GitHub Desktop.
#take array of items, use hash table to look up prices and return prices as a float
def get_amount(items)
total = 0.00
prices = {
"taco" => 6.00,
"burrito" => 7.00,
"coke" => 1.25,
"iced" => 1.50
}
items.each do |item|
total += prices[item].to_f
end
return total
end
#get amount plus tax
def get_taxes(amount)
return amount * 1.0825
end
#print menu
def print_menu
puts "taco - $6, burrito - $7, coke - $1.25, iced - $1.50"
end
#check to see if input is valid
def check_if_valid(item)
valid = ["taco", "burrito", "coke", "iced"]
valid.include?(item)
end
#get items until exit, return array of items
def get_items
items = []
loop do
print "Enter Item: "
selection = gets.chomp
if selection == "exit"
break
elsif check_if_valid(selection)
puts "Input Accepted"
items << selection
else puts "No Item Available"
end
end
items
end
#main
print_menu
get_taxes(get_amount(get_items)).round(2)
@jacobmoyle
Copy link

jacobmoyle commented May 29, 2019

Great job!

Here's some feedback based on my own opinions:

Thoughts

Don't need to actually do these

  • How could this be built to support different currencies?
    • Regions/tax codes?
  • What menus change, amounts change. Does this system support easy updates without requiring logic updates?

Bonus

Psuedo code out this system utilizing one or more Class objects

Feedback

Naming nits + suggestions:

  • get_amount(items) #=> get_order_sum(items)
  • I find items no better than x #=> order_items at minimum helps specificity
  • get_taxes returns a single sum #=> get_tax reduces expectations
  • No idea what get_items does unless I read through it

Code Cleanup

prices

As a human I wonder: What tax is this?
As a programmer I wonder: Is this pattern extensible if we need to add additional tax types

  • prices object should be extracted out. get_amount should not inherently know what the menu object is.
  • extract taxes
print_menu
  • print_menu and prices have a dependency. It'll be difficult to maintain changes between the two.
  • print_menu could benefit from a string expression
check_if_valid
  • has the same 'source of truth' issues as print_menu
get_items
  • may syntactically benefit from a switch statement
  • Could probably split into two methods

Test

Financial calculations can be fickle - This system could benefit from a few unit tests to ensure no rounding errors are occurring.

If Prices and Taxes are extracted, you could easily do some more intense calculations within your tests through by injecting objects for testing.

Suggested Features / UX improvements

  • As a user I'd like to print out available commands
  • As a user I'd like to remove an item
  • As a user I'd like to re-print the menu
  • As a user I'd like to see my updated total after adding or removing an item
  • As a user I'd like to clear my order
  • As a user I'd like to print an itemized receipt after finalizing my order
  • As a user I'd like to input case insensitive orders -> Taco and BurRiTo should both work

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment