Hi all you smart people of Chegg, I have been working on this assignment which w
ID: 3861117 • Letter: H
Question
Hi all you smart people of Chegg, I have been working on this assignment which was previously answered incorrectly and did not meet the requirements of what is needing to be built. Can someone please assist in helping me figure this out. I am in over my head and need help with how to implement the regex in method 1 and the set methods. The previous person who posted the wrong fix also used boolean to work through the subclass and I wasn't sure if that is how that should have been done.
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:
o Condition 1: The first digit must be a 4
o Condition 2: The fourth digit must be one greater than the fifth digit
o Condition 3: The product of the first, fifth, and ninth digits must be 24
o Condition 4: The sum of all digits must be evenly divisible by 4
o 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
o Condition 6: The sum of the first four digits must be one less than the sum of the last four digits
o 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:
o 4192112566331259
o 4283253533222358
o 4374398522116157
o 4292154566732358
o 4553223534333555
o 4643262531465454
o 4732169566119053
o 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!
Explanation / Answer
Given below is the java code as per specification. Output is shown in the end. Please don't forget to rate the answer if it helped. Thank you.
VerifyCard.java
public class VerifyCard {
private int[] cardNumber;
private int[] expiryDate;
public boolean validate(String cardNum, String expDate)
{
boolean valid = false;
if(cardNum == null || expDate == null)
return false;
if(condition1(cardNum) && expDate.matches("\d\d/\d\d")) //pattern is dd/dd, use double slash to escape
{
cardNumber = convert(cardNum.replaceAll("-", ""));
expiryDate = convert(expDate.replace("/", ""));
valid = condition2() && condition3() && condition4() && condition5() && condition6() && condition7();
}
return valid;
}
private int[] convert(String str)
{
int[] intArr = new int[str.length()];
for(int i = 0; i < intArr.length; i++)
intArr[i] = str.charAt(i) - '0';
return intArr;
}
//use a regex to check if cardnumber is a 16 digit number beginning with 4 and having optional - for every 4 digits
private boolean condition1(String cardNum)
{
String pattern = "4\d\d\d[-]?\d\d\d\d[-]?\d\d\d\d[-]?\d\d\d\d"; //[-]? indicates an optional hypen and d represents digit in java regex
return cardNum.matches(pattern);
}
//returns true if 4th digit is 1 more than the 5th digit, false otherwise
private boolean condition2()
{
//subtract ascii value of '0' to convert to int
int fourth = cardNumber[3]; //get 4th digit
int fifth = cardNumber[4];// get 5th digit
return fourth - 1 == fifth;
}
//returns true if the product of 1st , 5th and 9th digit is 24
private boolean condition3()
{
int first = cardNumber[0] ; //get 1st digit
int fifth = cardNumber[4] ;// get 5th digit
int ninth = cardNumber[8] ;// get 9th digit
return first * fifth * ninth == 24;
}
//returns true if sum of all digits is divisble by 4
private boolean condition4()
{
int digit;
int sum = 0;
for(int i = 0; i < cardNumber.length; i++)
{
digit = cardNumber[i];
sum += digit;
}
return sum % 4 == 0;
}
//returns true if the sum of digits in expiry date is less than product of last 2 digits
private boolean condition5()
{
int sum = 0, product = 0;
for(int i = 0; i < 4; i++) //calculate sum of four digits of expiry date
sum += expiryDate[i];
product = cardNumber[14] * cardNumber[15];//product of last 2 digits in card number
return sum < product;
}
//returns true if the sum of first 4 digits is 1 less than sum of last 4 digits
private boolean condition6()
{
int sum1 = 0, sum2 = 0;
for(int i = 0; i < 4; i++) //calculate sum of 1st four digits
sum1 += cardNumber[i] ;
for(int i = 12; i < 16; i++) //calculate sum of last four digits
sum2 += cardNumber[i];
return sum1 == sum2 - 1;
}
//return true if sum of first 2 digits and last 2 digits is 100
private boolean condition7()
{
int n1 = cardNumber[0] * 10 + cardNumber[1];
int n2 = cardNumber[14] * 10 + cardNumber[15];
return n1 + n2 == 100;
}
}
CreditCardMain.java
import java.util.Scanner;
public class CreditCardMain {
public static void main(String[] args) {
VerifyCard cardVerifier = new VerifyCard();
String[] cardNos = {"4192-1125-6633-1259", "4283253533222358", "4374398522116157", "4292154566732358",
"4553223534333555","4643262531465454", "4732169566119053", "4823287533234752"};
String expDate = "12/17";
for( int i = 0; i < cardNos.length; i++)
{
System.out.println("Card Number: " + cardNos[i]
+ " Expiry Date: " + expDate
+ " Valid: " + cardVerifier.validate(cardNos[i], expDate));
}
String ans = "y";
String name, cardNum;
Scanner scanner = new Scanner(System.in);
while(ans.equalsIgnoreCase("y"))
{
System.out.print("Enter Card Holder's name: ");
name = scanner.nextLine().trim();
System.out.print("Enter Credit Card Number: ");
cardNum = scanner.next();
System.out.print("Enter Credit Card Expiration Date (MM/YY): ");
expDate = scanner.next();
if(cardVerifier.validate(cardNum, expDate))
System.out.println("Card number: " + cardNum + " is a valid credit card.");
else
System.out.println("Card number: " + cardNum + " is NOT a valid credit card.");
System.out.print("Run program again? (Y for Yes, N for No): ");
ans = scanner.next();
scanner.nextLine(); //discard the newline
}
System.out.println("Thank you. Goodbye!");
}
}
output
Card Number: 4192-1125-6633-1259 Expiry Date: 12/17 Valid: true
Card Number: 4283253533222358 Expiry Date: 12/17 Valid: true
Card Number: 4374398522116157 Expiry Date: 12/17 Valid: true
Card Number: 4292154566732358 Expiry Date: 12/17 Valid: true
Card Number: 4553223534333555 Expiry Date: 12/17 Valid: true
Card Number: 4643262531465454 Expiry Date: 12/17 Valid: true
Card Number: 4732169566119053 Expiry Date: 12/17 Valid: true
Card Number: 4823287533234752 Expiry Date: 12/17 Valid: false
Enter Card Holder's name: Dean Martin
Enter Credit Card Number: 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: 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!
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.