Created
December 18, 2014 16:04
-
-
Save AllPurposeName/8cad4cae3e0c775122ce to your computer and use it in GitHub Desktop.
DJ's Credit Check and Tester
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' | |
class CreditCheck | |
attr_accessor :input | |
attr_accessor :numerizer | |
attr_accessor :multiplier | |
attr_accessor :sum | |
attr_accessor :reducables | |
attr_accessor :validater | |
attr_accessor :converter | |
def initialize | |
@converter = [] | |
@multiplier = [] | |
@converter = [] | |
@sum = [] | |
@reducables = [] | |
@input = "" | |
end | |
def input(pass_to_input) | |
#puts "Please enter your credit card digits with only numericals (0-9)" | |
# input = gets.chomp.reverse | |
@input = pass_to_input.reverse | |
#binding.pry | |
end | |
def numerize | |
@numerizer = [] | |
#binding.pry | |
@numerizer = @input.split('') | |
#binding.pry | |
# for later method? if input.include?('a','b','c','d','e','f','g','h','i','j','k','l','m','n','o','p','q','r','s','t','u','v','w''x')#,'y','z','!',"'",'@','#','$','%','&','*','(',')','_','[','{','}',']','|','\','?','/','>','.','<',',','`','~') | |
end | |
def int_convert | |
@converter = [] | |
#binding.pry | |
numerizer.each do |num| | |
@converter << num.to_i | |
end | |
#binding.pry | |
end | |
# => Method I wish I had: | |
# convert string to array of integers | |
# if cant because not ints, puts statement rejecting, retake.get | |
def multiply | |
@multiplier = [] | |
count = 1 | |
@converter.each do |num| | |
if count % 2 == 0 | |
@multiplier << num.to_i * 2 | |
else | |
@multiplier << num.to_i | |
end | |
count = count + 1 | |
end | |
#binding.pry | |
end | |
def reduction | |
@reducables = [] | |
#binding.pry | |
@multiplier.each do |num| | |
if num > 9 | |
@reducables << num - 9 | |
else | |
@reducables << num | |
end | |
end | |
#binding.pry | |
end | |
def valid | |
@sum = [] | |
#binding.pry | |
@sum = @reducables.reduce(:+) | |
#binding.pry | |
if @sum.nil? | |
valider = false | |
puts "The number is invalid!!" | |
elsif @sum % 10 == 0 | |
valider = true | |
puts "The number is valid!" | |
else | |
valider = false | |
puts "The number is invalid!" | |
end | |
# binding.pry | |
end | |
def validate(pass_to_input) | |
input(pass_to_input) | |
numerize | |
int_convert | |
multiply | |
reduction | |
valid | |
# binding.pry | |
end | |
end | |
creditee = CreditCheck.new | |
creditee.validate("") |
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' | |
require_relative 'credit_check' | |
require 'minitest/autorun' | |
require 'minitest/pride' | |
class CreditCheckTest < MiniTest::Test | |
def test_empty_string_is_invalid | |
checker = CreditCheck.new | |
result = checker.validate("") | |
assert_equal "The number is invalid!!", result | |
end | |
def test_string_of_zero_is_valid | |
checker = CreditCheck.new | |
result = checker.validate("0") | |
assert_equal true, result | |
end | |
def test_string_of_one_is_invalid | |
checker = CreditCheck.new | |
result = checker.validate("1") | |
assert_equal false, result | |
end | |
def test_string_of_42_is_valid | |
checker = CreditCheck.new | |
result = checker.validate("42") | |
binding.pry | |
assert_equal true, result | |
end | |
def test_string_of_4242_is_valid | |
checker = CreditCheck.new | |
result = checker.validate("4242") | |
assert_equal true, result | |
end | |
def test_string_credit_card_length_of_42s_is_valid | |
checker = CreditCheck.new | |
result = checker.validate("4242424242424242") | |
assert_equal true, result | |
end | |
def test_string_of_59_is_valid | |
checker = CreditCheck.new | |
result = checker.validate("59") | |
assert_equal true, result | |
end | |
def test_string_of_5959_is_valid | |
checker = CreditCheck.new | |
result = checker.validate("5959") | |
assert_equal true, result | |
end | |
def test_string_of_58_is_invalid | |
checker = CreditCheck.new | |
result = checker.validate("58") | |
assert_equal false, result | |
end | |
def test_actual_credit_card_number_is_valid | |
checker = CreditCheck.new | |
result = checker.validate("4095831209480129") | |
assert_equal true, result | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment