Hello I am having some trouble with my programming homework. The assignment is:
ID: 3679652 • Letter: H
Question
Hello I am having some trouble with my programming homework. The assignment is:
First your program should ask if the credit card is MasterCard or visa card. Then ask the 16 digit credit card number. Then add all the digits in the credit card number and get the modulo 10. If the modulo 10 of the summation of all the digits is zero, then its valid visa card. If the modulo 10 of the summation of all the digits is 1 then its valid MasterCard. Otherwise, the number customer has entered is an invalid card number. Write a Java program that reads the card type and the card number and then determine if the card number entered is a valid card number. If the card number is a valid card, display the message “valid card number”. Otherwise, display the message “invalid card number”.
For example, say the card number is 1234 5678 6789 1235 and its visa card. To validate it, first add all the digits 1+2+3+4+ 5+6+7+8+ 6+7+8+9 +1+2+3+5 = 77 then get mod 10 of 77 (77%10 = 7) Since it’s not zero, this is not a valid visa card number.
Requirements:
A)
You need to read the card number as a single string and each 4 numbers in the card must be separated by a space. For example, following is a correct input
1234 5678 6789 1235
B)
Card number must have 16 digits. If not, display the error message
“incorrect card number”
For the code can you please use relational and boolean operators, data comparison, selection, and repition (loops). Please dont use anything too complex, because I am only in a beginners coding class. Thank you.
Explanation / Answer
Please find the below desired program for said requirement.
public class VisaORMasterCard {
/**
* @param args
*/
public static void main(String[] args) throws Exception{
// TODO Auto-generated method stub
InputStreamReader read = new InputStreamReader(System.in);
BufferedReader in = new BufferedReader(read);
System.out.print("Choose your choice for card type. : 1) Master Card 2) Visa Card Please Enter your Choice: ");
// get card type input as a String
String cardTypeChoice = in.readLine();
if(cardTypeChoice.equals("1")){
System.out.println("User Choice is Master Card");
} else if(cardTypeChoice.equals("2")){
System.out.println("User Choice is Visa Card");
}
System.out.println("Please Enter your card number: ");
// get card number input as a String
String cardNumber = in.readLine();
System.out.println("Card Number "+cardNumber);
// card number separated by Space
String cardNo[] = cardNumber.split("\s+");;
int cardNumberLength = 0;
// Calculate entered card number length
for(int i=0; i<cardNo.length; i++){
cardNumberLength = cardNumberLength + cardNo[i].length();
}
System.out.println("Card Number Length: "+cardNumberLength);
if(cardNumberLength != 16){
System.out.println("Invalid card number.. Please enter 16 digit card number");
return;
}
else{
int sum = 0;
boolean flag = false;
for(int i=0; i<cardNo.length; i++){
for(int j = 0; j < cardNo[i].length(); j++) {
if(Character.isDigit(cardNo[i].charAt(j))) {
sum = sum + Integer.parseInt(""+cardNo[i].charAt(j));
}
else{
System.out.println("Invalid card number. Card number may contain the chracters other than digits");
flag = true;
break;
}
}
}
if(flag == false){
System.out.println("Card Digits total sum : "+sum);
if(sum % 10 == 0){
System.out.println("The card number which u entered is Visa card");
} else if(sum % 10 == 1){
System.out.println("The card number which u entered is Master card");
} else{
System.out.println("The card number which u entered is incorrect card number");
}
}
}
}
}
Compile:
Open cmd prompt and go to the file where ths program has been saved.
> javac VisaORMasterCard.java
> java VisaORMasterCard
Output1:
Choose your choice for card type. :
1) Master Card 2) Visa Card
Please Enter your Choice: 1
User Choice is Master Card
Please Enter your card number:
1234 5678 6789 1235
Card Number 1234 5678 6789 1235
Card Number Length: 16
Card Digits total sum : 77
The card number which u entered is incorrect card number
Output2:
Choose your choice for card type. :
1) Master Card 2) Visa Card
Please Enter your Choice: 2
User Choice is Visa Card
Please Enter your card number:
1234 5678 6789 1201
Card Number 1234 5678 6789 1201
Card Number Length: 16
Card Digits total sum : 70
The card number which u entered is Visa card
Output3:
Choose your choice for card type. :
1) Master Card 2) Visa Card
Please Enter your Choice: 1
User Choice is Master Card
Please Enter your card number:
1234 5678 6789 1202
Card Number 1234 5678 6789 1202
Card Number Length: 16
Card Digits total sum : 71
The card number which u entered is Master card
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.