Guest User

CC

a guest
Dec 2nd, 2012
140
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 5.33 KB | None | 0 0
  1. /**
  2.  * This class represents a credit card number
  3.  * with methods for validation.
  4.  *
  5.  * @author Lykos
  6.  * @version 11.28.2012
  7.  */
  8. public class CreditCard
  9. {
  10.     private String cardNumber;
  11.  
  12.     /**
  13.      * Constructor for objects of class CreditCard.
  14.      *
  15.      * @param  cardNumber   A credit card number used to initialize the field
  16.      *
  17.      */
  18.     public CreditCard(String cardNumber)
  19.     {
  20.         this.cardNumber = cardNumber;
  21.  
  22.     }
  23.  
  24.     /**
  25.      * Accessor method to return the cardNumber
  26.      *
  27.      * @return  A string that is the credit card number
  28.      */
  29.     public String getCardNumber()
  30.     {
  31.         return cardNumber;
  32.     }
  33.  
  34.     /**
  35.      * Mutator method to set the cardNumber
  36.      *
  37.      * @param  cardNumber   A credit card number
  38.      */
  39.     public void setCardNumber(String cardNumber)
  40.     {
  41.  
  42.         this.cardNumber = cardNumber;
  43.  
  44.     }
  45.  
  46.     /**
  47.      * Using the leading digits and the length of the card number, this method
  48.      * determines if the card type is either "Visa", "MasterCard", "American Express", or "Discover"
  49.      * or an unknown card type.
  50.      *
  51.      * @return  The credit card type, either "Visa", "MasterCard", "American Express", or "Discover"
  52.      *          or the string "Unknown" if the type cannot be found.
  53.      *  
  54.      */
  55.     public String creditCardType()
  56.     {
  57.         //buttload of if statements?
  58.         String cardType = "Unknown";
  59.         if(cardNumber.startsWith("4"))
  60.         {
  61.             if(cardNumber.length() == 13 || cardNumber.length() == 16)
  62.             {
  63.                 cardType = "Visa";
  64.             }
  65.         }
  66.         if(cardNumber.startsWith("51") || cardNumber.startsWith("52") || cardNumber.startsWith("53") || cardNumber.startsWith("54") || cardNumber.startsWith("55"))
  67.         {
  68.             if(cardNumber.length() == 16)
  69.             {
  70.                 cardType = "MasterCard";
  71.             }
  72.         }
  73.         if(cardNumber.startsWith("34") || cardNumber.startsWith("37"))
  74.         {
  75.             if(cardNumber.length() == 15)
  76.             {
  77.                 cardType = "American Express";
  78.             }
  79.         }
  80.         if(cardNumber.startsWith("6011"))
  81.         {
  82.             if(cardNumber.length() == 16)
  83.             {
  84.                 cardType = "Discover";
  85.             }
  86.         }
  87.  
  88.         return cardType;
  89.     }
  90.  
  91.     /**
  92.      * Determins whether the number parameter is a single or double digit number.
  93.      * If it is a single digit number, return that number, otherwise
  94.      * return the sum of the two digits in the number parameter
  95.      *
  96.      * @param number  A single or double digit integer
  97.      * @return        An integer, either the single digit number parameter
  98.      *                or the sum of the two digits of the double digit parameter
  99.      */
  100.     public int getDigit(int number)
  101.     {
  102.         if(number < 10) {
  103.             return number;
  104.         }
  105.         else {
  106.             if(number == 10) {
  107.                 number = 1;
  108.             }
  109.             if(number == 12) {
  110.                 number = 3;
  111.             }
  112.             if(number == 14) {
  113.                 number = 5;
  114.             }
  115.             if(number == 16) {
  116.                 number = 7;
  117.             }
  118.             if(number == 18) {
  119.                 number = 9;
  120.             }
  121.             return number;
  122.         }
  123.     }
  124.  
  125.     /**
  126.      * This method returns the total of doubling every second digit from right to left.
  127.      * If doubling of a digit results in a two-digit
  128.      * number, add up the two digit to get a single digit number and add to the total.
  129.      *
  130.      * @return  An integer which is the sum of the digits.
  131.      */
  132.     public int sumEverySecondDigitRightToLeft()
  133.     {
  134.         int sum = 0;
  135.         for(int index = 1; index < cardNumber.length(); index += 2)
  136.         {
  137.             int digit = Integer.parseInt(cardNumber.substring(cardNumber.length() - (index + 1), cardNumber.length() - index));
  138.             digit = digit * 2;
  139.             digit = getDigit(digit);
  140.             sum += digit;
  141.         }
  142.  
  143.         return sum;
  144.     }
  145.  
  146.     /**
  147.      * The method calculates the sum of the digits in the odd positions of the
  148.      * credit card string, going from right to left, where the last number
  149.      * in the credit card string is counted as the first position.
  150.      *
  151.      * @return  An integer which is the sum of the digits.
  152.      */
  153.     public int sumOfOddPlaceDigitsRightToLeft()
  154.     {
  155.         int sum = 0;
  156.         for(int index = 0; index < cardNumber.length(); index += 2)
  157.         {
  158.             int digit = Integer.parseInt(cardNumber.substring(cardNumber.length() - (index + 1), cardNumber.length() - index));
  159.             sum += digit;
  160.         }
  161.         return sum;
  162.     }
  163.  
  164.     /**
  165.      * The method determines if the credit card number is valid.
  166.      * If the card is valid, the total of sumEverySecondDigitRightToLeft()
  167.      * and sumOfOddPlaceDigitsRightToLeft()
  168.      * should be divisible by 10 with no remainder.
  169.      *
  170.      * @return  A boolean value of true if the card number is valid,
  171.      *          false, otherwise.
  172.      */
  173.     public boolean isValid()
  174.     {
  175.        
  176.         if((sumEverySecondDigitRightToLeft() + sumOfOddPlaceDigitsRightToLeft()) % 10 == 0)
  177.         {
  178.             return true;
  179.         }
  180.         else
  181.         {
  182.             return false;
  183.         }
  184.  
  185.        
  186.     }
  187.  
  188. }
Advertisement
Add Comment
Please, Sign In to add comment