Skip to content

Instantly share code, notes, and snippets.

@7even
Created July 9, 2012 18:56
Show Gist options
  • Save 7even/3078216 to your computer and use it in GitHub Desktop.
Save 7even/3078216 to your computer and use it in GitHub Desktop.
electroenergy payment calculation
# 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