Java Your program will read in a credit card number and expiration date from the user and validate the data. The program will continue to ask for user input until a valid number and date are entered. The rules for what is valid are specified below. readYear() and readMonth(int) methods  Complete the readYear() and readMonth(int) methods. These methods ask the user to input the expiration year and month of their credit card. Each method should contain a loop inside the method so the code repeats until a valid month or year is entered by the user. If the user enters an invalid year or month, print an error message and ask them to try again. The readMonth(int) method reads in the month as a number. A valid month is any number between 1 and 12 (inclusive). A valid year is 2022 or later. readCreditCardNumber() method  Complete the readCreditCardNumber() method. This method asks the user to input their credit card number. This method should contain a loop so the code repeats until a valid credit card number is entered by the user. A number is valid if it passes three tests: It is 16 digits long. It starts with a 4, 5, or 6 as the first digit. It passes the checkSum test (described next). If the user enters an invalid number, print an error message describing the reason why the number is invalid and ask them to try again. Note that you do not have to account for a user entering non-numeric input. We expect your program to crash if this happens. passesCheckSum(String) method  Complete the passesCheckSum(String) method. The method determines if those numbers pass the checkSum test, which is described on the Check Sum Algorithm page and is attached as two pictures. This method assumes you already have a String that is 16 digits long. You will invoke this method from the readCreditCardNumber() method. Output  Print the valid card and date information back to the user. Display the credit card number in four-digit groupings with a space in between each grouping (e.g., 0000 0000 0000 0000). Display the month and year in "month/year" format (e.g., 12/2022, 4/2023). Code Design  Follow Java coding conventions and best practices. Follow naming conventions for variables, methods, and constants. Use constants to improve readability and maintainability. Properly indent your code. Design your code (including loops and conditionals) to reduce repeated or duplicated code. Choose conditional and loop structures that are the best logical match to the task. Write helper methods when it will help clarify the code or reduce repeated code. Below is the provided template: public class CreditCardValidator {     public static void main(String[] args) {         int year = readUserYear();         int month = readUserMonth(year);         long creditCardNumber = readUserCreditCardNumber();                 // YOUR CODE HERE: output credit card number and date in the specified format     }              public static int readUserYear() {         // YOUR CODE HERE         return 0; // placeholder: remove when you write your own code     }          public static int readUserMonth() {         // YOUR CODE HERE                return 0; // placeholder: remove when you write your own code     }          public static long readUserCreditCardNumber() {         // YOUR CODE HERE         return 0; // placeholder: remove when you write your own code     }          // this method assumes cardNumber has 16 digits     public static boolean passesCheckSum(String cardNumber) {         // YOUR CODE HERE         return false; // placeholder: remove when you write your own code         }     /*      * Below is a collection of helper methods you might or might not use.      */          // example: pass in the char '3' and return the int 3     public static int convertDigitCharToInt(char digit) {         return Integer.valueOf(String.valueOf(digit));     }          // example: pass in the String "4" and return the int 4     public static int convertDigitStringToInt(String digit) {         return Integer.valueOf(digit);     }          // example: pass in the int 5 and return the String "5"     public static String convertIntToString(int number) {         return Integer.toString(number);     }          // this method assumes the int parameter is a single digit; it will not work properly otherwise     // example: pass in the int 6 and return the char '6'     public static char convertSingleDigitIntToChar(int digit) {         return convertIntToString(digit).charAt(0);     } }

Computer Networking: A Top-Down Approach (7th Edition)
7th Edition
ISBN:9780133594140
Author:James Kurose, Keith Ross
Publisher:James Kurose, Keith Ross
Chapter1: Computer Networks And The Internet
Section: Chapter Questions
Problem R1RQ: What is the difference between a host and an end system? List several different types of end...
icon
Related questions
Question

Java

Your program will read in a credit card number and expiration date from the user and validate the data. The program will continue to ask for user input until a valid number and date are entered. The rules for what is valid are specified below.

readYear() and readMonth(int) methods 

Complete the readYear() and readMonth(int) methods. These methods ask the user to input the expiration year and month of their credit card.

  • Each method should contain a loop inside the method so the code repeats until a valid month or year is entered by the user.
  • If the user enters an invalid year or month, print an error message and ask them to try again.
  • The readMonth(int) method reads in the month as a number. A valid month is any number between 1 and 12 (inclusive).
  • A valid year is 2022 or later.

readCreditCardNumber() method 

Complete the readCreditCardNumber() method. This method asks the user to input their credit card number.

  • This method should contain a loop so the code repeats until a valid credit card number is entered by the user.
  • A number is valid if it passes three tests:
    • It is 16 digits long.
    • It starts with a 4, 5, or 6 as the first digit.
    • It passes the checkSum test (described next).
  • If the user enters an invalid number, print an error message describing the reason why the number is invalid and ask them to try again.
  • Note that you do not have to account for a user entering non-numeric input. We expect your program to crash if this happens.

passesCheckSum(String) method 

Complete the passesCheckSum(String) method. The method determines if those numbers pass the checkSum test, which is described on the Check Sum Algorithm page and is attached as two pictures.

  • This method assumes you already have a String that is 16 digits long.
  • You will invoke this method from the readCreditCardNumber() method.

Output 

Print the valid card and date information back to the user.

  • Display the credit card number in four-digit groupings with a space in between each grouping (e.g., 0000 0000 0000 0000).
  • Display the month and year in "month/year" format (e.g., 12/2022, 4/2023).

Code Design 

  • Follow Java coding conventions and best practices.
  • Follow naming conventions for variables, methods, and constants.
  • Use constants to improve readability and maintainability.
  • Properly indent your code.
  • Design your code (including loops and conditionals) to reduce repeated or duplicated code.
  • Choose conditional and loop structures that are the best logical match to the task.
  • Write helper methods when it will help clarify the code or reduce repeated code.

Below is the provided template:

public class CreditCardValidator {

    public static void main(String[] args) {
        int year = readUserYear();
        int month = readUserMonth(year);
        long creditCardNumber = readUserCreditCardNumber();        

        // YOUR CODE HERE: output credit card number and date in the specified format
    }
        
    public static int readUserYear() {
        // YOUR CODE HERE
        return 0; // placeholder: remove when you write your own code
    }
    
    public static int readUserMonth() {
        // YOUR CODE HERE       
        return 0; // placeholder: remove when you write your own code
    }
    
    public static long readUserCreditCardNumber() {
        // YOUR CODE HERE
        return 0; // placeholder: remove when you write your own code
    }
    
    // this method assumes cardNumber has 16 digits
    public static boolean passesCheckSum(String cardNumber) {
        // YOUR CODE HERE
        return false; // placeholder: remove when you write your own code    
    }


    /*
     * Below is a collection of helper methods you might or might not use.
     */
    
    // example: pass in the char '3' and return the int 3
    public static int convertDigitCharToInt(char digit) {
        return Integer.valueOf(String.valueOf(digit));
    }
    
    // example: pass in the String "4" and return the int 4
    public static int convertDigitStringToInt(String digit) {
        return Integer.valueOf(digit);
    }
    
    // example: pass in the int 5 and return the String "5"
    public static String convertIntToString(int number) {
        return Integer.toString(number);
    }
    
    // this method assumes the int parameter is a single digit; it will not work properly otherwise
    // example: pass in the int 6 and return the char '6'
    public static char convertSingleDigitIntToChar(int digit) {
        return convertIntToString(digit).charAt(0);
    }

}

Example 2: Invalid Card Number
Card number 1234567890123456 (invalid)
1234567890012345
Step 1: Starting with the first digit, multiply every other digit by 2. If the result is more than one digit, add those digits together.
1234567890012345
1+2=22
3*2=64
The numbers are now:
2264165890014385
5*2=
10
1+0=1
Step 2: Add up the new 15 digits and the check digit:
2+2
7*2
= 14
1+4= 5
+6+4+1+6+5+8+9+0+0+1+4+3+8+ 5 = 64
8
*+++
0*2=0|1
2*2=43
9*2=
18
1+8=9
Step 3: Check if this sum is evenly divisible by 10. It is not! So the number is invalid.
0
4*2=85
Transcribed Image Text:Example 2: Invalid Card Number Card number 1234567890123456 (invalid) 1234567890012345 Step 1: Starting with the first digit, multiply every other digit by 2. If the result is more than one digit, add those digits together. 1234567890012345 1+2=22 3*2=64 The numbers are now: 2264165890014385 5*2= 10 1+0=1 Step 2: Add up the new 15 digits and the check digit: 2+2 7*2 = 14 1+4= 5 +6+4+1+6+5+8+9+0+0+1+4+3+8+ 5 = 64 8 *+++ 0*2=0|1 2*2=43 9*2= 18 1+8=9 Step 3: Check if this sum is evenly divisible by 10. It is not! So the number is invalid. 0 4*2=85
Check Sum Algorithm
The check sum algorithm is one way to very that a credit card number is valid. This algorithm allows you to identify a credit card number that is invalid or has
been entered incorrectly.
Credit Card Numbers
The digits in a credit card number have a meaning. For most cards with 16 digits, the first 6 digits identify the company that issued the card. The next 9
digits represent the account number. The last digit, the 16th digit, is called the check digit.
The first 15 numbers can be compared to the 16th check digit to determine if the card number
The Algorithm
1. Starting with the first digit, multiply every other digit by 2 (so multiply the 1st digit, 3rd digit, 5th digit,..., 15th digit).
a. If the multiplication gives you a 2-digit number, add the digits of those numbers together.
b. Use this result to replace the original digit.
2. Add up the 15 digits and the check digit (16 digits total).
3. If this sum is evenly divisible by 10, the number is valid. If not, the number is invalid.
Example 1: Valid Card Number
Card number 5019717010103742 (valid)
5019717010103742
Step 1: Starting with the first digit, multiply every other digit by 2. If the result is more than one digit, add those digits together.
5019717010103742
5*2
= 10
1+0=1
0
1*2=29
The numbers are now:
50202
7*2
=14
1+4=5
1
7*2
= 14
1+4= 5
Step 2: Add up the new 15 digits and the check digit:
1+0+2+9+5+1 +5+0+2+0+2+0+ 6+ 7+8+2 = 50
0
valid.
Step 3: Check if this sum is evenly divisible by 10. It is! So the number is valid.
1*2=20
1*2=20
___
3*2=67
4*2=82
Transcribed Image Text:Check Sum Algorithm The check sum algorithm is one way to very that a credit card number is valid. This algorithm allows you to identify a credit card number that is invalid or has been entered incorrectly. Credit Card Numbers The digits in a credit card number have a meaning. For most cards with 16 digits, the first 6 digits identify the company that issued the card. The next 9 digits represent the account number. The last digit, the 16th digit, is called the check digit. The first 15 numbers can be compared to the 16th check digit to determine if the card number The Algorithm 1. Starting with the first digit, multiply every other digit by 2 (so multiply the 1st digit, 3rd digit, 5th digit,..., 15th digit). a. If the multiplication gives you a 2-digit number, add the digits of those numbers together. b. Use this result to replace the original digit. 2. Add up the 15 digits and the check digit (16 digits total). 3. If this sum is evenly divisible by 10, the number is valid. If not, the number is invalid. Example 1: Valid Card Number Card number 5019717010103742 (valid) 5019717010103742 Step 1: Starting with the first digit, multiply every other digit by 2. If the result is more than one digit, add those digits together. 5019717010103742 5*2 = 10 1+0=1 0 1*2=29 The numbers are now: 50202 7*2 =14 1+4=5 1 7*2 = 14 1+4= 5 Step 2: Add up the new 15 digits and the check digit: 1+0+2+9+5+1 +5+0+2+0+2+0+ 6+ 7+8+2 = 50 0 valid. Step 3: Check if this sum is evenly divisible by 10. It is! So the number is valid. 1*2=20 1*2=20 ___ 3*2=67 4*2=82
Expert Solution
trending now

Trending now

This is a popular solution!

steps

Step by step

Solved in 2 steps with 1 images

Blurred answer
Recommended textbooks for you
Computer Networking: A Top-Down Approach (7th Edi…
Computer Networking: A Top-Down Approach (7th Edi…
Computer Engineering
ISBN:
9780133594140
Author:
James Kurose, Keith Ross
Publisher:
PEARSON
Computer Organization and Design MIPS Edition, Fi…
Computer Organization and Design MIPS Edition, Fi…
Computer Engineering
ISBN:
9780124077263
Author:
David A. Patterson, John L. Hennessy
Publisher:
Elsevier Science
Network+ Guide to Networks (MindTap Course List)
Network+ Guide to Networks (MindTap Course List)
Computer Engineering
ISBN:
9781337569330
Author:
Jill West, Tamara Dean, Jean Andrews
Publisher:
Cengage Learning
Concepts of Database Management
Concepts of Database Management
Computer Engineering
ISBN:
9781337093422
Author:
Joy L. Starks, Philip J. Pratt, Mary Z. Last
Publisher:
Cengage Learning
Prelude to Programming
Prelude to Programming
Computer Engineering
ISBN:
9780133750423
Author:
VENIT, Stewart
Publisher:
Pearson Education
Sc Business Data Communications and Networking, T…
Sc Business Data Communications and Networking, T…
Computer Engineering
ISBN:
9781119368830
Author:
FITZGERALD
Publisher:
WILEY