Created
December 2, 2016 05:46
-
-
Save seth-at-at/7fde63c6f00def1b7f704034b0f3473f to your computer and use it in GitHub Desktop.
Check if a credit card number is valid or not.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
require 'pry' | |
puts "Please enter a 16 digit credit card number for validation." | |
this = gets.chomp | |
card_number = this.reverse | |
# | |
unless card_number.length. == 16 | |
puts "Requires 16 digits" | |
load 'credit_check.rb' | |
end | |
card_array = card_number.split("") | |
# puts card_array.to_s | |
q = (card_number[0]) | |
j = (card_number[2]) | |
k = (card_number[4]) | |
l = (card_number[6]) | |
m = (card_number[8]) | |
n = (card_number[10]) | |
o = (card_number[12]) | |
p = (card_number[14]) | |
new_card_array = [q.to_i, j.to_i, k.to_i, l.to_i, m.to_i, n.to_i, o.to_i, p.to_i] | |
a = (card_number[1].to_i)*2 | |
b = (card_number[3].to_i)*2 | |
c = (card_number[5].to_i)*2 | |
d = (card_number[7].to_i)*2 | |
e = (card_number[9].to_i)*2 | |
f = (card_number[11].to_i)*2 | |
g = (card_number[13].to_i)*2 | |
h = (card_number[15].to_i)*2 | |
# puts "------------------------" | |
if a >= 10 | |
aa = a.to_s[0] | |
ab = a.to_s[1] | |
ac = aa.to_i | |
ad = ab.to_i | |
ae = (ac + ad) | |
new_card_array.insert(1, ae.to_i) | |
else | |
new_card_array.insert(1, a.to_i) | |
end | |
if b >= 10 | |
ba = b.to_s[0] | |
bb = b.to_s[1] | |
bc = ba.to_i | |
bd = bb.to_i | |
be = (bc + bd) | |
new_card_array.insert(3, be.to_i) | |
else | |
new_card_array.insert(3, b.to_i) | |
end | |
if c >= 10 | |
ca = c.to_s[0] | |
cb = c.to_s[1] | |
cc = ca.to_i | |
cd = cb.to_i | |
ce = (cc + cd) | |
new_card_array.insert(5, ce.to_i) | |
else | |
new_card_array.insert(5, c.to_i) | |
end | |
if d >= 10 | |
da = d.to_s[0] | |
db = d.to_s[1] | |
dc = da.to_i | |
dd = db.to_i | |
de = (dc + dd) | |
new_card_array.insert(7, de.to_i) | |
else | |
new_card_array.insert(7, d.to_i) | |
end | |
if e >= 10 | |
ea = e.to_s[0] | |
eb = e.to_s[1] | |
ec = ea.to_i | |
ed = eb.to_i | |
ee = (ec + ed.to_i) | |
new_card_array.insert(9, ee.to_i) | |
else | |
new_card_array.insert(9, e.to_i) | |
end | |
if f >= 10 | |
fa = f.to_s[0] | |
fb = f.to_s[1] | |
fc = fa.to_i | |
fd = fb.to_i | |
fe = (fc + fd) | |
new_card_array.insert(11, fe.to_i) | |
else | |
new_card_array.insert(11, f.to_i) | |
end | |
if g >= 10 | |
ga = g.to_s[0] | |
gb = g.to_s[1] | |
gc = ga.to_i | |
gd = gb.to_i | |
ge = (gc + gd) | |
new_card_array.insert(13, ge.to_i) | |
else | |
new_card_array.insert(13, g.to_i) | |
end | |
if h >= 10 | |
ha = h.to_s[0] | |
hb = h.to_s[1] | |
hc = ha.to_i | |
hd = hb.to_i | |
he = (hc + hd) | |
new_card_array.insert(15, he.to_i) | |
else | |
new_card_array.insert(15, h.to_i) | |
end | |
# puts "-------------" | |
# puts new_card_array.to_s | |
total = new_card_array.reduce(0) {|sum, i| sum + i } | |
# puts total | |
if total % 10 == 0 | |
puts "This number is Valid!" | |
valid = true | |
else | |
puts "This number is Invalid!" | |
valid = false | |
end | |
puts "Do you have another number? (y/n)" | |
user_input = gets.chomp | |
if user_input == "n" | |
abort | |
elsif user_input == "y" | |
load 'credit_check.rb' | |
elsif user_input == "Y" | |
load 'credit_check.rb' | |
elsif user_input == "N" | |
abort | |
else | |
puts "You did not enter (y/n) program will now close." | |
abort | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment