質問をすることでしか得られない、回答やアドバイスがある。

15分調べてもわからないことは、質問しよう!

新規登録して質問してみよう
ただいま回答率
85.48%
Ruby

Rubyはプログラミング言語のひとつで、オープンソース、オブジェクト指向のプログラミング開発に対応しています。

Q&A

1回答

683閲覧

商品注文システム【Ruby】

kaikubo_ko

総合スコア13

Ruby

Rubyはプログラミング言語のひとつで、オープンソース、オブジェクト指向のプログラミング開発に対応しています。

0グッド

1クリップ

投稿2021/01/26 06:33

商品注文システムを作成したく、下記のように記述しましたが、動作がうまくいかず詰まっています。
他にも別の記述方法があるかもしれませんが、下記のように記述した場合の修正方法を教えてください。

【やりたいこと】
下記の実行結果を表示したい

買い物かごに入っている商品は全部で 2 個です 金額は合計 1700 円です

【記述したコード】

class Product attr_accessor :name, :price def initialize(name, price) @name = name @price = price end end class Book < Product attr_accessor :author, :publisher, :page_count def initialize(name, price) self.author = author self.publisher = publisher self.page_count = page_count end def info return "#{self.name} #{self.price}円", "#{self.author}", "#{self.publisher}", "#{self.page_count}" end end class CD < Product attr_accessor :artist, :year, :songs def initialize(name, price ) self.artist = artist self.year = year self.songs = songs end def info return "#{self.name} #{self.price}円", "#{self.artist}", "#{self.year}", "#{self.song}" end end class ShoppingCart attr_accessor :count, :total_price def initialize self.count = 0 self.total_price = 0 end def add_product(product) self.count += 1 self.total_price += @price.to_i end end book1 = Book.new('吾輩は猫である', 500) book1.author = '夏目漱石' book1.publisher = 'Vitalize出版' book1.page_count = 400 cd1 = CD.new('SMAP', 1200) cd1.artist = 'スマップ' cd1.year = 2018 cd1.songs = ["青い稲妻", "世界に一つだけの花", "オレンジ"] cart = ShoppingCart.new cart.add_product(book1) cart.add_product(cd1) puts "買い物かごに入っている商品は全部で #{cart.count} 個です" puts "金額は合計 #{cart.total_price} 円です"

【問題と思われる箇所】
下記のように記述したのですが、
合計金額を表示するためのtotal_priceにproduct情報が入れられていないことが原因だと思いますが、記述方法がわからず困っています。

def add_product(product) self.count += 1 self.total_price += @price.to_i end

気になる質問をクリップする

クリップした質問は、後からいつでもMYページで確認できます。

またクリップした質問に回答があった際、通知やメールを受け取ることができます。

バッドをするには、ログインかつ

こちらの条件を満たす必要があります。

guest

回答1

0

修正しました。

ruby

1class Product 2 attr_accessor :name, :price 3 def initialize(name, price) 4 @name = name 5 @price = price 6 end 7end 8 9class Book < Product 10 attr_accessor :author, :publisher, :page_count 11 def initialize(name, price) 12 super(name,price) 13 self.author = author 14 self.publisher = publisher 15 self.page_count = page_count 16 end 17 def info 18 return "#{self.name} #{self.price}円", 19 "#{self.author}", 20 "#{self.publisher}", 21 "#{self.page_count}" 22 end 23end 24 25class CD < Product 26 attr_accessor :artist, :year, :songs 27 def initialize(name, price ) 28 super(name,price) 29 self.artist = artist 30 self.year = year 31 self.songs = songs 32 end 33 def info 34 return "#{self.name} #{self.price}円", 35 "#{self.artist}", 36 "#{self.year}", 37 "#{self.song}" 38 end 39end 40class ShoppingCart 41 attr_accessor :count, :total_price 42 43 def initialize 44 self.count = 0 45 self.total_price = 0 46 end 47 def add_product(product) 48 self.count += 1 49 self.total_price += product.price 50 end 51end 52 53book1 = Book.new('吾輩は猫である', 500) 54book1.author = '夏目漱石' 55book1.publisher = 'Vitalize出版' 56book1.page_count = 400 57 58cd1 = CD.new('SMAP', 1200) 59cd1.artist = 'スマップ' 60cd1.year = 2018 61cd1.songs = ["青い稲妻", "世界に一つだけの花", "オレンジ"] 62 63cart = ShoppingCart.new 64cart.add_product(book1) 65cart.add_product(cd1) 66 67puts "買い物かごに入っている商品は全部で #{cart.count} 個です" 68puts "金額は合計 #{cart.total_price} 円です" 69

変更箇所

diff

112d11 2< 313a13 4> super(name,price) 518d17 6< 729d27 8< 930a29 10> super(name,price) 1135d33 12< 1352c50 14< self.total_price += @price.to_i 15--- 16> self.total_price += product.price 17

投稿2021/01/26 10:46

tatsu99

総合スコア5438

バッドをするには、ログインかつ

こちらの条件を満たす必要があります。

あなたの回答

tips

太字

斜体

打ち消し線

見出し

引用テキストの挿入

コードの挿入

リンクの挿入

リストの挿入

番号リストの挿入

表の挿入

水平線の挿入

プレビュー

まだベストアンサーが選ばれていません

会員登録して回答してみよう

アカウントをお持ちの方は

15分調べてもわからないことは
teratailで質問しよう!

ただいまの回答率
85.48%

質問をまとめることで
思考を整理して素早く解決

テンプレート機能で
簡単に質問をまとめる

質問する

関連した質問