Commit 77073fa
Changed files (1)
src
src/Q3/CreditCard.java
@@ -1,11 +1,19 @@
+/**
+ * Assignment 2, COMP268 Class: CreditCard.java
+ *
+ * @description Provides an implementation of Luhn's algorithm
+ * @author: mo khan Student ID: 3431709
+ * @date Jun 9, 2019
+ * @version 1.0
+ */
package Q3;
import java.util.*;
public class CreditCard {
- private int evenSum;
- private int oddSum;
- private int sum;
+ private int evenSum = 0;
+ private int oddSum = 0;
+ private int sum = 0;
private String ccNumber;
private String company;
@@ -15,26 +23,52 @@ public class CreditCard {
this.calculateSums(num);
}
+ /**
+ * Returns the sum of the even digits
+ *
+ * @return the sum of the even digits
+ */
public int getEvenSum() {
return evenSum;
}
+ /**
+ * Returns the sum of the odd digits
+ *
+ * @return the sum of the odd digits
+ */
public int getOddSum() {
return oddSum;
}
+ /**
+ * Returns the sum of all the digits
+ *
+ * @return the sum of the even and odd digits.
+ */
public int getSum() {
return sum;
}
+ /**
+ * Return the credit card number
+ *
+ * @return the credit card number.
+ */
public String getCcNumber() {
return this.ccNumber;
}
+ /**
+ * Return the company that issues this credit card number.
+ *
+ * @return the name of the company that owns this credit card number.
+ */
public String getCompany() {
return this.company;
}
+ /** @return true if the credit card # is valid, otherwise returns false. */
public boolean isValid() {
return this.validateCompany()
&& this.validateLength()
@@ -43,10 +77,16 @@ public class CreditCard {
&& this.isDivisibleBy10();
}
+ /** @return true if the total sum is divisible by 10, otherwise returns false. */
public boolean isDivisibleBy10() {
- return ((this.evenSum + this.oddSum) % 10) == 0;
+ return ((this.getSum()) % 10) == 0;
}
+ /**
+ * Validates the company that the credit card number belongs to.
+ *
+ * @return true if the card belongs to one of the described credit card company, otherwise false.
+ */
public boolean validateCompany() {
return this.ccNumber.startsWith("4")
|| this.ccNumber.startsWith("5")
@@ -54,16 +94,19 @@ public class CreditCard {
|| this.ccNumber.startsWith("6");
}
+ /** @return true if the length of the card number is valid. */
public boolean validateLength() {
return this.ccNumber.length() >= 13 && this.ccNumber.length() <= 16;
}
+ /** @return true if each character in the credit card number is a digit. */
public boolean validateNumber() {
for (int i = 0; i < this.ccNumber.length(); i++)
if (!Character.isDigit(this.ccNumber.charAt(i))) return false;
return true;
}
+ /** @return true if the sum is greater than 0; */
public boolean validateSums() {
return this.evenSum > 0 && this.oddSum > 0;
}
@@ -96,6 +139,8 @@ public class CreditCard {
for (int j = 0; j < value.length(); j++) this.evenSum += this.digitFrom(value, j);
}
}
+
+ this.sum = this.oddSum + this.evenSum;
}
private String reverseString(String value) {
@@ -114,6 +159,11 @@ public class CreditCard {
return Character.getNumericValue(input.charAt(index));
}
+ /**
+ * The entry point to the console application.
+ *
+ * @param args the argument vector provided to the console application.
+ */
public static void main(String[] args) {
Scanner in = new Scanner(System.in);
System.out.println("Enter credit card #:");