diff --git "a/\043\043kaimonoo.rb\043\043" "b/\043\043kaimonoo.rb\043\043" new file mode 100644 index 0000000..0519ecb --- /dev/null +++ "b/\043\043kaimonoo.rb\043\043" @@ -0,0 +1 @@ + \ No newline at end of file diff --git "a/\043hello15.rb\043" "b/\043hello15.rb\043" new file mode 100755 index 0000000..c910a64 --- /dev/null +++ "b/\043hello15.rb\043" @@ -0,0 +1,51 @@ +#!/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} "円" diff --git "a/\043hello16.rb\043" "b/\043hello16.rb\043" new file mode 100644 index 0000000..e69de29 --- /dev/null +++ "b/\043hello16.rb\043" diff --git "a/\043hello18.rb\043" "b/\043hello18.rb\043" new file mode 100644 index 0000000..e69de29 --- /dev/null +++ "b/\043hello18.rb\043" diff --git "a/\043kaimono\043" "b/\043kaimono\043" new file mode 100644 index 0000000..e69de29 --- /dev/null +++ "b/\043kaimono\043" diff --git "a/\043kaimonoo.rb\043" "b/\043kaimonoo.rb\043" new file mode 100644 index 0000000..e69de29 --- /dev/null +++ "b/\043kaimonoo.rb\043" diff --git "a/.\043\043kaimonoo.rb\043" "b/.\043\043kaimonoo.rb\043" new file mode 120000 index 0000000..3d9dd78 --- /dev/null +++ "b/.\043\043kaimonoo.rb\043" @@ -0,0 +1 @@ +j2523@jd.koeki-u.ac.jp.1164:1755790718 \ No newline at end of file diff --git "a/.\043hello14.rb" "b/.\043hello14.rb" deleted file mode 120000 index 23e6d39..0000000 --- "a/.\043hello14.rb" +++ /dev/null @@ -1 +0,0 @@ -j2523@jd.koeki-u.ac.jp.1494:1754743980 \ No newline at end of file diff --git "a/.\043hello15.rb" "b/.\043hello15.rb" new file mode 120000 index 0000000..1a7ffa1 --- /dev/null +++ "b/.\043hello15.rb" @@ -0,0 +1 @@ +j2523@jd.koeki-u.ac.jp.1032:1755790718 \ No newline at end of file diff --git "a/.\043hello16.rb" "b/.\043hello16.rb" new file mode 120000 index 0000000..157a3f1 --- /dev/null +++ "b/.\043hello16.rb" @@ -0,0 +1 @@ +j2523@jd.koeki-u.ac.jp.996:1755703645 \ No newline at end of file diff --git "a/.\043hello18.rb" "b/.\043hello18.rb" new file mode 120000 index 0000000..157a3f1 --- /dev/null +++ "b/.\043hello18.rb" @@ -0,0 +1 @@ +j2523@jd.koeki-u.ac.jp.996:1755703645 \ No newline at end of file diff --git "a/.\043kaimono" "b/.\043kaimono" new file mode 120000 index 0000000..1a7ffa1 --- /dev/null +++ "b/.\043kaimono" @@ -0,0 +1 @@ +j2523@jd.koeki-u.ac.jp.1032:1755790718 \ No newline at end of file diff --git "a/.\043require \047csv\047.rb" "b/.\043require \047csv\047.rb" new file mode 120000 index 0000000..3d9dd78 --- /dev/null +++ "b/.\043require \047csv\047.rb" @@ -0,0 +1 @@ +j2523@jd.koeki-u.ac.jp.1164:1755790718 \ No newline at end of file diff --git a/hello15.rb b/hello15.rb index 38c0a38..c910a64 100755 --- a/hello15.rb +++ b/hello15.rb @@ -1,15 +1,51 @@ #!/usr/bin/env ruby # -*- coding: utf-8 -*- -def aka - "\e[31m%s\n" +require 'csv' + + +items = {} +CSV.foreach("item.csv", headers: true) do |row| + items[row["name"]] = row["price"].to_i end -puts"Enter" -a = gets.chomp -printf(aka ,a + "------------------------------") -printf(aka ,a + "------------------------------") -printf(aka ,a + "------------------------------") -printf(aka ,a + "------------------------------") -printf(aka ,a + "------------------------------") -printf(aka ,a + "------------------------------") + +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} "円" diff --git a/items.csv b/items.csv new file mode 100644 index 0000000..f1e573b --- /dev/null +++ b/items.csv @@ -0,0 +1,8 @@ +name,price +りんご,100 +バナナ,80 +みかん,120 +アクエリアス,110 +株式会社よっちゃん,50000000 +ゴミ1t,-10000 + diff --git a/require csv.rb b/require csv.rb new file mode 100755 index 0000000..7cc3bc4 --- /dev/null +++ b/require csv.rb @@ -0,0 +1,50 @@ +#!/usr/bin/env ruby +# -*- coding: utf-8 -*- + +require 'csv' + +# CSVファイルから商品リストを読み込み(Hash: { "商品名" => 単価 }) +items = {} +CSV.foreach("items.csv", headers: true) do |row| + items[row["name"]] = row["price"].to_i +end + +cart = [] +total = 0 + +puts "=== 買い物プログラム (CSV版) ===" +puts "商品リスト:" +items.each { |name, price| puts " #{name} : #{price}円" } +puts "買いたい商品を入力してください(endで終了)" + +loop do + print "商品名: " + name = gets&.strip + break if name.nil? || name.downcase == "end" + + if items.key?(name) + print "数量: " + qty = Integer(gets&.strip || "0", exception: false) || 0 + if qty <= 0 + puts "数量は1以上で入力してください。" + next + end + + price = items[name] + subtotal = price * qty + cart << { name: name, price: price, quantity: qty, subtotal: subtotal } + total += subtotal + + puts "#{name} × #{qty} = #{subtotal}円 を追加しました。" + puts "現在の合計: #{total} 円" + puts "--------------------------" + else + puts "その商品はリストにありません。" + end +end + +puts "\n=== お会計 ===" +cart.each do |item| + puts "#{item[:name]} (#{item[:price]}円 × #{item[:quantity]}個) = #{item[:subtotal]}円" +end +puts "合計金額: #{total} 円"