Created
May 28, 2019 03:51
-
-
Save Joejhorn/1e02e4d2f6796e528bce29ae25acd114 to your computer and use it in GitHub Desktop.
This file contains hidden or 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
ClassobjectsFeedback
Naming nits + suggestions:
get_amount(items)#=>get_order_sum(items)itemsno better thanx#=>order_itemsat minimum helps specificityget_taxesreturns a single sum #=>get_taxreduces expectationsget_itemsdoes unless I read through itCode Cleanup
pricesAs 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
pricesobject should be extracted out.get_amountshould not inherently know what the menu object is.taxesprint_menuprint_menuandpriceshave a dependency. It'll be difficult to maintain changes between the two.print_menucould benefit from a string expressioncheck_if_validprint_menuget_itemsTest
Financial calculations can be fickle - This system could benefit from a few unit tests to ensure no rounding errors are occurring.
If
PricesandTaxesare extracted, you could easily do some more intense calculations within your tests through by injecting objects for testing.Suggested Features / UX improvements
TacoandBurRiToshould both work