Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
35 changes: 35 additions & 0 deletions class_book.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
class Book
attr_reader :title, :price
attr_writer :title, :price
def initialize(title=nil,price=nil)
if title.nil? || title.empty?
raise ArgumentError.new("Invalid string")
end
if price.nil? || price<0
raise ArgumentError.new("Invalid price")
end
@title=title
@price=price
end

def formatted_price
if @price.integer?
return "#{@price} dollars"
else
conv = @price.to_s.split('.')
int_part = conv.first.to_i
dec_part = conv.last.to_i
if int_part == 0
return "#{dec_part} cents"
end
return "#{int_part} dollars and #{dec_part} cents"
end
end

end


object = Book.new(' ',10.05)
puts object.formatted_price