Class: CompoundInterest::Calculation
- Inherits:
-
Object
- Object
- CompoundInterest::Calculation
- Defined in:
- lib/compound_interest/calculation.rb
Class Method Summary collapse
- .calculate(params) ⇒ Object
- .compound_interest_formula(num = 0) ⇒ Object
- .periodical_payment_calculation ⇒ Object
- .raise_error(params) ⇒ Object
- .result ⇒ Object
Class Method Details
.calculate(params) ⇒ Object
5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 |
# File 'lib/compound_interest/calculation.rb', line 5 def self.calculate(params) raise_error(params) @initial_payment = params[:initial_payment] # number of terms @term = params[:term] # interest rate per term @interest_rate = params[:interest_rate].to_f / 100 # number of interest payments for the term @capitalization_periodicity = params[:capitalization_periodicity] @payment = params[:payment] || 0 # number of payments for the term @payment_periodicity = params[:payment_periodicity] result end |
.compound_interest_formula(num = 0) ⇒ Object
37 38 39 40 41 42 43 44 |
# File 'lib/compound_interest/calculation.rb', line 37 def self.compound_interest_formula(num = 0) if num.zero? (1.0 + @interest_rate / @capitalization_periodicity)**(@term * @capitalization_periodicity) else (1.0 + @interest_rate / @capitalization_periodicity)**((@term * @capitalization_periodicity) - (@capitalization_periodicity / @payment_periodicity * num)) end end |
.periodical_payment_calculation ⇒ Object
28 29 30 31 32 33 34 35 |
# File 'lib/compound_interest/calculation.rb', line 28 def self.periodical_payment_calculation times = (@payment_periodicity * @term).to_i payment_result = 0 (1..times).each do |i| payment_result += @payment * compound_interest_formula(i) end payment_result end |
.raise_error(params) ⇒ Object
46 47 48 49 50 51 52 |
# File 'lib/compound_interest/calculation.rb', line 46 def self.raise_error(params) raise "Argument must be Hash" if params.class != Hash raise ArgumentError, "Argument must include :initial_payment" if params[:initial_payment].nil? raise ArgumentError, "Argument must include :term" if params[:term].nil? raise ArgumentError, "Argument must include :interest_rate" if params[:interest_rate].nil? raise ArgumentError, "Argument must include :capitalization_periodicity" if [:capitalization_periodicity].nil? end |
.result ⇒ Object
22 23 24 25 26 |
# File 'lib/compound_interest/calculation.rb', line 22 def self.result result = @initial_payment * compound_interest_formula result = periodical_payment_calculation if @payment.positive? result end |