Advertisement
Guest User

My Solution

a guest
Mar 4th, 2012
73
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Ruby 0.74 KB | None | 0 0
  1. @prices = []
  2. @num_of_items = 0
  3. @sum = 0
  4.  
  5. puts "Enter the amount of money you have:"
  6. print "$"
  7. @amount_of_money = gets.chomp.to_i
  8.  
  9. puts "Enter the number of items:"
  10. @num_of_items = gets.chomp.to_i
  11.  
  12. while @prices.length <= (@num_of_items - 1)
  13.     puts "Enter item #{@prices.length+1} price:"
  14.     print "$"
  15.     @prices << gets.chomp
  16.     break   if @prices.last == -1
  17. end
  18. puts "*" * 50
  19.  
  20. @prices.each do |p|
  21.     @sum += p.to_i
  22. end
  23. puts "You have a total of $#{@sum}."
  24. if @sum >= @amount_of_money
  25.     short = (@amount_of_money-@sum).abs
  26.     puts "You have enough money by $ #{short}. Now go buy it!"
  27.     exit
  28. end
  29.  
  30. if @sum < @amount_of_money
  31.     diff = (@sum-@amount_of_money).abs
  32.     puts "Sorry, you don't have enough money. You are short by $#{diff}."
  33.     exit
  34. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement