fahadm507
6/2/2014 - 5:01 PM

gistfile1.txt

class Mortgage
  attr_reader :home_value, :down_payment_percentage, :apr, :duration_in_years, :down_payment

  def initialize(home_value, down_payment_percentage, apr, duration_in_years)
    @home_value = home_value
    @down_payment_percentage = down_payment_percentage
    @apr = apr
    @duration_in_years = duration_in_years
  end

  def down_payment
    @down_payment = (@down_payment_percentage * home_value)
  end

  def monthly_payment
    monthly_payment = (apr/12) * (@home_value - @down_payment)/(1-(1+ apr/12)**(-1 * duration_in_years * 12))
  end

  def total_interest_paid
    total_interest_paid = monthly_payment * 360 - (home_value - down_payment)
  end
end
require_relative 'mortgage'

DOWN_PAYMENT_PERCENTAGES = [0.05, 0.10, 0.15, 0.20]

print "What is the home value: "
home_value = gets.chomp.to_f

print "What is the APR: "
apr = gets.chomp.to_f

print "What is the duration (in years): "
duration_in_years = gets.chomp.to_f

puts
puts sprintf("%-15s %-10s %-10s %-20s %-20s",
  "Down Payment", "Duration", "APR", "Monthly Payment", "Total Interest Paid")

DOWN_PAYMENT_PERCENTAGES.each do |down_payment_percentage|
  mortgage = Mortgage.new(home_value, down_payment_percentage, apr, duration_in_years)

  puts sprintf("%-15.2f %-10d %-10.3f %-20.2f %-20.2f",
    mortgage.down_payment, duration_in_years, apr,
    mortgage.monthly_payment, mortgage.total_interest_paid)
end