My problem is line 24 of my code. My whole code will compile and the error I get is this:
CreditCardValidator.java:24: error: method isValid in class CreditCardValidator
cannot be applied to given types;
isValid(card);
^
required: String,int,int,String
found: String
reason: actual and formal argument lists differ in length
1 error
My goal is to use "isValid(card)" to Display the type of credit card and if the card is valid or invalid. Our instructor gave us the outline of the program and we have not learned about arrays or anything like that. We're a beginner level computer class. If anyone could show me the code that would properly validate the credit card and explain it to me that would be appreciated. I've been working on trying to solve this problem for the past few hours now and have not been able to figure it out. Our instructor doesn't really explain anything, we all have to figure it out ourselves with no help besides ourselves. Thank you! Here's my code so far. I have a test string currently being used. The program will use user input when I can get the program to work properly.
/**
Credit Card Validator
@author Your Name
@date Today's Date
@class Our Class
*/
import java.util.Scanner;
public class CreditCardValidator {
public static void main (String[] args) {
String card = "4012888888881881";
// When you are finished writing the methods below,
// uncomment the three lines below to test.
// Scanner input = new Scanner(System.in);
// System.out.print("Enter a Credit Card Number: ");
// String card = input.nextLine();
if (isValid(card) ) {
// System.out.print("valid");
// } else {
// System.out.print("invalid");
// If False, state the card number is invalid
// }
}
/**
Returns true if the card number is valid
To determine if a card is valid, the sum of the Double Even Place
Numbers and the Sum of the Odd Place Numbers must be divisible by
ten (10), the String must be 13 to 16 digits, *and* the String must
start with "4", "5", "37", or "6".
@param number: A 13 to 16 digit String of numbers
@returns true if the String is a valid card, False otherwise
*/
public static boolean isValid(int totalEven, int totalOdd, String company) {
if (((totalEven + totalOdd) % 10 == 0) && company.equals("Visa")) {
return true;
} else {
if (((totalEven + totalOdd) % 10 == 0) && company.equals("Master Card")) {
return true;
} else {
if (((totalEven + totalOdd) % 10 == 0) && company.equals("American Express")) {
return true;
} else {
if (((totalEven + totalOdd) % 10 == 0) && company.equals("Discover Card")) {
return true;
} else {
return false;
}
}
}
}
}
/**
Double every second digit from *right to left*.
If doubling of a digit results in a two-digit number, add the two digits
together to get a single digit number using the getDigit(...) method.
Use a *loop* to cycle through all the numbers of the String.
Note: You will need to *convert a char to an int*
@param number: A 13 to 16 digit String of numbers
@returns an integer
*/
public static int sumOfDoubleEvenPlace(String number){
int totalEven = 0;
int start = number.length() - 1; // starts at 14
// i = 14; 14 > 0; 14 -= 2
for (int i = start; i >= 0; i -= 2) {
char c = number.charAt(i);
int digit = Character.getNumericValue(c) * 2;
// System.out.println(digit + " " + getDigit(digit));
totalEven += getDigit(digit);
}
return totalEven; // Total of double even place
}
/**
Return this number if it is a single digit, otherwise, return the sum
of the two digits. For example, 18 will return 9 (because 1 + 8)
@param number: a digit that will be between 0 and 18
@returns an integer
*/
public static int getDigit(int number){
int calc = 0;
if (number < 10) {
// This is one digit
} else {
// This is two digits
calc = number - 9;
}
return calc;
}
/**
Return sum of odd-place digits in number
Use a *loop* to cycle through all the numbers of the String.
Note: You will need to *convert a char to an int*
@param number: A 13 to 16 digit String of numbers
@returns an integer
*/
public static int sumOfOddPlace(String number){
int totalOdd = 0;
int start = number.length();
for (int i = start; i >= 0; i -= 2) {
char c = number.charAt(i);
int digit = Character.getNumericValue(c) * 2;
totalOdd += getDigit(digit);
}
return totalOdd;
}
/**
Return the company of the card by looking at the character
at the zero (0) index of number.
@param number: A 13 to 16 digit String of numbers
@returns a String that is either
"Visa", "Master Card", "American Express", or "Discover Card"
*/
public static String getCompany(String number){
int card = number.length();
int i = card - number.length();
char c = number.charAt(i);
int digit = Character.getNumericValue(c);
int d = i + 1;
String company = "";
if (digit == 4 ) {
company = "Visa";
} else if (digit == 5) {
company = "Master Card";
} else if (digit == 6) {
company = "Discover Card";
} else if (digit == 3 && d == 7) {
company = "American Express";
}
return company;
}
}
Aucun commentaire:
Enregistrer un commentaire