Pre-lab Write the pseudocode to determine the credit card issuer with the follow
ID: 3675794 • Letter: P
Question
Pre-lab
Write the pseudocode to determine the credit card issuer with the following requirements:
Credit begins with
Card Provider/Issuer
3
American Express
4
Visa
5
MasterCard
6
Discover
other
Specialty cards
lab
Write a class called Credit Card Issuer. Implement your pseudo code solution written
for Pre-Lab in a method called determine Credit Card Issuer. The method should
Accept a character representing the first character in a 16-digit credit card number,
and return the appropriate credit card issuer name. Any variables needed should be declared locally.
Create a Credit Card Tester with a main method. Request a 16-digit credit card number from the user, call the determine Credit Card Issuer method and pass the first character in the credit card number to get the credit card issuer. Print the issuer’s name returned by the method. Sample run Enter a credit card number: 5123223456379456Issuer: MasterCard
Credit begins with
Card Provider/Issuer
3
American Express
4
Visa
5
MasterCard
6
Discover
other
Specialty cards
Explanation / Answer
main.java
import java.util.Scanner;
public class main {
public static void main(String[] args) {
Scanner in = new Scanner(System.in);
CreditCard card2 = new CreditCard();
do {
System.out.println("Please enter card number:");
card2.setNumber(in.nextLine());
System.out.println("Credit Card Issuer: " +card2.getCardType());
System.out.println("Continue? (y/N)");
} while (in.nextLine().toUpperCase().equals("Y"));
in.close();
}
}
CreditCard.java
public class CreditCard {
private String number;
private static final int MIN_LENGTH = 13;
private static final int MAX_LENGTH = 19;
/**
* Default constructor
* Construct a blank credit card (null)
*/
public CreditCard() {
this.number = null;
}
/**
* Constructs a credit card with the given card number
* @param numberStr the number of the credit card
*/
public CreditCard(String numberStr) {
this.number = numberStr;
}
/**
* Constructs a credit card with the given card number
* @param numberInt
* The number of the credit card as Integer
*/
public CreditCard(int numberInt) {
this.number = String.valueOf(numberInt);
}
/**
* Set the number of the credit card
* @param numberStr
* The number of the credit card as a string.
*/
public void setNumber(String numberStr) {
this.number = numberStr;
}
/**
* Set the number of the credit card
* @param numberInt
* The number of the credit card as Integer.
*/
public void setNumber(int numberInt) {
this.number = String.valueOf(numberInt);
}
/**
* Get the number of the credit card
* @return The number of the credit card as String.
*/
public String getNumber() {
return this.number;
}
/**
* Checks if the number matches a known card issuer.
* @return The credit card issuer as a String.
* "Unknown Issuer" if no issuer prefix matches.
*/
public String getCardType() {
if (this.isVisa())
return "VISA";
if (this.isMasterCard())
return "MasterCard";
if (this.isDiscover())
return "Discover";
if (this.isAmericanExpress())
return "American Express";
if (this.isSpecialty())
return "Specialty";
return "Unknown Issuer";
}
/**
* Check whether card number matches the VISA prefix or not.
* @return true if card is a VISA
*/
public boolean isVisa() {
// VISA Check; starts with 4 ; length 13,16 ; eg. 4111 1111 1111 1111
return this.number.startsWith("4");
}
/**
* Check whether card number matches the Master Card prefix or not.
* @return true if card is a Master Card
*/
public boolean isMasterCard() {
// MasterCard Check ; starts with 51,52,53,54,55
// length 16; eg. 5500 0000 0000 0004
return this.number.startsWith("51") ||
this.number.startsWith("52") ||
this.number.startsWith("53") ||
this.number.startsWith("54") ||
this.number.startsWith("55");
}
/**
* Check whether card number matches the Discover Card prefix or not.
* @return true if card is a Discover Card
*/
public boolean isDiscover() {
// Discover Card Check ; starts with 6011 ;
// 6011, 622126 to 622925, 644, 645, 646, 647, 648, 649, 65
// eg. 6011 0000 0000 0004; length 16
if ( this.number.startsWith("65") ||
this.number.startsWith("644") ||
this.number.startsWith("645") ||
this.number.startsWith("646") ||
this.number.startsWith("647") ||
this.number.startsWith("648") ||
this.number.startsWith("649") ||
this.number.startsWith("6011") )
return true;
for (int i = 622126; i <= 622925; i++) {
if (this.number.startsWith(Integer.toString(i)))
return true;
}
return false;
}
/**
* Check whether card number matches the AmEx prefix or not.
* @return true if card is an AmEx
*/
public boolean isAmericanExpress() {
// American Express12 34,37 15 3400 0000 0000 009
// Length 15
return this.number.startsWith("34") ||
this.number.startsWith("37");
}
/**
* Check whether card number matches the Maestro Card prefix or not.
* @return true if card is a Maestro Card
*/
public boolean isSpecialty() {
// Maestro 5018, 5020, 5038, 5893, 6304, 6759, 6761, 6762, 6763 ;
// length 16-19
return this.number.startsWith("5018") ||
this.number.startsWith("5020") ||
this.number.startsWith("5038") ||
this.number.startsWith("5893") ||
this.number.startsWith("6304") ||
this.number.startsWith("6759") ||
this.number.startsWith("6761") ||
this.number.startsWith("6762") ||
this.number.startsWith("6763");
}
}
sample output
Please enter card number:
5123223456379456
Credit Card Issuer: MasterCard
Continue? (y/N)
N
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.