Created
November 17, 2013 00:49
-
-
Save vanmichael/7507516 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
#Cashier.rb | |
require './register.rb' | |
require 'pry' | |
cashier = Register.new | |
while cashier.get_sale_price != "done" | |
cashier.add_sale_price_to_list | |
cashier.calculate_sub_total | |
puts "Subtotal: $" + cashier.sub_total.to_s | |
end | |
cashier.list_items | |
puts "The total amount due is: $" + cashier.sub_total.to_s | |
cashier.get_amount_tendered | |
if cashier.validate_amount? | |
cashier.calculate_change | |
puts "=========Thank You!=========" | |
puts "The total change due is $" + cashier.change.to_s | |
else | |
puts "WARNING: Customer still owes #{sprintf('$%0.2f', (cashier.sub_total.to_f-cashier.amount_tendered.to_f))}! Exiting..." | |
end |
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
#Register.rb | |
class Register | |
attr_accessor :sale_price, :items, :sub_total, :amount_tendered, :change | |
def initialize | |
@items = [] | |
end | |
#VAIDATIONS | |
def validate?(object) | |
if object == "done" || (object !~ /\A\$?\d+(\.\d{1,2})?\z/) == false | |
return true | |
else | |
return false | |
end | |
end | |
def validate_amount? | |
@amount_tendered.to_f > @sub_total.to_f | |
end | |
#Other Methods | |
def get_sale_price | |
puts "What is the sales price?" | |
@sale_price = gets.chomp | |
return "done" if @sale_price == "done" | |
get_sale_price if validate?(@sale_price) == false | |
end | |
def get_amount_tendered | |
puts "What is the amount tendered?" | |
@amount_tendered = gets.chomp | |
end | |
def add_sale_price_to_list | |
@items << @sale_price.to_f | |
end | |
def list_items | |
puts "Here are your item prices?" | |
@items.each do |i| | |
puts "$" + i.to_s | |
end | |
end | |
def calculate_sub_total | |
@sub_total = 0 | |
@items.each do |i| | |
@sub_total +=i.to_f | |
end | |
end | |
def calculate_change | |
@change = @amount_tendered.to_f - @sub_total.to_f | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment