Skip to content

Instantly share code, notes, and snippets.

@ilhamarrouf
Created October 23, 2017 11:21
Show Gist options
  • Save ilhamarrouf/d20c8e2a3f8409ef15266ffe34938e8a to your computer and use it in GitHub Desktop.
Save ilhamarrouf/d20c8e2a3f8409ef15266ffe34938e8a to your computer and use it in GitHub Desktop.
Ruby: Applikasi toko simple
bill = 0
products = {}
products[:a] = {:nama => 'sampo', :harga => 25000}
products[:b] = {:nama => 'sikat gigi', :harga => 10000}
products[:c] = {:nama => 'air mineral', :harga => 3000}
products[:d] = {:nama => 'gelas', :harga => 4000}
products[:e] = {:nama => 'piring', :harga => 7000}
loop do
products.each do |key, product|
puts "#{key}. #{product[:nama]} = #{product[:harga]}"
end
print 'Silakan pilih product yang akan dibeli : '
selected = gets.strip
case selected
when 'a'
product = products[:a]
when 'b'
product = products[:b]
when 'c'
product = products[:c]
when 'd'
product = products[:d]
when 'e'
product = products[:e]
else
puts 'Produk yang anda pilih tidak tersedia!'
end
if product
bill += product[:harga]
end
print 'Ingin berbelanja lagi? y/n : '
again = gets.strip
break if again == 'n'
end
puts "Total tagihan belanja anda Rp. #{bill}"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment