Qiita Teams that are logged in
You are not logged in to any team

Log in to Qiita Team
Community
OrganizationAdvent CalendarQiitadon (β)
Service
Qiita JobsQiita ZineQiita Blog
Help us understand the problem. What is going on with this article?

ペアプロ 送料問題 回答編

More than 1 year has passed since last update.
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
Why not register and get more from Qiita?
  1. We will deliver articles that match you
    By following users and tags, you can catch up information on technical fields that you are interested in as a whole
  2. you can read useful information later efficiently
    By "stocking" the articles you like, you can search right away