-
Notifications
You must be signed in to change notification settings - Fork 0
/
credit_check.rb
77 lines (63 loc) · 1.44 KB
/
credit_check.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
class CreditCheck
attr_reader :card_number
attr_accessor :card_numbers,
:doubled_card_nums,
:nums_over_10_sum,
:summed
def initialize(card_number)
@card_number = card_number
@valid = false
@card_numbers = card_numbers
@doubled_card_nums = doubled_card_nums
@nums_over_10_sum = nums_over_10_sum
@summed = summed
end
def validate
string_to_array
double_every_other
sum_double_digits
sum_all_digits
valid?
end
def string_to_array
card_numbers_array = @card_number.split("")
@card_numbers = card_numbers_array.map do |num|
num.to_i
end
end
def double_every_other
@doubled_card_nums = @card_numbers.map.with_index do |num, index|
if index.even?
num * 2
else
num
end
end
end
def sum_double_digits
@nums_over_10_sum = @doubled_card_nums.map do |num|
if num.digits.count > 1
num.digits.sum
else
num
end
end
end
def sum_all_digits
@summed = @nums_over_10_sum.sum
end
def valid?
if @summed % 10 == 0
@valid == true
puts "The number is good."
else
puts "The number is no good."
end
end
end
card_number = CreditCheck.new("4832890564887362")
card_number.validate
card_number_2 = CreditCheck.new("5541801923795240")
card_number_2.validate
card_number_3 = CreditCheck.new("4024007136512380")
card_number_3.validate