4 Replies - 3206 Views - Last Post: 17 November 2013 - 05:06 AM Rate Topic: -----

#1 FRESHPINETRICKZ  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 3
  • Joined: 16-November 13

Credit Card validation assignment help

Posted 16 November 2013 - 06:10 PM

Hi guys! I'm new to these forums, so I guess I'll start by saying hello world! :D/> So, on to business. I have an assignment due tomorrow in my java class (Yes it's due on Sunday, I have to email it to my professor). Unfortunately, I have spent the good part of my weekend trying to figure out what's wrong with my program, but it seems that I need some help. So, the assignment uses the "mod 10 algorithm" to validate credit cards. Basically, the assignment asks to write a program that prompts the user to enter a credit card number as a long integer, and to display whether the number is valid or invalid. But there is some special restrictions that my professor set for us. First of all, I have to use certain methods, these methods have to be private (Not a problem), the program can't exclusively use the mod 10 to check for validity; it must also check that the digits are between 13 and 16 digits, and I can't use arrays or string processing. Note that this is not a "copy and paste" of my homework assignment, as even though it seems like a lot of description, these are things that I had to mention in order for other's to understand the problem that I'm having with my program. Simply put, my program always displays invalid for everything that I input. Even if it's a valid number. (4388576018410707 <-- valid number, 4388576018402626 <-- invalid number). I am having difficulty finding what is wrong with my coding (You don't say?), and if anyone can shed some light on what I'm doing wrong, that would be very helpful. Thanks!

  
import java.util.Scanner;

public class CreditValidation {

	public static void main(String[] args) {

		Scanner input = new Scanner(System.in);
		System.out.print("Enter credit card number: ");
		long num = input.nextLong();
		input.close();

		if (isValid(num))
			System.out.println("Credit Card Number is GOOD!!!");
		else
			System.out.println("Credit Card Number is BAD!!!");

	}

	//*****************************************************************************
	
	private static boolean isValid(long num) {
        if (num <= 0)
			return false;

	int size = getSize(num);
	if (size < 13 || size > 16)
			return false;

	    int pre1 = (int) getPrefix(num, size - 1);
		int pre2 = (int) getPrefix(num, size - 2);
		if (pre1 != 4 && pre1 != 5 && pre1 != 6 && pre2 != 37)
			return false;
		
	    int total = (sumOfDoubleEvenPlace(num) + sumOfOddPlace(num));
		
		if ((total % 10 == 0) && prefixMatched(num, 1) == true) {
		return true;
		}
		
		else {
		return false;
		}
	}
	//********************************************************************************
	
	private static int getSize(long x) {
		int count = 0;
		while (x > 0) {
			x = x / 10;
			count++;
		}
		//System.out.println(count);
		return count;
	}

	//*******************************************************************************
	
	private static int sumOfDoubleEvenPlace(long x) {
		int sum = 0;
		while (x > 0) {
			int last2Digits = (int) (x % 100);
			int evenDigit = last2Digits / 10;

			sum = sum + getDigit(2 * evenDigit);
			x = x / 100;
		}
		//System.out.println(sum);
		return sum;
	}
  
	//******************************************************************************
	private static int getDigit(int num) {
		if (num < 10) {
		return num;
		}
		if (num > 10) {
		num = ((num / 10) + (num % 10));
	    }
		return num;
		
	  // System.out.println(number);
	  //System.out.println(sum);
	}
	
	
	//******************************************************************************
	
	private static int sumOfOddPlace(long num) {
	   int sum = 0;
	   while (num != 0) {
		   sum = sum + (int)(num % 10);
		   num = num / 100; 
	   }
		//System.out.println(sum);
		return sum;
   }

	//*****************************************************************************
	
	private static boolean prefixMatched(long num, int d) {
		
		if (d == getPrefix(num, 2) || d == getPrefix(num, 1)) {
			return true;
		}
		else{
			return false;
		}
	}

	//******************************************************************************
	private static long getPrefix(long num, int k) {
	long sum = 0;
	sum = sum + num;
	int i  = 0;
	while (i < getSize(num) - (k - 1)) {
    i++;
	}
    sum = sum / 10;
    return sum;
	}
}



Is This A Good Question/Topic? 0
  • +

Replies To: Credit Card validation assignment help

#2 andrewsw  Icon User is online

  • blow up my boots
  • member icon

Reputation: 6500
  • View blog
  • Posts: 26,288
  • Joined: 12-December 12

Re: Credit Card validation assignment help

Posted 16 November 2013 - 06:25 PM

The problem is within your getPrefix() method which, for the suggested input, sets pre1 and pre2 both to the value 2138503086.

This is because this method does nothing other than divide the number by 10, which is then cast to an integer.

This post has been edited by andrewsw: 16 November 2013 - 06:30 PM

Was This Post Helpful? 1
  • +
  • -

#3 FRESHPINETRICKZ  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 3
  • Joined: 16-November 13

Re: Credit Card validation assignment help

Posted 16 November 2013 - 06:50 PM

View Postandrewsw, on 16 November 2013 - 06:25 PM, said:

The problem is within your getPrefix() method which, for the suggested input, sets pre1 and pre2 both to the value 2138503086.

This is because this method does nothing other than divide the number by 10, which is then cast to an integer.


Thanks so much for the quick reply. I'm sorry if I come across as stupid here, but I'm new to coding (And I've got a nasty virus. :sad2: ) . I'm struggling to see how is it that both pre1 and pre2 are being set to the same, ridiculous number. I'm messing with my loop and how it's returned (What I think is causing the issue), but still can't get it to work. Like I said, I'm very sorry if I'm sounding like an idiot here.

private static long getPrefix(long num, int k) {
	long sum = num;
	int i  = 0;
	while (i < getSize(num) - (k - 1)) 
    i++;
	sum = sum / 10;
    return sum;
	}
}



Never mind! Man, this cold is getting into my head now. I'll try again at fixing it now. (My previous code didn't change anything.)

Still can't get my code to work. But thanks a lot for pointing me in the right direction. I'll come back to this in 10-30 minutes with fresh eyes and see if I can see what's up. If I can make it work, I'll post back. In the meantime, any more input and explanations is also appreciated! :)
Was This Post Helpful? 0
  • +
  • -

#4 FRESHPINETRICKZ  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 3
  • Joined: 16-November 13

Re: Credit Card validation assignment help

Posted 16 November 2013 - 07:30 PM

Sorry everyone. Still having problems. Can't figure out was is wrong with my code. Any more input or explanations is very much appreciated. Like I said, I'm very new to coding and having quite a difficult time figuring this out.
Was This Post Helpful? 0
  • +
  • -

#5 andrewsw  Icon User is online

  • blow up my boots
  • member icon

Reputation: 6500
  • View blog
  • Posts: 26,288
  • Joined: 12-December 12

Re: Credit Card validation assignment help

Posted 17 November 2013 - 05:06 AM

So what is getPrefix() supposed to do, and what should it return?

You wrote that code so you'll need to explain it - what is should be doing.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1