-
Notifications
You must be signed in to change notification settings - Fork 1
/
cc_payoff_estimate.rb
executable file
·89 lines (73 loc) · 1.93 KB
/
cc_payoff_estimate.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
#!/usr/bin/env ruby
##
# Calculate the interest paid and number of payments needed to pay off a credit card.
#
# Ex.
# cc_payment_estimator.rb 10_000 12.99 500.0
#
class String
def bold
"\e[1m#{self}\e[0m"
end
def green
"\e[32m#{self}\e[0m"
end
def red
"\e[31m#{self}\e[0m"
end
def red_bg
"\e[41m#{self}\e[0m"
end
end
class CCPayoffEstimator
attr_reader :apr, :starting_balance, :monthly_payment
def initialize(starting_balance, apr, monthly_payment)
@apr = apr.to_f
@starting_balance = starting_balance.to_f
@monthly_payment = monthly_payment.to_f
end
def print_estimate
current_balance = starting_balance
payments = 0
total_interest_paid = 0
puts "Starting balance: #{to_usd current_balance}",
"APR: #{apr}%",
"Monthly payment: #{to_usd monthly_payment}"
while current_balance > 0 do
interest = current_balance * apr/100/12
if interest >= monthly_payment
puts "\nThe #{to_usd(interest).bold} interest for the next billing cycle is " \
"greater than or equal to the #{to_usd(monthly_payment).red_bg} monthly payment.",
"At this rate, it will never be paid off! 😵".red
exit 1
end
current_balance -= (monthly_payment - interest)
total_interest_paid += interest
payments += 1
end
puts "\nOnce this is paid off...",
"Interest paid: #{to_usd(total_interest_paid).bold}",
"Payments made: #{payments.to_s.bold}"
end
private
##
# Return a US dollar-formatted string for +num+
#
def to_usd(num)
'$' + sprintf('%.2f',num)
.reverse
.scan(/(\d*\.\d{1,3}|\d{1,3})/)
.join(',')
.reverse
end
end
##
# Check runtime arguments
#
unless ARGV.length == 3
puts "Please provide the current balance, APR, and monthly payment".red,
' Ex.',
"#{__FILE__} 10_000.03 12.99 500"
exit 1
end
CCPayoffEstimator.new(*ARGV).print_estimate