Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

below is my java code for the following assignment below. i have to edit the following i have to make sure that All Condition listed

below is my java code for the following assignment below.

i have to edit the following

i have to make sure that All Condition listed in Constraints should be private methods

Consider using the .replaceAll() method to remove slashes and/or dashes from entered Strings

Set methods must be private

All Condition listed in Constraints should be private methods

Use a regex String for Condition 1

Most Condition methods will use an if statement

Condition methods 4-6 will need one to two for loops to step through the int arrays

Consider using the .split() method when converting the Strings to int Arrays

The .parseInt() method and the Integer class can be your friend in this assignment

Assignment

a program that checks to see if a credit card number is valid.

Constraints

The credit card number (CC#) must be 16 digits in length

The CC# can be in either form: ####-####-####-#### or ################

The Expiration Date (Exp) must be in the form: MM/YY (Example: 12/15)

Notify user of correct entry form for CC# and Exp

Name, CC#, and Exp must be entered as Strings

Use a separate (external to the main class) subclass, VerifyCard() to validate the CC# with the following private method conditions:

  • Condition 1: The first digit must be a 4
  • Condition 2: The fourth digit must be one greater than the fifth digit
  • Condition 3: The product of the first, fifth, and ninth digits must be 24
  • Condition 4: The sum of all digits must be evenly divisible by 4
  • Condition 5: The sum of the four digits in the expiration date must be less than the product of the last two digits of the card number
  • Condition 6: The sum of the first four digits must be one less than the sum of the last four digits
  • Condition 7: If you treat the first two digits as a two-digit number, and the 15th and 16th digits as a two digit number, their sum must be 100 (Example: 4643262531465454 -> 46 + 54 = 100)

Requirements

Ask the user for the name on the credit card

Ask the user for the credit card number

Ask the user for the expiration date

Use set and get methods in the subclass for the information entered by the user

Set methods must be private

All Condition listed in Constraints should be private methods

Use a regex String for Condition 1

Most Condition methods will use an if statement

Condition methods 4-6 will need one to two for loops to step through the int arrays

This program will utilize code learned from Week 1 through Week 10

Hints

CC# and Exp can be converted into int Arrays

The following CC#s can be used as test cases. Each of these numbers will return as valid. Make sure you change them around or enter other CC#s that are not valid:

  • 4192112566331259
  • 4283253533222358
  • 4374398522116157
  • 4292154566732358
  • 4553223534333555
  • 4643262531465454
  • 4732169566119053
  • 4823287533234752

Each of the CC#s will work with an Exp of 12/15. By increasing the YY, you may find that a few of these cards will not be valid

Consider using the .replaceAll() method to remove slashes and/or dashes from entered Strings

Consider using the .split() method when converting the Strings to int Arrays

The .parseInt() method and the Integer class can be your friend in this assignment

Expected Output

The sample output below has executed three runs of the program. User input is in red.

Enter Card Holder's Name: Tom Jones Enter Credit Card Number (No dashes): 4553223534333555 Enter Credit Card Expiration Date (MM/YY): 12/15

Card number: 4553223534333555 is a valid credit card.

Run program again? (Y for Yes, N for No): y Enter Card Holder's Name: Dean Martin Enter Credit Card Number (No dashes): 4732169566119053 Enter Credit Card Expiration Date (MM/YY): 12/15

Card number: 4732169566119053 is a valid credit card.

Run program again? (Y for Yes, N for No): Y Enter Card Holder's Name: Frank Sinatra Enter Credit Card Number (No dashes): 4234253533211358 Enter Credit Card Expiration Date (MM/YY): 12/15

Card number: 4234253533211358 is NOT a valid credit card.

Run program again? (Y for Yes, N for No): n Thank you. Goodbye!

import java.util.Scanner; public static void main(String[] args) {

//Declare object for the scanner class Scanner input = new Scanner(System.in);

//declare variables String aname;

String anumber;

String adate;

String choice;

//create object card of the class VerifyCard VerifyCard card = new VerifyCard();

//start do-while loop do {

//Prompt the message and input the values System.out.print("Enter Card Holder's Name: ");

aname = input.nextLine();

System.out.print("Enter Credit Card Number (No dashes): ");

anumber = input.nextLine();

//use replaceAll() method to remove dashes or slashes. String cardNumber = anumber.replaceAll("[^0-9]", "");

System.out.print("Enter Credit Card Expiration Date (MM/YY): ");

adate = input.nextLine();

//use replaceAll() method to remove slashes. String exDate = adate.replaceAll("[^0-9]", "");

card.displayValidity(cardNumber, exDate);

System.out.print(" Run Program again?(Y for Yes , N for No):"); choice = input.nextLine(); } while (choice.equalsIgnoreCase("y"));

System.out.println("Thank you.Goodbye!"); System.exit(0);

}

}

subclass

public class VerifyCard {

private int[] number; private String expDate; //set the number

private void setNumber(String n) { String num[] = n.split("(?!^)"); number = new int[16]; for (int i = 0; i < 16; i++) { number[i] = Integer.parseInt(num[i]); } } //set the date

private void setExpDate(String n) { expDate = n; } //Method to check whether first digit is 4 or not

private boolean checkfirstdigit() { String num = ""; for (int i = 0; i < number.length; i++) { num += number[i]; }

if (num.matches("^4([0-9]*)")) { return true; } else { return false; } } //Method to check whether fourth digit is greater than fifth

private boolean checkFourthGrtrFiftth() { int fourthdigit = number[3]; int fifthdigit = number[4]; if (fourthdigit > fifthdigit) { return true; } else { return false; } } //Method to check whether product of first,fifth and ninth is 24

private boolean checkProd159() { int product = 0; int firstdigit = number[0]; int fifthdigit = number[4]; int ninthdigit = number[8]; //calculate product product = firstdigit * fifthdigit * ninthdigit; //Check product is 24 or not if (product == 24) { return true; } else { return false; } } //Method to check The sum of all digits must be evenly divisible by 4

private boolean sumDivBy4() { int sum = 0; for (int i = 0; i < number.length; i++) { sum += number[i]; } if (sum % 4 == 0) { return true; } else { return false; } } //Method to check The sum of the four digits in the expiration date must be less //than the product of the last two digits of the card number

private boolean sum4LessProd2digits() { int sum = 0; int produnct = 0; for (int i = 0; i < expDate.length(); i++) { sum += (int) expDate.charAt(i) - '0'; } int lDig1 = number[number.length - 2]; int lDig2 = number[number.length - 1]; produnct = lDig1 * lDig2; if (sum < produnct) { return true; } else { return false; } } //Method to check that The sum of the first four digits must //be one less than the sum of the last four digits

private boolean sumFirst4LessSumLast4() { int firstsum = 0, lastsum = 0; for (int i = 0; i < 4; i++) { firstsum += number[i]; } for (int i = 4; i > 0; i--) { lastsum += number[number.length - i]; } if (lastsum == (firstsum + 1)) { return true; } else { return false; } } //Method to check whether sum of first two digit product and //last two digit is equal to 100 or not

private boolean first2Last2Sum100() { int firstdigit = number[0]; int seconddigit = number[1]; int f = firstdigit * 10; int f2 = f + seconddigit; int lastdigit = number[number.length - 1]; int last2digit = number[number.length - 2]; int z = last2digit * 10; int z2 = z + lastdigit; //calculate sum int val = f2 + z2; //check for equality whether true or not if (val == 100) { return true; } else { return false; } } //Define the method to display card validity

public void displayValidity(String num, String date) { setNumber(num); setExpDate(date); if (num.length() == 16 && checkfirstdigit() && checkFourthGrtrFiftth() && checkProd159() && sumDivBy4() && sum4LessProd2digits() && sumFirst4LessSumLast4() && first2Last2Sum100()) { System.out.print("Card number :"); for (int i = 0; i < number.length; i++) { System.out.print(number[i]); } System.out.print(" is a valid credit card"); } else { System.out.print("Card number :"); for (int i = 0; i < number.length; i++) { System.out.print(number[i]); } System.out.print(" is not a valid credit card");

} }

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image_2

Step: 3

blur-text-image_3

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Modern Dental Assisting

Authors: Doni Bird, Debbie Robinson

13th Edition

978-0323624855, 0323624855

Students also viewed these Programming questions