Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Credit Card In your system you have the following class structure for the credit

ID: 3687137 • Letter: C

Question

Credit Card

In your system you have the following class structure for the credit cards:
     a class CreditCard,
     classes VisaCC, MasterCC, AmExCC that are all subclasses of CreditCard,
     you assume more subclasses for other credit card types will be added later on.
You now have to design the method(s) (and maybe additional classes) that verifies that the credit card number is a possible account number, and creates an instance of the appropriate credit card class.


Important details: Credit card numbers cannot exceed 19 digits, including a single check digit in the rightmost position. The exact algorithm for calculating the check digit as defined in ISO 2894/ANSI 4.13 is not important for this assignment. You can also determine the card issuer based on the credit card number:

Hint: you face here (at least) two problems, one has to do with how you figure out what kind of card a specific record is about, the other one with how you create the appropriate objects. Look at behavioural patterns and at creational patterns.

MasterCard First digit is a 5, second digit is in range 1 through 5 inclusive. Only valid lenght of number is 16 digits. Visa First digit is a 4. Length is either 13 or 16 digits. AmericanExpress First digit is a 3 and second digit a 4 or 7. Length is 15 digits. Discover First four digits are 6011. Length is 16 digits.

Explanation / Answer

Hello there ,

Please find below code with factory pattern and it's O/P.


public class CreditCard {
String number;

public CreditCard(String number) {
   super();
   this.number = number;
}

public CreditCard() {
   super();
}

}

/**
* Represents Master Credit Card.
* @author dipal.prajapati
*
*/
public class MasterCC extends CreditCard {

   public MasterCC(String cardNumber) {
       super.number=cardNumber;
   }

   @Override
   public String toString() {
       return "MasterCC ["+super.number+"]";
   }

}

/**
* Represents Visa Credit Card.
* @author dipal.prajapati
*
*/
public class VisaCC extends CreditCard {

   public VisaCC(String cardNumber) {
       super.number=cardNumber;
   }

   @Override
   public String toString() {
       return "VisaCC ["+super.number+"]";
   }

}

/**
* Represents Discover Credit Card.
* @author dipal.prajapati
*
*/
public class DiscoverCC extends CreditCard {

   public DiscoverCC(String cardNumber) {
       super.number=cardNumber;
   }

   @Override
   public String toString() {
       return "DiscoverCC ["+super.number+"]";
   }

}

/**
* Represents AmericanExpress Credit Card.
* @author dipal.prajapati
*
*/
public class AmExCC extends CreditCard {

   public AmExCC(String cardNumber) {
       super.number=cardNumber;
   }

   @Override
   public String toString() {
       return "AmExCC ["+super.number+"]";
   }

}

/**
* Factory class to generate appropriate instance of Card.
* @author dipal.prajapati
*
*/
public class CardFactory {

   public CreditCard getCard(int cardType, String aCard) {
       CreditCard card = null;
       switch (cardType) {
       case 0:
           card = new VisaCC(aCard);
           break;
       case 1:
           card = new MasterCC(aCard);
           break;
       case 2:
           card = new AmExCC(aCard);
           break;
       case 3:
           card = new DiscoverCC(aCard);
           break;
       }
       return card;
   }
}

import java.io.BufferedReader;
import java.io.InputStreamReader;

/**
* Credit card validation App.
*
* @author dipal.prajapati
*
*/
public class CreditCardValidation {
   public static final int INVALID = -1;
   public static final int VISA = 0;
   public static final int MASTERCARD = 1;
   public static final int AMERICAN_EXPRESS = 2;
   public static final int EN_ROUTE = 4;
   public static final int DINERS_CLUB = 5;
   public static final int DISCOVER = 3;

   private static final String[] cardNames = { "Visa", "Mastercard",
           "American Express", "Discover" };

   /**
   * Valid a Credit Card number
   */
   public static boolean validCC(String number) throws Exception {
       int CardID;
       if ((CardID = getCardID(number)) != -1)
           return validCCNumber(number);
       return false;
   }

   /**
   * Get the Card type returns the credit card type INVALID = -1; VISA = 0;
   * MASTERCARD = 1; AMERICAN_EXPRESS = 2; DISCOVER = 3
   */
   public static int getCardID(String number) {
       int valid = INVALID;

       String digit1 = number.substring(0, 1);
       String digit2 = number.substring(0, 2);
       String digit3 = number.substring(0, 3);
       String digit4 = number.substring(0, 4);

       if (isNumber(number)) {
           /*
           * ----* VISA prefix=4* ---- length=13 or 16 (can be 15 too!?!
           * maybe)
           */
           if (digit1.equals("4")) {
               if (number.length() == 13 || number.length() == 16)
                   valid = VISA;
           }
           /*
           * ----------* MASTERCARD prefix= 51 ... 55* ---------- length= 16
           */
           else if (digit2.compareTo("51") >= 0 && digit2.compareTo("55") <= 0) {
               if (number.length() == 16)
                   valid = MASTERCARD;
           }

           /*
           * ----* DISCOVER card prefix = 60* -------- lenght = 16* left as an
           * exercise ...
           */
           else if (digit4.equals("6011")) {
               if (number.length() == 16)
                   valid = DISCOVER;
           }
           /*
           * ----* AMEX prefix=34 or 37* ---- length=15
           */
           else if (digit2.equals("34") || digit2.equals("37")) {
               if (number.length() == 15)
                   valid = AMERICAN_EXPRESS;
           }

       }

       return valid;

   }

   public static boolean isNumber(String n) {
       try {
           double d = Double.valueOf(n).doubleValue();
           return true;
       } catch (NumberFormatException e) {
           e.printStackTrace();
           return false;
       }
   }

   public static String getCardName(int id) {
       return (id > -1 && id < cardNames.length ? cardNames[id] : "");
   }

   public static boolean validCCNumber(String n) {
       try {
           /*
           * * known as the LUHN Formula (mod10)
           */
           int j = n.length();

           String[] s1 = new String[j];
           for (int i = 0; i < n.length(); i++)
               s1[i] = "" + n.charAt(i);

           int checksum = 0;

           for (int i = s1.length - 1; i >= 0; i -= 2) {
               int k = 0;

               if (i > 0) {
                   k = Integer.valueOf(s1[i - 1]).intValue() * 2;
                   if (k > 9) {
                       String s = "" + k;
                       k = Integer.valueOf(s.substring(0, 1)).intValue()
                               + Integer.valueOf(s.substring(1)).intValue();
                   }
                   checksum += Integer.valueOf(s1[i]).intValue() + k;
               } else
                   checksum += Integer.valueOf(s1[0]).intValue();
           }
           return ((checksum % 10) == 0);
       } catch (Exception e) {
           e.printStackTrace();
           return false;
       }
   }

   /*
   * * For testing purpose
   * *
   */
   public static void main(String args[]) throws Exception {

       BufferedReader input = new BufferedReader(new InputStreamReader(
               System.in));
       System.out.print("Card number : ");
       String aCard = input.readLine().trim();
       int cardType = getCardID(aCard);
       CreditCard card = null;
       CardFactory cardFactory = new CardFactory();
       if ((validCC(aCard))) {

           card = cardFactory.getCard(cardType, aCard);
           System.out.println("This is a " + getCardName(cardType) + " card. "
                   + card.toString());
       }

       else
           System.out.println("This card is invalid or unsupported!");
   }
}

=====O/P====

Card number : 4388576018410707
This is a Visa VisaCC [4388576018410707]

Card number : 6011000593748746
This is a Discover card. DiscoverCC [6011000593748746]

Card number : 4388576018402625
This card is invalid or unsupported!

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote