Created
December 4, 2013 08:43
2013年12月の買った本リスト
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class InvalidateCurrencyUnitError < RuntimeError; end; | |
module CurrencyUnit | |
USD = 1 | |
EURO = 2 | |
YEN = 3 | |
def validate_currency_unit(currency_unit) | |
if currency_unit == USD; return true; end; | |
if currency_unit == EURO; return true; end; | |
if currency_unit == YEN; return true; end; | |
false | |
end | |
module_function :validate_currency_unit | |
/*為替変換メソッドとか作る*/ | |
end | |
class Money | |
attr_reader :amount, :currency_unit | |
def initialize(currency_unit, amount) | |
@amount = amount | |
if CurrencyUnit.validate_currency_unit(currency_unit) then | |
@currency_unit = currency_unit | |
else | |
raise InvalidateCurrencyUnitError | |
end | |
end | |
def to_s | |
case @currency_unit | |
when CurrencyUnit::USD | |
"$#{@amount} USD" | |
when CurrencyUnit::EURO | |
"€#{@amount} EURO" | |
when CurrencyUnit::YEN | |
"#{@amount} JYN" | |
else | |
"#{@amount}" | |
end | |
end | |
end | |
class Book | |
attr_reader :price, :publisher | |
def initialize(publisher, title, price) | |
@publisher = publisher | |
@title = title | |
if price.kind_of?(Money) then | |
@price = price | |
else | |
raise "InvalidateArguments" | |
end | |
end | |
def to_s | |
<<"TO_S" | |
PUBLISHER : #{@publisher} | |
TITLE : #{@title} | |
PRICE : #{@price.to_s} | |
TO_S | |
end | |
end | |
books = Array.new | |
books.push(Book.new("O'reilly", 'ruby under a microscope', Money.new(CurrencyUnit::USD, 12.78))) | |
books.push(Book.new("O'reilly", 'physics for game developers, 2nd edition', Money.new(CurrencyUnit::USD, 14.40))) | |
books.push(Book.new("O'reilly", 'an introduction to the mathematics of finance, 2nd edition', Money.new(CurrencyUnit::USD, 31.98))) | |
books.push(Book.new("O'reilly", 'head first statistics', Money.new(CurrencyUnit::USD, 11.20))) | |
books.push(Book.new('apress', 'expert f# 3.0', Money.new(CurrencyUnit::USD, 15))) | |
books.push(Book.new('apress', 'beginning haskell', Money.new(CurrencyUnit::USD, 15))) | |
books.push(Book.new('manning publications', 'java 8 lambdas in action', Money.new(CurrencyUnit::USD, 19.99))) | |
books.push(Book.new('the pragmatic bookshelf', 'build awesome command-line applications in ruby 2', Money.new(CurrencyUnit::USD, 22.00 / 2 ))) | |
books.push(Book.new('the pragmatic bookshelf', 'explore it!', Money.new(CurrencyUnit::USD, 23.00 / 2))) | |
books.push(Book.new('the pragmatic bookshelf', 'functional programming patterns in scala and clojure', Money.new(CurrencyUnit::USD, 24.00 / 2 ))) | |
books.push(Book.new('the pragmatic bookshelf', 'good math', Money.new(CurrencyUnit::USD, 24.00 / 2))) | |
books.push(Book.new('the pragmatic bookshelf', 'modern c++ programming with test-driven development', Money.new(CurrencyUnit::USD, 26.00 / 2 ))) | |
books.push(Book.new('the pragmatic bookshelf', 'node.js the right way', Money.new(CurrencyUnit::USD, 11.00 / 2 ))) | |
books.push(Book.new('the pragmatic bookshelf', 'programming elixir', Money.new(CurrencyUnit::USD, 24.00 / 2 ))) | |
books.push(Book.new('the pragmatic bookshelf', 'seven concurrency models in seven weeks', Money.new(CurrencyUnit::USD, 25.00 / 2 ))) | |
books.push(Book.new('the pragmatic bookshelf', 'seven web frameworks in seven weeks', Money.new(CurrencyUnit::USD, 25.00 / 2 ))) | |
books.push(Book.new('the pragmatic bookshelf', 'the healthy programmer', Money.new(CurrencyUnit::USD, 24.00 / 2 ))) | |
books.push(Book.new('the pragmatic bookshelf', 'web development with clojure', Money.new(CurrencyUnit::USD, 24.00 / 2 ))) | |
books.push(Book.new('the pragmatic bookshelf', 'working with ruby threads', Money.new(CurrencyUnit::USD, 29.00 / 2))) | |
books.each{|book| | |
puts book.to_s | |
puts "" | |
} | |
amount = 0 | |
books.each{|book| | |
amount += book.price.amount | |
} | |
puts Money.new(CurrencyUnit::USD, amount).to_s | |
oreilly_amount = 0 | |
oreilly_books = books.select{|book| book.publisher == "O'reilly"} | |
oreilly_books.each{|book| | |
oreilly_amount += book.price.amount | |
} | |
puts Money.new(CurrencyUnit::USD, oreilly_amount).to_s |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment