Question
can someone help fix the code and test it and post proof it works i keep getting errorCode 2 1. The first digit must be
can someone help fix the code and test it and post proof it works i keep getting errorCode 2
1. The first digit must be a 4. 2. The fourth digit must be one greater than the fifth digit 3. The product of the first, fifth, and ninth digits must be 24 4. The sum of all digits must be evenly divisible by 4 5. The sum of the first four digits must be one less than the sum of the last four digits 6. If you treat the first two digits as a two-digit number, and the seventh and eight digits as a two digit number, their sum must be 100.
Hint: valid card numbers according to this set of rules include 4807-6052-1766 and 4094-3460-2754. You should use these for testing out your program. Note: You must include the dashes in your credit card number!
NEEDS to be used with this code and cannot change the following code in " "
"import java.util.scanner;
Public class CreditCardTester {
public static void main (String[]args){
System.out.println(please enter a 12-digit credit card number.);
Scanner scanner = new Scanner(System.in);
String creditCardNumber = scanner.next();
CreditCard card = new CreditCard (creditCardNumber);
Card.check();
if(card.isValid()) {
System.out.println(valid);
}
else {
int code = card.getErrorCode();
System.out.println(Oops! Thats an invalid number.);
System.out.println(The error code was: + code);"
this is the class in java this can be changed
public class CreditCard {
private String number; private boolean valid; private int errorCode;
public CreditCard(String number) { this.number = number; this.valid = false; this.errorCode = 0; }
public void check() { // Rule 1: The first digit must be a 4. if (number.charAt(0) != '4') { errorCode = 1; return; } int sum = 0; int product = 1; int firstFourSum = 0; int lastFourSum = 0; int firstTwo = Integer.parseInt(number.substring(0, 2)); int seventhEight = Integer.parseInt(number.substring(6, 8));
for (int i = 0; i < number.length(); i++) { char c = number.charAt(i); if (c == '-') { continue; } if (!Character.isDigit(c)) { errorCode = 2; return; } int digit = Character.getNumericValue(c); sum += digit; if (i == 0 || i == 4 || i == 8) { product *= digit; } if (i < 4) { firstFourSum += digit; } if (i > 7) { lastFourSum += digit; } }
// Rule 2: The fourth digit must be one greater than the fifth digit. if (number.charAt(3) - number.charAt(4) != 1) { errorCode = 2; return; }
// Rule 3: The product of the first, fifth, and ninth digits must be 24. if (product != 24) { errorCode = 3; return; }
// Rule 4: The sum of all digits must be evenly divisible by 4. if (sum % 4 != 0) { errorCode = 4; return; }
// Rule 5: The sum of the first four digits must be one less than the sum of the last four digits. if (firstFourSum != lastFourSum - 1) { errorCode = 5; return; }
// Rule 6: If you treat the first two digits as a two-digit number, and the seventh and eight digits as a two digit number, their sum must be 100. if (firstTwo + seventhEight != 100) { errorCode = 6; return; }
valid = true; }
public boolean isValid() { return valid; }
public int getErrorCode() { return errorCode; } }
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started