Skip to content

Instantly share code, notes, and snippets.

@vanmichael
Created November 20, 2013 00:12
Show Gist options
  • Save vanmichael/7555017 to your computer and use it in GitHub Desktop.
Save vanmichael/7555017 to your computer and use it in GitHub Desktop.
Cash Register with Hash to store Quantity
#Cashier
require './register.rb'
puts "Welcome to James' Coffee Emporium!"
puts "1) Add item - $5.00 - Light Bag "
puts "2) Add item - $7.50 - Medium Bag"
puts "3) Add item - $9.75 - Bold Bag"
puts "4) Complete Sale"
cashier = Register.new
while cashier.gets_selection != "4"
cashier.gets_quantity
cashier.add_quantity
cashier.calculate_subtotal
end
puts "Sale Complete"
cashier.list_items
puts "Total: $" + cashier.total.to_s
cashier.get_amount_tendered
cashier.calculate_change
puts "========Thankyou========"
puts "Total change due is: $" + cashier.total.to_s
require 'pry'
#Register
class Register
attr_reader :items, :selection, :total
def initialize
@items = { 1 => { :name => "Light Bag", :price => 5.00, :qty => 0 }, 2 => { :name => "Medium Bag", :price => 7.50, :qty => 0 }, 3 => { :name => "Bold Bag", :price => 9.75, :qty => 0 }, 4 => { :name => "done", :price => 0, :qty => 0}}
@quantity = 5
@selection = 0
@total = 0
end
def gets_selection
puts "Make Your Selection"
@selection = gets.chomp
end
def gets_quantity
puts "How Many?"
@quantity = gets.chomp
end
def add_quantity
@items[@selection.to_i][:qty] += @quantity.to_i
end
def calculate_subtotal
@total += (@items[@selection.to_i][:qty].to_f * @items[@selection.to_i][:price].to_f)
puts "Subtotal: $#{@total}"
end
def list_items
@items.each do |key,value|
puts "#{(@items[key][:qty] * @items[key][:price])}" + "-" + "#{@items[key][:name]}" if @items[key][:qty] != 0
end
end
def get_amount_tendered
puts "What is the amount tendered?"
@amount_tendered = gets.chomp
end
def calculate_change
@change = @amount_tendered.to_f - @total.to_f
puts "Change due: $" + @change.to_s
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment