Newer
Older
Ruby / #hello15.rb#
@SAITO Azuma SAITO Azuma on 21 Aug 1 KB 2025-08-21 16:35:16
#!/usr/bin/env ruby
# -*- coding: utf-8 -*-

require 'csv'


items = {}
CSV.foreach("item.csv", headers: true) do |row|
  items[row["name"]] = row["price"].to_i
end


cart {}
total = 0


puts "買い物プログラム"
puts "商品リスト:"
item.each { |name, price| puts "  #{name} : #{price}円" }
puts "買いたい商品を入力してください(endで終了) "

loop do
  print "商品名:  "
  name = gets.chomp
  break if name.downcase == "end"


  if items.key?(name)
    print "数量: "
    quantity = gets.chomp.to_i
    price = items[name]
    item_total = price * quantity
    cart << {name: name, price: price, quantity: quantity, subtotal: item_total}
    total += item_total


    puts "#{name} * #{quantity} = #{item_total}円 を追加しました。"
    puts "現在の合計 #{total} 円"
    puts "11----------------------------"
  else
    puts "その商品はリストにありません。"
  end
end


puts "\n   お会計     "
cart.each do |item|
  puts "#{item[:name]} {#{item[:price]"円") * #{item[quantity]}"個)" = #{item[:subtotal]}"円")
end

puts "合計金額: #{total} "円"