forked from ruby-course-may-2014/homework0
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtask3.rb
executable file
·32 lines (29 loc) · 992 Bytes
/
task3.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
# Represents a book with
# - an isbn number, `isbn`,
# - and price of the book as a floating-point number, `price`,
# as attributes.
#
# The constructor should accept
# - the ISBN number (a string) 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.
# Include a method `price_as_string`
# that returns the price of the book
# with a leading dollar sign and trailing zeros,
# that is, a price of 20 should display as "$20.00"
# and a price of 33.8 should display as "$33.80".
class BookInStock
attr_accessor :isbn, :price
def initialize(isbn, price)
@isbn, @price = isbn.to_s, price.to_f.round(2)
raise ArgumentError if (@isbn.empty? || @price <= 0)
end
def price_as_string
price_2f = '%.2f' % @price
"$#{price_2f}"
end
end