Skip to content

Commit 9c03cdf

Browse files
author
serhatleventyavas
committed
implements CreditCardValidation
1 parent b6829f8 commit 9c03cdf

File tree

4 files changed

+70
-0
lines changed

4 files changed

+70
-0
lines changed

EasyValidationManager.xcodeproj/project.pbxproj

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
objects = {
88

99
/* Begin PBXBuildFile section */
10+
045A9F5B24194CB700DDD1AC /* CreditCardValidation.swift in Sources */ = {isa = PBXBuildFile; fileRef = 045A9F5A24194CB700DDD1AC /* CreditCardValidation.swift */; };
1011
0499107C240AADA70082E521 /* EasyValidationManager.framework in Frameworks */ = {isa = PBXBuildFile; fileRef = 04991072240AADA70082E521 /* EasyValidationManager.framework */; };
1112
04991081240AADA70082E521 /* EasyValidationManagerTests.swift in Sources */ = {isa = PBXBuildFile; fileRef = 04991080240AADA70082E521 /* EasyValidationManagerTests.swift */; };
1213
04991083240AADA70082E521 /* EasyValidationManager.h in Headers */ = {isa = PBXBuildFile; fileRef = 04991075240AADA70082E521 /* EasyValidationManager.h */; settings = {ATTRIBUTES = (Public, ); }; };
@@ -33,6 +34,7 @@
3334
/* End PBXContainerItemProxy section */
3435

3536
/* Begin PBXFileReference section */
37+
045A9F5A24194CB700DDD1AC /* CreditCardValidation.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = CreditCardValidation.swift; sourceTree = "<group>"; };
3638
04991072240AADA70082E521 /* EasyValidationManager.framework */ = {isa = PBXFileReference; explicitFileType = wrapper.framework; includeInIndex = 0; path = EasyValidationManager.framework; sourceTree = BUILT_PRODUCTS_DIR; };
3739
04991075240AADA70082E521 /* EasyValidationManager.h */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.h; path = EasyValidationManager.h; sourceTree = "<group>"; };
3840
04991076240AADA70082E521 /* Info.plist */ = {isa = PBXFileReference; lastKnownFileType = text.plist.xml; path = Info.plist; sourceTree = "<group>"; };
@@ -147,6 +149,7 @@
147149
isa = PBXGroup;
148150
children = (
149151
049910A8240AAEFD0082E521 /* PhoneValidation.swift */,
152+
045A9F5A24194CB700DDD1AC /* CreditCardValidation.swift */,
150153
);
151154
path = Validations;
152155
sourceTree = "<group>";
@@ -302,6 +305,7 @@
302305
buildActionMask = 2147483647;
303306
files = (
304307
049910A6240AAEE50082E521 /* Country.swift in Sources */,
308+
045A9F5B24194CB700DDD1AC /* CreditCardValidation.swift in Sources */,
305309
049910A9240AAEFD0082E521 /* PhoneValidation.swift in Sources */,
306310
049910AE240AAF2B0082E521 /* JsonParser.swift in Sources */,
307311
);
Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
import Foundation
2+
3+
public class CreditCardValidation {
4+
5+
private init() {}
6+
7+
public static func validCreditCard(cardNumber: String) -> Bool {
8+
if cardNumber.trimmingCharacters(in: .whitespacesAndNewlines).count != 16 {
9+
return false
10+
}
11+
12+
let cardNumberArray = Array(cardNumber)
13+
var numberList: [Int] = []
14+
15+
let lastDigit: Int = Int(String(cardNumberArray[cardNumberArray.count - 1]))!
16+
17+
for index in 0...cardNumberArray.count - 2 {
18+
let number: Int = Int(String(cardNumberArray[index]))!
19+
numberList.append(number)
20+
}
21+
22+
numberList.reverse()
23+
24+
for index in 0..<numberList.count {
25+
if (index % 2 == 0) {
26+
let number = numberList[index]
27+
numberList[index] = number * 2
28+
}
29+
}
30+
31+
for index in 0..<numberList.count {
32+
if (index % 2 == 0) {
33+
let number = numberList[index]
34+
if (number > 9) {
35+
numberList[index] = number - 9
36+
}
37+
}
38+
}
39+
40+
var totalSum = 0
41+
42+
for index in 0..<numberList.count {
43+
let number = numberList[index]
44+
totalSum += number
45+
}
46+
47+
let resultOfMod10 = (totalSum + lastDigit) % 10
48+
return resultOfMod10 == 0
49+
}
50+
51+
}

EasyValidationManagerTests/EasyValidationManagerTests.swift

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -54,4 +54,19 @@ class EasyValidationManagerTests: XCTestCase {
5454
let result: Bool = PhoneValidation.validPhoneNumber(country: country, phoneNumber: "5551231233")
5555
XCTAssertEqual(result, true)
5656
}
57+
58+
func test_givenCreditWrongCreditCard_returnFalse() {
59+
let result: Bool = CreditCardValidation.validCreditCard(cardNumber: "4532030566401450")
60+
XCTAssertEqual(result, false)
61+
}
62+
63+
func test_givenCreditCorrectCreditCard_returnTrue() {
64+
let result: Bool = CreditCardValidation.validCreditCard(cardNumber: "4556737586899855")
65+
XCTAssertEqual(result, true)
66+
}
67+
68+
func test_givenCreditAnotherCorrectCreditCard_returnTrue() {
69+
let result: Bool = CreditCardValidation.validCreditCard(cardNumber: "4175001382805488")
70+
XCTAssertEqual(result, true)
71+
}
5772
}

0 commit comments

Comments
 (0)