Created
July 9, 2012 18:56
-
-
Save 7even/3078216 to your computer and use it in GitHub Desktop.
electroenergy payment calculation
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
# encoding: utf-8 | |
class Payment | |
RATE = 3.8 | |
attr_accessor :previous, :current | |
def initialize(params = {}) | |
@previous = params.delete(:previous) | |
@current = params.delete(:current) | |
end | |
def difference | |
@current - @previous | |
end | |
def cost | |
difference * RATE | |
end | |
def to_s | |
"(#{@previous} -> #{@current}): #{difference} КВт * #{RATE} руб/КВт = #{cost} руб" | |
end | |
def self.for(amount, params = {}) | |
max_difference = (amount / RATE).floor | |
if params[:previous] | |
params[:current] = params[:previous] + max_difference | |
elsif params[:current] | |
params[:previous] = params[:current] - max_difference | |
end | |
new(params) | |
end | |
end | |
puts Payment.new(previous: 38565, current: 39354) | |
puts Payment.for(3000, previous: 38565) | |
puts Payment.for(5000, current: 39354) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment