***RUBY CODE LANGUAGE ONLY Define a class BookInStock which represents a book wi
ID: 3742429 • Letter: #
Question
***RUBY CODE LANGUAGE ONLY
Define a class BookInStock which represents a book with an ISBN
number, isbn, and price of the book as a floating-point number,
price, as attributes. Run associated tests via: $ rspec -e 'getters and setters' spec/part3_spec.rb
The constructor should accept the ISBN number
(a string, since in real life ISBN numbers can begin with zero and can
include hyphens) as the first argument and price as second argument, and
should raise ArgumentError (one of Ruby’s built-in exception types) if
the ISBN number is the empty string or if the price is less than or
equal to zero. Include the proper getters and setters for these
attributes. Run associated tests via: $ rspec -e 'constructor' spec/part3_spec.rb
Include a method price_as_string that returns the price of
the book formatted with a leading dollar sign and two decimal places, that is, a price
of 20 should format as “$20.00” and a price of 33.8 should format as
"$33.80". Run associated tests via: $ rspec -e '#price_as_string' spec/part3_spec.rb
You can check your progress on the all the above by running rspec spec/part3_spec.rb.
Explanation / Answer
class BookInStock
attr_reader :isbn
attr_accessor :price
def initialize(isbn, price)
raise ArgumentError if isbn.empty? || price <= 0
@isbn = isbn
@price = Float(price)
end
def price_as_string
format("$%.2f", @price)
end
end
book = BookInStock.new("isbn1", 33.80)
puts "ISBN = #{book.isbn}"
puts "Price = #{book.price_as_string}"
book.price = book.price * 0.75
puts "New price = #{book.price_as_string}"
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.