-
-
Save sdanko11/7506171 to your computer and use it in GitHub Desktop.
| 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 |
You could clean up all of the places where you're printing out a formatted dollar amount by creating a method that converts a float to a formatted version of itself like this:
def formatted_money(amount)
"$#{sprintf("%.2f", amount)}"
endYou would then replace all the places where you're repeating that formatting with something like this:
puts "Change Due: #{formatted_money(amount_due)}"Using a method like this means that if you ever needed to change the formatting of the output, you would only need to make the change in one place. As an additional benefit, it's also easier to read what's going on without having to spend any brain power trying to understand how the output is being formatted.
On line 42, you could make the checking for 'done' case insensitive by downcasing the input like this:
# The current way you have it
item_price == "Done" || item_price == "done"
# A more robust case insensitivity
item_price.downcase == 'done'You could eliminate the duplication between lines 27-28 and 35-36 by refactoring the while loop to be as follows:
item_total = []
number_check = 0
while true
puts "What is the sale price"
item_price = gets.chomp
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: #{formatted_money(total_price)}"
else number_check(item_price) == nil
break
end
endGetting the user's input at the beginning of the loop will ensure that it keeps repeating as long as the loop does.
The while loop could be further simplified by removing the nesting of code within an if-statement like so:
while true
puts "What is the sale price"
item_price = gets.chomp
break if number_check(item_price).nil?
item_total.push(item_price.to_f)
total_price = item_total.inject() {|sum, price| sum + price }
puts "Total Price: #{formatted_money(total_price)}"
endI'd also recommend to spend time to make sure that variable names are very good descriptions of their value. For instance, the variable total_price is somewhat misleading since it does not actually hold the value of the total price. Instead, this would be better if it were named something like item_prices or prices, signifying that it is a collection of more than one price.
item_prices = []
number_check = 0
while true
puts "What is the sale price"
item_price = gets.chomp
break if number_check(item_price).nil?
item_prices.push(item_price.to_f)
total_price = item_prices.inject() {|sum, price| sum + price }
puts "Total Price: #{formatted_money(total_price)}"
endThis will make the code much easier for other developers to read and understand.
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)
Try to make sure that everything is indented consistently, with 2 spaces. Make sure that you've got these settings in your Sublime Text settings https://gist.github.com/HeroicEric/77c860d2fe8d7ae0a98f