Here is my current code I have. *****In addition the program should have additio
ID: 671656 • Letter: H
Question
Here is my current code I have.
*****In addition the program should have additional output when the user exits the program. This additional output will be the following statistics:
The number of Visa Cards Processed, the number of Master cards processed, the number of American Express cards processed, the number of Discover cards processed and the total number of cards processed.
import java.util.Scanner;
public class Midterm {
public static void main(String args[]) {
Scanner input = new Scanner(System.in);
System.out.println("Enter Credit Card Number for Validation: ");
long number = input.nextLong();
long total = sumOfEvenPlaces(number) + sumOfOddPlaces(number);
if (isValid(total)) {
System.out.println(number + " is valid");
} else {
System.out.println(number + " is invalid");
}
}
public static boolean isValid(long total) {
if (total % 10 == 0) {
return true;
}
return false;
}
public static int sumOfEvenPlaces(long number) {
int sum = 0;
int remainder;
number %= 10;
while (number % 10 != 0 || number / 10 != 0) {
remainder = (int) (number % 10);
sum = sum + getDigit(remainder * 2);
number /= 100;
}
return sum;
}
public static int getDigit(int number) {
if (number > 9) {
return (number % 10 + number / 10);
}
return number;
}
public static int sumOfOddPlaces(long number) {
int sum = 0;
int remainder;
while (number % 10 != 0 || number / 10 != 0) {
remainder = (int) (number % 10);
sum = sum + getDigit(remainder);
number /= 100;
}
return sum;
}
}
Explanation / Answer
import java.util.Scanner;
public class Midterm {
public static void main(String args[]) {
Scanner input = new Scanner(System.in);
int[] count=new int[3];
int counter=0;
System.out.println("Enter the card type: (Visa/Master/AmericanExpress)");
String name=input.nextLine();
counter++;
if(name.equalsIgnoreCase("master")){
count[0]++;
}else if(name.equalsIgnoreCase("visa")){
count[1]++;
}else if(name.equalsIgnoreCase("americanexpress")){
count[2]++;
}
System.out.println("Enter Credit Card Number for Validation: ");
long number = input.nextLong();
long total = sumOfEvenPlaces(number) + sumOfOddPlaces(number);
if (isValid(total)) {
System.out.println(number + " is valid");
} else {
System.out.println(number + " is invalid");
}
if(name.equalsIgnoreCase("master")){
System.out.println("Number of master cards processed: "+count[0]);
}else if(name.equalsIgnoreCase("visa")){
System.out.println("Number of visa cards processed: "+count[1]);
}else if(name.equalsIgnoreCase("americanexpress")){
System.out.println("Number of AmericanExpress cards processed: "+count[2]);
}
System.out.println("Total Cards Accepted: "+counter);
}
public static boolean isValid(long total) {
if (total % 10 == 0) {
return true;
}
return false;
}
public static int sumOfEvenPlaces(long number) {
int sum = 0;
int remainder;
number %= 10;
while (number % 10 != 0 || number / 10 != 0) {
remainder = (int) (number % 10);
sum = sum + getDigit(remainder * 2);
number /= 100;
}
return sum;
}
public static int getDigit(int number) {
if (number > 9) {
return (number % 10 + number / 10);
}
return number;
}
public static int sumOfOddPlaces(long number) {
int sum = 0;
int remainder;
while (number % 10 != 0 || number / 10 != 0) {
remainder = (int) (number % 10);
sum = sum + getDigit(remainder);
number /= 100;
}
return sum;
}
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.