Alladaskill17 Posted March 12, 2009 Share Posted March 12, 2009 How do I ask ask a user to enter numbers as a long integer, or do they just input it, and I somehow check if it is a long integer? This problem deals with user putting in credit card number as a long integer. Link to comment Share on other sites More sharing options...
0 pradyot Posted March 12, 2009 Share Posted March 12, 2009 although i did not get much but my advice that u can display that user can enter number of the following size **** **** **** ie max 12 digit Link to comment Share on other sites More sharing options...
0 Antaris Veteran Posted March 12, 2009 Veteran Share Posted March 12, 2009 Seeing as you said this is for Credit Card numbers, what you need to have is the specifications of acceptable Credit Card lengths, and you might as well go ahead and look at Check digits too. I recently had to do a validator in PHP, so I can give you the following details: Card Type: Visa Acceptable Length(s): 13, 16 Acceptable Prefix(es): "4" Card Type: MasterCard Acceptable Length(s): 16 Acceptable Prefix(es): "51", "52", "53", "54", "55" Card Type: Amex (American Express) Acceptable Length(s): 15 Acceptable Prefix(es): "34", "37" Card Type: Discover Acceptable Length(s): 16 Acceptable Prefix(es): "6011", "622", "64", "65" Card Type: Solo Acceptable Length(s): 16, 18, 19 Acceptable Prefix(es): "6334", "6767" Card Type: Maestro Acceptable Length(s): 12, 13, 14, 15, 16, 18, 19 Acceptable Prefix(es): "5018", "5020", "5038", "6304", "6759", "6761" Of course there are other cards, but these were the main ones I am dealing with. What you would need to do, is validate the credit card number according to the card type. So if it was a Visa card, I would validate this according to the Visa card rules. The way I handled this in PHP, was to create an interface called iValidator, and have classes implement that interface, each validating its own rules. You could do the same in Java, firstly declaring your interface: public interface iCardValidator { Boolean isValid(String number); } You then need to implement the interface for each of your Card types, in the below example, ive done that for Visa and MasterCard, and to use some shared functionality from a static class CardValidator: public static class CardValidator { public static iCardValidator getValidator(String type) { switch (type) { case "visa": return new VisaCardValidator(); case "mastercard": return new MasterCardValidator(); default: return null; } } public static Boolean hasValidCheckDigit(String number) { Boolean result = false; int checksum = 0; int mul = 1; for (int i = 0; i < number.length(); i+= 1) { int num = 0; try { num = Integer.parseInt(number.substring(i, 1)); } catch (NumberFormatException ex) { break; } int calc = num * mul; if (calc > 9) { checksum += 1; calc -= 10; } checksum += calc; mul = (mul == 1) ? 2 : 1; } if (checksum == 0) return false; return ((checksum % 10) == 0); } public static Boolean hasValidLength(String number, int[] validLengths) { Boolean result = false; for (int i = 0; i < validLengths.length; i++) { if (number.length() == validLengths[i]) { result = true; break; } } return result; } public static Boolean hasValidPrefix(String number, String[] validPrefixes) { Boolean result = false; for (int i = 0; i < validPrefixes.length; i++) { if (number.startsWith(validPrefixes[i])) { result = true; break; } } return result; } } public class VisaCardValidator implements iCardValidator { public Boolean isValid(String number) { int[] lengths = new int[] { 13, 16 }; String[] prefixes = new String[] { "4" }; return ( CardValidator.hasValidLength(number, lengths) & CardValidator.hasValidPrefix(number, prefixes) & CardValidator.hasValidCheckDigit(number)); } } public class MasterCardValidator implements iCardValidator { public Boolean isValid(String number) { int[] lengths = new int[] { 16 }; String[] prefixes = new String[] { "51", "52", "53", "54", "55" }; return ( CardValidator.hasValidLength(number, lengths) & CardValidator.hasValidPrefix(number, prefixes) & CardValidator.hasValidCheckDigit(number)); } } Now, its easy to use, you simply do something similar to: String number = "4xxxxxxxxxxxxxxx"; iCardValidator validator = CardValidator.getCardValidator("visa"); Boolean isValid = validator.isValid(number); You'd want to flesh out the other card types as validator classes, and update the getCardValidator method to return one for each of the card types. I haven't test it, but it should pretty much be there. It's also a good example of some decent OO principals, as well as Factories, etc. Hope that helps, let me know :) Link to comment Share on other sites More sharing options...
0 primexx Posted March 12, 2009 Share Posted March 12, 2009 you'll be taking input as a string anyways so when you parse that into an int, do long instead. Link to comment Share on other sites More sharing options...
0 Antaris Veteran Posted March 12, 2009 Veteran Share Posted March 12, 2009 Lol, in all that I didn't actually check to see if it was actually a number at all, whoops. Link to comment Share on other sites More sharing options...
0 _kane81 Posted March 13, 2009 Share Posted March 13, 2009 ^^ Not quite the best design :/ you instigate a new validation object every call..... public static class CardValidator { public static iCardValidator getValidator(String type) { switch (type) { case "visa": return new VisaCardValidator(); case "mastercard": return new MasterCardValidator(); default: return null; } } should be public static class CardValidator { public static iCardValidator visaCardValidator = new VisaCardValidator(); public static iCardValidator masterCardValidator = new MasterCardValidator(); public static iCardValidator getValidator(String type) { switch (type) { case "visa": return visaCardValidator; case "mastercard": return masterCardValidator; default: throw new Exception("Invalid Card Type"); } } what if there are more cards ....here is a better solution public interface iCardValidator { Boolean isValid(String cardNumber, String cardType); } Link to comment Share on other sites More sharing options...
0 _kane81 Posted March 13, 2009 Share Posted March 13, 2009 // singleton pattern public class CardValidator { private static CardValidator instance; private ArrayList cardValidationList; public static CardValidator getInstance() { if (instance = null) { instance = new CardValidator(); } return instance; } protected CardValidator() { this.cardValidationList = new ArrayList(); this.cardValidationList.add(new VisaCardValidator()); this.cardValidationList.add(new MasterCardValidator()); } public boolean isValid(String cardNumber, String type) { boolean isValid = false; for (int i=0;(i < this.cardValidationList.size()) && !isValid; i++) { isValid = this.cardValidationList.get(i).isValid(cardNumber, cardType); } return isValid; } if (CardValidator.getInstance().IsValid(cardNumber, cardType)) { } to be more dynamic you could add methods to add and remove card types dynamically ;) Link to comment Share on other sites More sharing options...
0 _kane81 Posted March 13, 2009 Share Posted March 13, 2009 correction - u should use a hashmap instead of list ;) Link to comment Share on other sites More sharing options...
Question
Alladaskill17
How do I ask ask a user to enter numbers as a long integer, or do they just input it, and I somehow check if it is a long integer?
This problem deals with user putting in credit card number as a long integer.
Link to comment
Share on other sites
7 answers to this question
Recommended Posts