My program instructions are below, but I don\'t need help with all of it. I only
ID: 3554679 • Letter: M
Question
My program instructions are below, but I don't need help with all of it. I only need help with bolded and underlined area. I need to learning how to get my program to run three times and then quit. Any help will be greatly appreciated. I will post my program below (everything works perfectly except the three strikes and you're out).
**********************************************************************************************************************************
In addition to the output listed in the book, your program must ask the user for the amount to be charged. The output will indicate the type of card used (Visa, MasterCard, etc.), the card number and amount charged. However, if the card number is invalid the output will inform the user and ask the user to reenter a valid card number. The user will be given three attempts to enter a valid card number. After a third unsuccessful attempt, the program will inform the user that his/her account has been frozen and to contact customer service for assistance.
After either a successful entry or the user's third unsuccessful attempt, the program will terminate.
(Financial: credit card number validation) Credit card numbers follow certain patterns. A credit card number must have between 13 and 16 digits. It must start with:
4 for Visa cards
5 for Master cards
37 for American Express cards
6 for Discover cards
In 1954, Hans Luhn of IBM proposed an algorithm for validating credit card numbers. The algorithm is useful to determine whether a card number is entered correctly or whether a credit card is scanned correctly by a scanner. Credit card numbers are generated following this validity check, commonly known as the Luhn check or the Mod 10 check, which can be described as follows (for illustration, consider the card number 4388576018402626):
1. Double every second digit from right to left. If doubling of a digit results in a two-digit number, add up the two digits to get a single-digit number.
4388576018402626
2*2=4
2*2=4
4*2=8
1*2=2
6*2=12 (1+2=3)
5*2=10 (1+0=1)
8*2=16 (1+6=7)
4*2=8
2. Now add all single-digit numbers from Step 1.
4 + 4 + 8 + 2 + 3 + 1 + 7 + 8 = 37
3. Add all digits in the odd places from right to left in the card number.
6 + 6 + 0 + 8 + 0 + 7 + 8 + 3 = 38
4. Sum the results from Step 2 and Step 3.
37 + 38 = 75
5. If the result from Step 4 is divisible by 10, the card number is valid: otherwise, it is invalid. For example, the number 4388576018410707 is valid.
0 + 0 + 8+ 2 + 3 + 1+ 7+8 = 29
7+7+1+8+0+7+8+3=41
29+41=70 70/10=7 Dividable by 10 makes it valid
Write a program that prompts the user to enter a credit card number as a long integer. Display whether the number is valid or invalid. Design your program to use the following methods:
/** Return true if the card number is valid */
public static Boolean isValid (long number)
/** Get the result from Step 2. */
public static int sumOfDoubleEvenPlace (long number)
/** Return this number if it is a single digit, otherwise, return the sum of the two digits */
public static int getDigit (int number)
/** Return sum of odd-place digits in number */
public static int sumOfOddPlace (long number)
/** Return true if the digit d is a prefix for number */
public static boolean prefixMatched (long number, int d)
/** Return the number of digits in d */
public static int getSize (long d)
/** Return the first k number of digits from number. If the number of digits in number is less than k, return number. */
public static long getPrefix (long number, int k)
*****************************************************************************************************************************
My program is below...
******************************************************************************************************************************
import javax.swing.JOptionPane;
public class CreditCardNumberValidationNew{
public static void main (String[] args){
greeting();
count();
beginAgain();
sayGoodbye();
}
//*************************************************************************************************
public static void greeting(){
JOptionPane.showMessageDialog(null, "This department checks to see if your card number is valid, oversees " +
"your transactions, and outputs the results.", "Welcome to the Credit Card " +
"Validation Department!", JOptionPane.INFORMATION_MESSAGE);
JOptionPane.showMessageDialog(null, "We only accept the four major credit cards: " +
"Visa Card " + "Master Card " + "American Express " + "Discover Card",
"Welcome to the Credit Card Validation Department!", JOptionPane.INFORMATION_MESSAGE);
return;
}
//*************************************************************************************************
public static void count(){
int count = 1;
getCardNumber(count);
return;
}
//*********************************************
public static void getCardNumber(int i){
// Declarations
long cardNumber;
String cardNumberString;
// endDeclaraions
cardNumberString = JOptionPane.showInputDialog(null, "Please enter your credit card number without dashes, " +
"for example: 1234567891011121: ", "Credit Card Validation Department",
JOptionPane.QUESTION_MESSAGE);
cardNumber = Long.parseLong(cardNumberString);
if (isValid (cardNumber) == false){
for (int counting = i; counting < 3; counting++){
invalidCard(cardNumber, counting);
}
}
return;
}
//*************************************************************************************************
public static boolean isValid (long number){
if (((sumOfDoubleEvenPlace (number) + sumOfOddPlace (number)) % 10 == 0) &&
(getSize (number) >= 13 && getSize (number) <= 16) &&
(prefixMatched (number, 4) || prefixMatched (number, 5) || prefixMatched (number, 37) || prefixMatched (number, 6))){
return true;
}
else {
return false;
}
}
//*************************************************************************************************
public static void threeWrongEntries(long number){ // 3 strikes & you're out
// wrong entry
JOptionPane.showMessageDialog (null, "Your entry of " + number + " is not a valid credit card number. " + "As " +
"this is your third wrong entry, your account has been temporarily frozen. " +
"Please contact Customer Service for assistance between the hours of 8 am and " +
"4:30 pm. " + "The system will now end.", "Customer Service 1-800-555-4203",
JOptionPane.INFORMATION_MESSAGE); // wrong card # customer service
System.exit(0);
return;
}
//*************************************************************************************************
public static int sumOfDoubleEvenPlace (long number){
// Declarations
int numberOfDigits;
int sum;
// endOfDeclarations
numberOfDigits = getSize(number) -1;
sum = 0;
number /= 10;
for (int count = 0; count < numberOfDigits; count += 2){
sum += getDigit ((int) (2 * (number % 10)));
number /= 100;
}
return sum;
}
//*************************************************************************************************
// Return this number if it is a single digit, otherwise, return the sum of the two digits
public static int getDigit (int number){
return ((number - number % 10) / 10) + number % 10;
}
//*************************************************************************************************
public static int sumOfOddPlace (long number){
// Declarations
int numOfDigits;
int sum;
// endDeclarations
numOfDigits = getSize (number);
sum = 0;
for (int count = 0; count < numOfDigits; count += 2){
sum += number % 10;
}
return sum;
}
//*************************************************************************************************
public static boolean prefixMatched (long number, int d){
if (getPrefix (number, getSize(d)) == d){
switch (d){
case 4: String cardType = "Visa";
outputResults (cardType, number, d);
break;
case 5: cardType = "Master";
outputResults (cardType, number, d);
break;
case 6: cardType = "Discover";
outputResults (cardType, number, d);
break;
default: cardType = "American Express";
outputResults (cardType, number, d);
break;
}
return true;
}
else{
return false;
}
}
public static int getSize (long d){
// Declarations
int count;
// endDeclarations
count = 0;
while(d != 0) {
d /= 10;
count++;
}
return count;
}
public static long getPrefix (long number, int k){
// Declarations
int numOfDigits;
// endDeclarations
numOfDigits = getSize (number);
if (numOfDigits - k > 0){
for (int count = 0; count < numOfDigits - k; count++){
number /= 10;
}
return number;
}
else{
return number;
}
}
public static void invalidCard (long number, int counter){
if (counter <= 2){
JOptionPane.showMessageDialog (null, "Your card number : " + number + " is not a valid card number. " +
"This is your " + counter + " time(s) entering an invalid card number. " + "After your third wrong attempt, " + "your account will be frozen and you will be asked to contact customer service to reactivate your account.",
"Welcome to the Credit Card Validation Department!", JOptionPane.INFORMATION_MESSAGE);
getCardNumber(counter);
}
threeWrongEntries(number);
return;
}
public static double getAmount (){
// Declarations
double amount;
String getAmountString;
// endDeclarations
getAmountString = JOptionPane.showInputDialog (null, "What amount would you like to charge today?", "Welcome to the Credit Card Validation Department", JOptionPane.QUESTION_MESSAGE);
amount = Double.parseDouble (getAmountString);
return amount;
}
public static void outputResults(String type, long cardNum, int digits){
JOptionPane.showMessageDialog (null, "Your " + type + " card number is " + cardNum +
", and the amount you have charged is $" + getAmount() + ". " + "Your " +
"charge has been validated. " + "Please keep a copy of this for your records.",
"Credit Card Validation Department", JOptionPane.INFORMATION_MESSAGE);
return;
}
public static void beginAgain(){
// Declarations
boolean begin;
String beginString;
// endDeclarations
beginString = JOptionPane.showInputDialog (null, "Would you like to begin again? Press Y for yes.",
"Credit Card Validation Department", JOptionPane.QUESTION_MESSAGE);
begin = "Y".equalsIgnoreCase (beginString);
if (begin == true){
count();
}
return;
}
public static void sayGoodbye(){
JOptionPane.showMessageDialog (null, "Thanks for using the Credit Card Number Validation program. " +
"Please come again.", "Credit Card Validation Department",
JOptionPane.INFORMATION_MESSAGE);
return;
}
}
Explanation / Answer
/*You need two changes
1.) In getCardNumber method, change the loop terminating condition as counting <= 3
2.) In invalidCard method, increment the counter by 1.
*/
import javax.swing.JOptionPane;
public class CreditCardNumberValidationNew{
public static void main (String[] args){
greeting();
count();
beginAgain();
sayGoodbye();
}
//*************************************************************************************************
public static void greeting(){
JOptionPane.showMessageDialog(null, "This department checks to see if your card number is valid, oversees " +
"your transactions, and outputs the results.", "Welcome to the Credit Card " +
"Validation Department!", JOptionPane.INFORMATION_MESSAGE);
JOptionPane.showMessageDialog(null, "We only accept the four major credit cards: " +
"Visa Card " + "Master Card " + "American Express " + "Discover Card",
"Welcome to the Credit Card Validation Department!", JOptionPane.INFORMATION_MESSAGE);
return;
}
//*************************************************************************************************
public static void count(){
int count = 1;
getCardNumber(count);
return;
}
//*********************************************
public static void getCardNumber(int i){
// Declarations
long cardNumber;
String cardNumberString;
// endDeclaraions
cardNumberString = JOptionPane.showInputDialog(null, "Please enter your credit card number without dashes, " +
"for example: 1234567891011121: ", "Credit Card Validation Department",
JOptionPane.QUESTION_MESSAGE);
cardNumber = Long.parseLong(cardNumberString);
if (isValid (cardNumber) == false){
for (int counting = i; counting <= 3; counting++){
invalidCard(cardNumber, counting);
}
}
return;
}
//*************************************************************************************************
public static boolean isValid (long number){
if (((sumOfDoubleEvenPlace (number) + sumOfOddPlace (number)) % 10 == 0) &&
(getSize (number) >= 13 && getSize (number) <= 16) &&
(prefixMatched (number, 4) || prefixMatched (number, 5) || prefixMatched (number, 37) || prefixMatched (number, 6))){
return true;
}
else {
return false;
}
}
//*************************************************************************************************
public static void threeWrongEntries(long number){ // 3 strikes & you're out
// wrong entry
JOptionPane.showMessageDialog (null, "Your entry of " + number + " is not a valid credit card number. " + "As " +
"this is your third wrong entry, your account has been temporarily frozen. " +
"Please contact Customer Service for assistance between the hours of 8 am and " +
"4:30 pm. " + "The system will now end.", "Customer Service 1-800-555-4203",
JOptionPane.INFORMATION_MESSAGE); // wrong card # customer service
System.exit(0);
return;
}
//*************************************************************************************************
public static int sumOfDoubleEvenPlace (long number){
// Declarations
int numberOfDigits;
int sum;
// endOfDeclarations
numberOfDigits = getSize(number) -1;
sum = 0;
number /= 10;
for (int count = 0; count < numberOfDigits; count += 2){
sum += getDigit ((int) (2 * (number % 10)));
number /= 100;
}
return sum;
}
//*************************************************************************************************
// Return this number if it is a single digit, otherwise, return the sum of the two digits
public static int getDigit (int number){
return ((number - number % 10) / 10) + number % 10;
}
//*************************************************************************************************
public static int sumOfOddPlace (long number){
// Declarations
int numOfDigits;
int sum;
// endDeclarations
numOfDigits = getSize (number);
sum = 0;
for (int count = 0; count < numOfDigits; count += 2){
sum += number % 10;
}
return sum;
}
//*************************************************************************************************
public static boolean prefixMatched (long number, int d){
if (getPrefix (number, getSize(d)) == d){
switch (d){
case 4: String cardType = "Visa";
outputResults (cardType, number, d);
break;
case 5: cardType = "Master";
outputResults (cardType, number, d);
break;
case 6: cardType = "Discover";
outputResults (cardType, number, d);
break;
default: cardType = "American Express";
outputResults (cardType, number, d);
break;
}
return true;
}
else{
return false;
}
}
public static int getSize (long d){
// Declarations
int count;
// endDeclarations
count = 0;
while(d != 0) {
d /= 10;
count++;
}
return count;
}
public static long getPrefix (long number, int k){
// Declarations
int numOfDigits;
// endDeclarations
numOfDigits = getSize (number);
if (numOfDigits - k > 0){
for (int count = 0; count < numOfDigits - k; count++){
number /= 10;
}
return number;
}
else{
return number;
}
}
public static void invalidCard (long number, int counter){
if (counter <= 2){
JOptionPane.showMessageDialog (null, "Your card number : " + number + " is not a valid card number. " +
"This is your " + counter + " time(s) entering an invalid card number. " + "After your third wrong attempt, " + "your account will be frozen and you will be asked to contact customer service to reactivate your account.",
"Welcome to the Credit Card Validation Department!", JOptionPane.INFORMATION_MESSAGE);
counter++;
getCardNumber(counter);
}
threeWrongEntries(number);
return;
}
public static double getAmount (){
// Declarations
double amount;
String getAmountString;
// endDeclarations
getAmountString = JOptionPane.showInputDialog (null, "What amount would you like to charge today?", "Welcome to the Credit Card Validation Department", JOptionPane.QUESTION_MESSAGE);
amount = Double.parseDouble (getAmountString);
return amount;
}
public static void outputResults(String type, long cardNum, int digits){
JOptionPane.showMessageDialog (null, "Your " + type + " card number is " + cardNum +
", and the amount you have charged is $" + getAmount() + ". " + "Your " +
"charge has been validated. " + "Please keep a copy of this for your records.",
"Credit Card Validation Department", JOptionPane.INFORMATION_MESSAGE);
return;
}
public static void beginAgain(){
// Declarations
boolean begin;
String beginString;
// endDeclarations
beginString = JOptionPane.showInputDialog (null, "Would you like to begin again? Press Y for yes.",
"Credit Card Validation Department", JOptionPane.QUESTION_MESSAGE);
begin = "Y".equalsIgnoreCase (beginString);
if (begin == true){
count();
}
return;
}
public static void sayGoodbye(){
JOptionPane.showMessageDialog (null, "Thanks for using the Credit Card Number Validation program. " +
"Please come again.", "Credit Card Validation Department",
JOptionPane.INFORMATION_MESSAGE);
return;
}
}
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.