soryo.rb
#商品選択
def purchase(lists, total)
puts "何を買いますか"
num = 0
lists.each do |list|
puts "#{num}:#{list[:fruit]}"
num += 1
end
select = gets.to_i
list = lists[select]
total[0] += list[:price]
total[1] += list[:weight]
puts "#{list[:fruit]}を購入しました"
return total
end
#レジに進む
def register(total)
total_weight = total[1]
if total[0] >= 2500
postage = 0
elsif total_weight >= 2000
postage = 500
else
postage = 200
end
total_price = total[0] + postage
puts "品物代:#{total[0]}円"
puts "送料:#{postage}円"
puts "合計金額:#{total_price}円"
done
end
def done
puts "またのご利用をお待ちしています"
exit
end
#商品リスト
lists = [{fruit: "cherry", price: 500, weight: 200}, {fruit: "apple", price: 200, weight: 500}, {fruit: "melon", price: 1000, weight: 1000}]
total = [0, 0]
line = "-------------------------------------------"
puts "いらっしゃいませ!"
puts line
puts "商品"
lists.each do |list|
puts "#{list[:fruit]}:#{list[:price]}円:重量#{list[:weight]}g"
end
puts line
puts "***品物代2500円以上で送料無料です!!***"
while true do
puts "何をしますか?\n番号を選択してください"
puts "[0]商品を選択する"
puts "[1]レジに進む"
puts "[2]買い物をやめる"
puts line
num = gets.to_i
if num == 0
total = purchase(lists, total) #商品選択へ
elsif num == 1
register(total) #レジに進む
else
done() #終了
end
end