0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

ペアプロ 送料問題 回答編

Last updated at Posted at 2019-10-30
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
0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?