Skip to content

Instantly share code, notes, and snippets.

@SoPat712
Created July 11, 2021 22:49
Show Gist options
  • Save SoPat712/3a44bae862d9f06f296cfae1a91e4f2c to your computer and use it in GitHub Desktop.
Save SoPat712/3a44bae862d9f06f296cfae1a91e4f2c to your computer and use it in GitHub Desktop.
Java Intermediate: 07/11/2021
package com.javafx.test;
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
scoopIceCream("chocolate", true);
scoopIceCream("vanilla", false);
scoopIceCream("strawberry", true);
System.out.println(average(5, 4)); // prints 4.5
System.out.println(average(0, 10)); // prints 5.0
System.out.println(average(1.5, 3.75)); // prints 2.625
System.out.print("Enter a credit card number: ");
long number = input.nextLong();
// print whether the number is valid
if (isValid(number)) {
String cardType;
// determine card type based on prefix
if (prefixMatched(number, 4)) {
cardType = "Visa";
} else if (prefixMatched(number, 5)) {
cardType = "Master";
} else if (prefixMatched(number, 6)) {
cardType = "Discover";
} else {
cardType = "American Express";
}
// display the card type
System.out.println("That is a valid " + cardType + " card number.");
} else {
System.out.println("That is not a valid credit card number.");
}
input.close();
}
public static void scoopIceCream(String flavor, boolean wantCone) {
if (wantCone) {
System.out.println(
"Here's a scoop of " + flavor + " ice cream on a cone!"
);
} else {
System.out.println("Here's a scoop of " + flavor + " ice cream!");
}
}
public static double average(double a, double b) {
return (a + b) / 2;
}
/**
* Returns true if the card number is valid
* @param number credit card number
* @return true if the card number is valid
*/
public static boolean isValid(long number) {
// number has a valid prefix if it begins with 4, 5, 6, or 37
boolean prefixMatches =
prefixMatched(number, 4) ||
prefixMatched(number, 5) ||
prefixMatched(number, 6) ||
prefixMatched(number, 37);
if (number < 0) {
// if number is negative, it's invalid
return false;
} else if (getSize(number) < 13 || getSize(number) > 16) {
// if number is NOT between 13 and 16 digits (inclusive), it's invalid
return false;
} else if (!prefixMatches) {
// if the number does not have a valid prefix, it's invalid
return false;
} else {
// if the number passes all of the other tests,
// do the Luhn check (aka Mod 10 check)
int sum = sumOfDoubleEvenPlace(number) + sumOfOddPlace(number);
return sum % 10 == 0;
}
}
/**
* Returns the sum of the doubled even-place digits,
* from right to left
* @param number credit card number
* @return the sum of the doubled even-place digits,
* from right to left
*/
public static int sumOfDoubleEvenPlace(long number) {
int sum = 0;
// start at the second-to-last digit
number /= 10;
int result;
// while there are still digits remaining
while (number != 0) {
// get the rightmost digit
int rightmostDigit = (int) (number % 10);
// double it
rightmostDigit *= 2;
// the result is either the doubled value itself
// or the sum of the two digits of doubled value
result = getDigit(rightmostDigit);
// add that result to the running sum
sum += result;
// move 2 digits to the left (the next even place)
number /= 100;
}
return sum;
}
/**
* Returns the given number if it is a single digit,
* otherwise return the sum of the two digits
* @param n a doubled even-place digit of a credit card number
* @return the given number if it is a single digit,
* otherwise the sum of the two digits
*/
public static int getDigit(int n) {
// if n is 2 digits
if (n > 9) {
// get the first and second digits
int firstDigit = 1; // if n is 2 digits, n can only be from 10-18
int secondDigit = n % 10;
// result is the sum of the 2 digits
return firstDigit + secondDigit;
} else {
// if n is 1 digit, result is n itself
return n;
}
}
/**
* Returns the sum of the odd-place digits,
* from right to left
* @param number credit card number
* @return the sum of the odd-place digits,
* from right to left
*/
public static int sumOfOddPlace(long number) {
int sum = 0;
// while there are still digits remaining in number
while (number != 0) {
// get the rightmost digit
int rightmostDigit = (int) (number % 10);
// add the rightmost digit to the sum
sum += rightmostDigit;
// move 2 digits to the left (the next odd place)
number /= 100;
}
return sum;
}
/**
* Returns true if d is a prefix for a number
* @param number credit card number
* @param d credit card number prefix
* @return true if d is a prefix for a number
*/
public static boolean prefixMatched(long number, int d) {
return getPrefix(number, getSize(d)) == d;
}
/**
* Return the number of digits in the given number
* @param number a long integer
* @return the number of digits in the given number
*/
public static int getSize(long number) {
int numberOfDigits = 0;
while (number != 0) {
number /= 10;
numberOfDigits++;
}
return numberOfDigits;
}
/**
* Return the first k number of digits from number.
* If the number of digits in number is less than k,
* return number.
* @param number credit card number
* @param k number of first digits to retrieve from number
* @return the first k number of digits from number.
* If the number of digits in number is less than k,
* returns number.
*/
public static long getPrefix(long number, int k) {
// If the number of digits in number is less than k,
// return number
if (getSize(number) < k) {
return number;
} else {
// keep removing rightmost digits
// until the length of the number is equal to k
while (getSize(number) > k) {
number /= 10;
}
// once loop ends, number is the prefix
return number;
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment