Created
May 28, 2019 03:51
-
-
Save Joejhorn/1e02e4d2f6796e528bce29ae25acd114 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#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) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Great job!
Here's some feedback based on my own opinions:
Thoughts
Bonus
Psuedo code out this system utilizing one or more
Class
objectsFeedback
Naming nits + suggestions:
get_amount(items)
#=>get_order_sum(items)
items
no better thanx
#=>order_items
at minimum helps specificityget_taxes
returns a single sum #=>get_tax
reduces expectationsget_items
does unless I read through itCode 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.taxes
print_menu
print_menu
andprices
have a dependency. It'll be difficult to maintain changes between the two.print_menu
could benefit from a string expressioncheck_if_valid
print_menu
get_items
Test
Financial calculations can be fickle - This system could benefit from a few unit tests to ensure no rounding errors are occurring.
If
Prices
andTaxes
are extracted, you could easily do some more intense calculations within your tests through by injecting objects for testing.Suggested Features / UX improvements
Taco
andBurRiTo
should both work