Skip to content

Instantly share code, notes, and snippets.

@hisaju
Last active March 19, 2022 07:23
Show Gist options
  • Select an option

  • Save hisaju/0d2e6506a193d992025da3b7970f6e87 to your computer and use it in GitHub Desktop.

Select an option

Save hisaju/0d2e6506a193d992025da3b7970f6e87 to your computer and use it in GitHub Desktop.
names = ['オレンジジュース', 'コカコーラ', '烏龍茶', '緑茶', 'レッドブル']
juices = names.map{|n| {name: n, stock: 10} }
while(true) do
p '10円、50円、100円、500円、1000円、いずれかのお金を入れてください'
money = 0
while(coin = gets.chomp.to_i) do
case coin.to_i
when 10, 50, 100, 500, 1000
money = money + coin
p "現在#{money}円です"
else
p 'そのお金は無効です'
end
break if money >= 100
end
p '購入するジュースの番号を入力してください'
valid_numbers = []
juices.each_with_index do |juice, idx|
next if juice[:stock] < 1
p "#{idx + 1}. #{juice[:name]} 100円"
valid_numbers << idx
end
while(no=gets) do
unless no =~ /^[0-9]$/
p "無効な番号です。再度入力してください"
next
end
no = no.to_i - 1
if juices[no].nil? || juices[no][:stock] < 1
p "無効な番号です。再度入力してください"
next
end
juice = juices[no]
juice[:stock] -= 1
p "#{juice[:name]}をお取りください。お釣りは#{money - 100}円です"
break
end
p "在庫数"
juices.each_with_index do |juice, idx|
p "#{idx + 1}. #{juice[:name]} #{juice[:stock]}本"
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment