Answered step by step
Verified Expert Solution
Link Copied!
Question
1 Approved Answer

COSC Homework, credit card validation!!! Here is the CreditCard Class, public class CreditCard { private String cardNumber; /** * Constructor for objects of class CreditCard.

COSC Homework, credit card validation!!!

Here is the CreditCard Class,

public class CreditCard { private String cardNumber;

/** * Constructor for objects of class CreditCard. * * @param cardNumber A credit card number used to initialize the field * */

public CreditCard(String cardNumber) { } /** * Accessor method to return the cardNumber * * @return A string that is the credit card number */ public String getCardNumber() { return null; } /** * Mutator method to set the cardNumber * * @param cardNumber A credit card number */ public void setCardNumber(String cardNumber) { } /** * Using the leading digits and the length of the card number, this method * determines if the card type is either "Visa", "MasterCard", "American Express", or "Discover" * or an unknown card type. * * @return The credit card type, either "Visa", "MasterCard", "American Express", or "Discover" * or the string "Unknown" if the type cannot be found. * */ public String creditCardType() { return null; } /** * Determins whether the number parameter is a single or double digit number. * If it is a single digit number, return that number, otherwise * return the sum of the two digits in the number parameter * * @param number A single or double digit integer * @return An integer, either the single digit number parameter * or the sum of the two digits of the double digit parameter */ public int getDigit(int number) { return 0; } /** * This method returns the total of doubling every second digit from right to left. * If doubling of a digit results in a two-digit * number, add up the two digit to get a single digit number and add to the total. * * @return An integer which is the sum of the digits. */ public int sumEverySecondDigitRightToLeft() { return 0; } /** * The method calculates the sum of the digits in the odd positions of the * credit card string, going from right to left, where the last number * in the credit card string is counted as the first position. * * @return An integer which is the sum of the digits. */ public int sumOfOddPlaceDigitsRightToLeft() { return 0; } /** * The method determines if the credit card number is valid. * If the card is valid, the total of sumEverySecondDigitRightToLeft() * and sumOfOddPlaceDigitsRightToLeft() * should be divisible by 10 with no remainder. * * @return A boolean value of true if the card number is valid, * false, otherwise. */ public boolean isValid() { return true; }

}

Here is the ProcessCardNumbers class

import java.io.*; /** * Class ProcessCardNumbers reads a text file of * credit card numbers and determines of the * numbers are valid, invalid, or if the number * is an unknown credit card type. * * @author (your name) */ public class ProcessCardNumbers {

/** * Write a main method to process a text file of credit cards (cardNumbers.txt). * The method should read each card number from the file, print the card number * and whether or not the card is valid, invalid or and unknown card type. * Use exception handing to catch FileNotFoundExceptions and IOExceptions. */ public static void main() { }

}

Here's the algorithm,

Credit Card Validation

Credit card numbers follow certain patterns for length and starting digits:

Visa cards length 13 or 16, must start with 4,

Master cards length 16, must start with 51 - 55

American Express cards length 15, must start with 34 or 37

Discover cards length 16, must start with 6011

In 1954, Hans Luhn of IBM proposed an algorithm for validating credit card numbers. The algorithm is useful to determine if a card number is entered correctly or if a credit card is scanned correctly by a scanner. Almost all credit card numbers are generated following this validity check, commonly known as the Luhn check or the Mod 10 check, which can be described as follows (for illustration, consider the card number 4388576018402625):

Double every second digit from right to left. If doubling of a digit results in a two-digit number, add up the two digits to get a single-digit number.

2 * 2 = 4

2 * 2 = 4

4 * 2 = 8

1 * 2 = 2

6 * 2 = 12 (1 + 2 = 3)

5 * 2 = 10 (1 + 0 = 1)

8 * 2 = 16 (1 + 6 = 7)

4 * 2 = 8

Now add all the single digit numbers from Step 1.

4 + 4 + 8 + 2 + 3 + 1 + 7 + 8 = 37

Add all digits in the odd places from right to left in the card number.

5 + 6 + 0 + 8 + 0 + 7 + 8 + 3 = 37

Sum the results from Step 2 and Step 3

37 + 37 = 74

If the result from Step 4 is divisible by 10, the card number is valid; otherwise, it is invalid. For example, the number 4388576018402625 is invalid, but the number 4388576018410707 is valid.

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image
Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image_2

Step: 3

blur-text-image_3

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Intelligent Information And Database Systems Asian Conference Aciids 2012 Kaohsiung Taiwan March 19 21 2012 Proceedings Part 3 Lnai 7198

Authors: Jeng-Shyang Pan ,Shyi-Ming Chen ,Ngoc-Thanh Nguyen

2012th Edition

3642284922, 978-3642284922

More Books

Students explore these related Databases questions