Skip to content

Instantly share code, notes, and snippets.

@justuseapen
Created December 4, 2013 20:12
Show Gist options
  • Save justuseapen/7794671 to your computer and use it in GitHub Desktop.
Save justuseapen/7794671 to your computer and use it in GitHub Desktop.
Mortgage Calculator
class HomePurchaseOption
def initialize(address,property_value,selling_price,down_payment)
@address = address
@property_value = property_value
@selling_price = selling_price
@down_payment = down_payment
end
def address
@address
end
#SHOULD CHANGE BASED ON PAYMENTS AND INTEREST ACCUED:
# def mortgage_balance
# @selling_price - @down_payment
# end
#the amount of cash value you have in the home
#This would be difference in your property's value
#and your mortgage balance
def equity_after_sale
@property_value.to_i - required_mortgage
end
#the amount of money you must borrow to purchase the home
def required_mortgage
@selling_price.to_i - @down_payment.to_i
end
# def mortgage_payment
# monthly_mortgage_payment = required_mortgage /
#how much your insurance cost will cost over `years` years
def insurance_cost(years)
payments = years*12
monthly_mortgage_payment = required_mortgage / payments
if pmi_required?
monthly_insurance_payment = monthly_mortgage_payment * 0.005
monthly_insurance_payment * payments
else
0
end
end
private
#determine if the purchaser must pay insurance
def pmi_required?
if equity_after_sale < 0.2*@property_value.to_i
return true
end
end
end
require 'csv'
require_relative 'home_purchase_option'
properties = []
CSV.foreach("./properties.csv", headers: true) do |row|
properties << HomePurchaseOption.new(row['Address'], row['Property Value'], row['Selling Price'], row['Down Payment'])
end
properties.each do |prop|
puts ""
puts "**** #{prop.address} ****"
puts "Mortgage: #{prop.required_mortgage}"
puts "2 Years of Insurance : #{prop.insurance_cost(2)}"
end
Address Property Value Selling Price Down Payment
43 Fenmore Lane 439000 419000 20000
58 Johnson Way 512000 524000 105000
32 Silver Lane 485000 490000 97000
45 Fenway Drive 465000 460000 93000
54 Denise Drive 445000 450000 98000
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment