1+ package com .codefortomorrow .intermediate .chapter11 .practice ;
2+
3+ /*
4+ Credit card numbers follow certain patterns. A credit card number
5+ must have between 13 and 16 digits. It must start with:
6+ ■ 4 for Visa cards
7+ ■ 5 for Master cards
8+ ■ 37 for American Express cards
9+ ■ 6 for Discover cards
10+
11+ In 1954, Hans Luhn of IBM proposed an algorithm for validating credit card
12+ numbers. The algorithm is useful to determine whether a card number is entered
13+ correctly or whether a credit card is scanned correctly by a scanner. Credit card
14+ numbers are generated following this validity check, commonly known as the
15+ Luhn check or the Mod 10 check, which can be described as follows
16+ (for illustration, consider the card number 4388576018402626):
17+
18+ 1. Double every second digit from right to left. If doubling of a digit results in a
19+ two-digit number, add up the two digits to get a single-digit number.
20+ 4388576018402626
21+ 2 * 2 = 4
22+ 2 * 2 = 4
23+ 4 * 2 = 8
24+ 1 * 2 = 2
25+ 6 * 2 = 12 (1 + 2 = 3)
26+ 5 * 2 = 10 (1 + 0 = 1)
27+ 8 * 2 = 16 (1 + 6 = 7)
28+ 4 * 2 = 8
29+
30+ 2. Now add all single-digit numbers from Step 1.
31+ 4 + 4 + 8 + 2 + 3 + 1 + 7 + 8 = 37
32+
33+ 3. Add all digits in the odd places from right to left in the card number.
34+ 6 + 6 + 0 + 8 + 0 + 7 + 8 + 3 = 38
35+
36+ 4. Sum the results from Step 2 and Step 3.
37+ 37 + 38 = 75
38+
39+ 5. If the result from Step 4 is divisible by 10, the card number is valid; otherwise,
40+ it is invalid. For example, the number 4388576018402626 is invalid, but the
41+ number 4388576018410707 is valid.
42+
43+ Write a program that prompts the user to enter a credit card number as a long
44+ integer. Display whether the number is valid or invalid. Design your program to
45+ use the following methods:
46+
47+ Returns true if the card number is valid
48+ public static boolean isValid(long number)
49+
50+ Gets the result from Step 2
51+ public static int sumOfDoubleEvenPlace(long number)
52+
53+ Returns this number if it is a single digit, otherwise,
54+ return the sum of the two digits
55+ public static int getDigit(int number)
56+
57+ Returns sum of odd-place digits in number
58+ public static int sumOfOddPlace(long number)
59+
60+ Returns true if the digit d is a prefix for number
61+ public static boolean prefixMatched(long number, int d)
62+
63+ Returns the number of digits in d
64+ public static int getSize(long d)
65+
66+ Returns the first k number of digits from number. If the
67+ number of digits in number is less than k, return number.
68+ public static long getPrefix(long number, int k)
69+
70+ Here are sample runs of the program:
71+
72+ Enter a credit card number: 4388576018410707
73+ 4388576018410707 is valid
74+
75+ Enter a credit card number: 4388576018402626
76+ 4388576018402626 is invalid
77+
78+ Bonus points if you can also print to the user
79+ what type of card the number is, and for writing JavaDoc comments.
80+
81+ Note: If you need more card numbers to test, you can look here:
82+ https://www.simplify.com/commerce/docs/testing/test-card-numbers
83+ (or simply search "test credit card numbers" on Google).
84+
85+ Note: The practice template has method stubs written for you.
86+ Simply delete the default return values and start coding!
87+
88+ Adapted from Exercise 6.31, Introduction to Java Programming (Comprehensive),
89+ 10th ed. by Y. Daniel Liang
90+ */
91+
92+ public class CreditCard {
93+ public static void main (String [] args ) {
94+ // write code here
95+ }
96+
97+ /** Returns true if the card number is valid */
98+ public static boolean isValid (long number ) {
99+ return false ;
100+ }
101+
102+ /** Returns the sum of the doubled even-place digits, from right to left */
103+ public static int sumOfDoubleEvenPlace (long number ) {
104+ return 0 ;
105+ }
106+
107+ /**
108+ * Returns the given number if it is a single digit,
109+ * otherwise return the sum of the two digits
110+ */
111+ public static int getDigit (int n ) {
112+ return 0 ;
113+ }
114+
115+ /** Returns the sum of the odd-place digits, from right to left */
116+ public static int sumOfOddPlace (long number ) {
117+ return 0 ;
118+ }
119+
120+ /** Return true if d is a prefix for a number */
121+ public static boolean prefixMatched (long number , int d ) {
122+ return false ;
123+ }
124+
125+ /** Returns the number of digits in the given number */
126+ public static int getSize (long number ) {
127+ return 0 ;
128+ }
129+
130+ /**
131+ * Returns the first k number of digits from number.
132+ * If the number of digits in number is less than k,
133+ * returns number.
134+ */
135+ public static long getPrefix (long number , int k ) {
136+ return 0 ;
137+ }
138+ }
0 commit comments