Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /**
- * This class represents a credit card number
- * with methods for validation.
- *
- * @author Lykos
- * @version 11.28.2012
- */
- 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)
- {
- this.cardNumber = cardNumber;
- }
- /**
- * Accessor method to return the cardNumber
- *
- * @return A string that is the credit card number
- */
- public String getCardNumber()
- {
- return cardNumber;
- }
- /**
- * Mutator method to set the cardNumber
- *
- * @param cardNumber A credit card number
- */
- public void setCardNumber(String cardNumber)
- {
- this.cardNumber = 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()
- {
- //buttload of if statements?
- String cardType = "Unknown";
- if(cardNumber.startsWith("4"))
- {
- if(cardNumber.length() == 13 || cardNumber.length() == 16)
- {
- cardType = "Visa";
- }
- }
- if(cardNumber.startsWith("51") || cardNumber.startsWith("52") || cardNumber.startsWith("53") || cardNumber.startsWith("54") || cardNumber.startsWith("55"))
- {
- if(cardNumber.length() == 16)
- {
- cardType = "MasterCard";
- }
- }
- if(cardNumber.startsWith("34") || cardNumber.startsWith("37"))
- {
- if(cardNumber.length() == 15)
- {
- cardType = "American Express";
- }
- }
- if(cardNumber.startsWith("6011"))
- {
- if(cardNumber.length() == 16)
- {
- cardType = "Discover";
- }
- }
- return cardType;
- }
- /**
- * 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)
- {
- if(number < 10) {
- return number;
- }
- else {
- if(number == 10) {
- number = 1;
- }
- if(number == 12) {
- number = 3;
- }
- if(number == 14) {
- number = 5;
- }
- if(number == 16) {
- number = 7;
- }
- if(number == 18) {
- number = 9;
- }
- return number;
- }
- }
- /**
- * 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()
- {
- int sum = 0;
- for(int index = 1; index < cardNumber.length(); index += 2)
- {
- int digit = Integer.parseInt(cardNumber.substring(cardNumber.length() - (index + 1), cardNumber.length() - index));
- digit = digit * 2;
- digit = getDigit(digit);
- sum += digit;
- }
- return sum;
- }
- /**
- * 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()
- {
- int sum = 0;
- for(int index = 0; index < cardNumber.length(); index += 2)
- {
- int digit = Integer.parseInt(cardNumber.substring(cardNumber.length() - (index + 1), cardNumber.length() - index));
- sum += digit;
- }
- return sum;
- }
- /**
- * 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()
- {
- if((sumEverySecondDigitRightToLeft() + sumOfOddPlaceDigitsRightToLeft()) % 10 == 0)
- {
- return true;
- }
- else
- {
- return false;
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment