SO i have completed my credit card class which will be called into the data mana
ID: 3544597 • Letter: S
Question
SO i have completed my credit card class which will be called into the data manager class.
These are two different Java Files
The problem i am having is how to code:
An arrayList of the CreditCard objects
A method validateCard that takes in the cardHolder's name, card type and card number and returns a boolean based on the Luhn Algorithm.
The CreditCard class file code is:
public class CreditCard {
private String cardHolder;
private String AmericanExpress;
private String Visa;
private String Discover;
private String MasterCard;
private int cardNumber;
public String getCardHolder() {
return cardHolder;
}
public void setCardHolder(String cardHolder) {
this.cardHolder = cardHolder;
}
public String getAmericanExpress() {
return AmericanExpress;
}
public void setAmericanExpress(String americanExpress) {
AmericanExpress = americanExpress;
}
public String getVisa() {
return Visa;
}
public void setVisa(String visa) {
Visa = visa;
}
public String getDiscover() {
return Discover;
}
public void setDiscover(String discover) {
Discover = discover;
}
public String getMasterCard() {
return MasterCard;
}
public void setMasterCard(String masterCard) {
MasterCard = masterCard;
}
public int getCardNumber() {
return cardNumber;
}
public void setCardNumber(int cardNumber) {
this.cardNumber = cardNumber;
}
final String amExpress = "";
final String viSa = "";
final String disCover = "";
final String mCard = "";
public CreditCard(){
String cardHolder = "";
String AmericanExpress = "";
String Visa = "";
String Discover = "";
String MasterCard = "";
int cardNumber = 0;
}
public CreditCard(String name, int number){
cardHolder = name;
cardNumber = number;
}
public String toString(){
String str = "Credit Card Validator";
str += "cardHolder" + getCardHolder();
str += "AmericanExpress" + getAmericanExpress();
str += "Visa" + getVisa();
str += "Discover" + getDiscover();
str += "MasterCard" + getMasterCard();
return str;
}
}
How would i be able to code this? I appreciate the Help!
Explanation / Answer
// Here I have written down a method to validate the card using the Luhn algorithm
public boolean validateCard(String name, String cardType, int cardNum) {
int digit_sum = 0;
int num = cardNum;
int count = 1;
while (num > 0) {
int digit = cardNum%10;
if (count%2 == 0) {
int newdigit = 2*digit;
if (newdigit>10) {
digit_sum += (newdigit/10) + (newdigit%10);
}
else {
digit_sum += newdigit;
}
}
else {
digit_sum += digit;
}
num -= digit;
num /= 10;
}
if (digit_sum%10 == 0) {
return true;
}
return false;
}
// Here are the statements you need to declare a list 'N' of CreditCard in maybe your DataManager class:
CreditCard cards[] = new CreditCards[N];
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.