Skip to content

Instantly share code, notes, and snippets.

@badosu
Last active December 30, 2015 20:08
Show Gist options
  • Save badosu/7878261 to your computer and use it in GitHub Desktop.
Save badosu/7878261 to your computer and use it in GitHub Desktop.
class Stamps
attr_reader :amount
def initialize(amount)
if (amount =~ /^\d+$/).nil?
raise ArgumentError, "The amount should be an integer"
end
if amount.to_i < 8
raise ArgumentError, "The amount should be bigger than 7"
end
@amount = amount.to_i
end
def print_calculation
fives, threes = calculate
puts "#{fives} five cent stamp#{"s" if fives != 1}, #{
threes} three cent stamp#{"s" if threes != 1}"
end
def calculate
remainder = amount % 5
three_remainder = remainder % 3
[amount/5 - three_remainder, (remainder/3) + three_remainder * 2]
end
end
require_relative 'stamps'
require 'test/unit'
class StampsTest < Test::Unit::TestCase
def test_raise_empty
assert_raise(ArgumentError) { Stamps.new(nil) }
end
def test_raise_lesser_than_8
assert_raise(ArgumentError) { Stamps.new("4") }
end
def test_raise_non_integer
assert_raise(ArgumentError) { Stamps.new("14.3") }
end
def test_raise_non_numerical
assert_raise(ArgumentError) { Stamps.new("asd") }
end
def test_calculate_11
stamps = Stamps.new(11.to_s)
assert_equal([1, 2], stamps.calculate)
end
def test_calculate_100
stamps = Stamps.new(100.to_s)
assert_equal([20, 0], stamps.calculate)
end
def test_calculate_101
stamps = Stamps.new(101.to_s)
assert_equal([19, 2], stamps.calculate)
end
def test_calculate_10000002
stamps = Stamps.new(10000002.to_s)
assert_equal([1999998, 4], stamps.calculate)
end
end
#!/usr/bin/env ruby
require_relative 'stamps'
begin
Stamps.new(ARGV[0]).print_calculation
rescue ArgumentError => e
puts <<-EOF
#{e.message}
Usage:
stampy.rb amount
Options:
amount - should be an integer lesser than 8
Example:
stampy.rb 15
EOF
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment