Created
November 16, 2013 22:25
-
-
Save sdanko11/7506171 to your computer and use it in GitHub Desktop.
Cashier Problem 2
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
| def error_message | |
| puts "Error invalid amount of money exiting program" | |
| end | |
| def number_check(number) | |
| number =~ /^\d+(\.\d{0,2})?$/ | |
| end | |
| def letter_check(word) | |
| word =~ /[a-zA-Z]/ | |
| end | |
| def total_calculation(amount_tendered, total_price) | |
| amount_due = amount_tendered.to_f - total_price | |
| if amount_due >= 0 | |
| puts '===================' | |
| puts"Change Due: $#{"%.2f"%amount_due}" | |
| else | |
| amount_still_owed = amount_due * -1 | |
| puts puts '===================' | |
| puts "The customer still owes: $#{"%.2f"%amount_still_owed}" | |
| end | |
| end | |
| item_total=[] | |
| number_check=0 | |
| puts "What is the price of the item?" | |
| item_price=gets.chomp | |
| while true | |
| if number_check(item_price) == 0 | |
| item_total.push(item_price.to_f) | |
| total_price = item_total.inject() {|sum, price| sum + price } | |
| puts "Total Price: $#{"%.2f"%total_price}" | |
| puts "What is the sale price" | |
| item_price=gets.chomp | |
| else number_check(item_price) == nil | |
| break | |
| end | |
| end | |
| if (item_total.length > 0 && letter_check(item_price) != 0) || item_price == "Done" || item_price == "done" | |
| puts "==============" | |
| puts "Total Price: $#{"%.2f"%total_price}" | |
| puts "Here are your list of items:" | |
| puts item_total | |
| puts "What is the amount tendered?" | |
| amount_tendered=gets.chomp.to_s | |
| end | |
| if number_check(amount_tendered) == 0 | |
| total_calculation(amount_tendered, total_price) | |
| puts Time.now.strftime("%m/%d/%Y") | |
| puts Time.now.strftime("%I:%M%p") | |
| else | |
| number_check(amount_tendered) == nil | |
| error_message | |
| end |
A good habit to get into is to try and create methods that help isolate logic/computation and make the code easier to read and understand.
This is sometimes even appropriate when the code that will be in the method is only one line long. An example of this would be changing line 33 to take advantage of a calculate_total method like this:
def calculate_total(item_prices)
item_prices.inject(0) { |sum, price| sum + price }
end
# ...
total_price = calculate_total(item_prices)
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
I'd also recommend to spend time to make sure that variable names are very good descriptions of their value. For instance, the variable
total_priceis somewhat misleading since it does not actually hold the value of the total price. Instead, this would be better if it were named something likeitem_pricesorprices, signifying that it is a collection of more than one price.This will make the code much easier for other developers to read and understand.