![]() |
![]() |
|
home—info—exams—lectures—labs—hws
Recipe—Laws—lies—syntax—java.lang docs—java.util docs
due 2008.Apr.06 (Mon)
Credit card numbers are always 16 digits long. To make the number more readable for humans, they they are written as four block of 4 digits, each block separated by a space (or, sometimes, a hyphen).
Frustratingly, many user-unfriendly websites ask users to type in a credit card number without any spaces or hyphens. (This makes it difficult for humans to look at what they've just typed and check for typos, or even if they have the correct number of digits.)
Your task is to write a static method which takes in a String typed into a website, and cooperatively return a credit card number (exactly 16 digits). That is, for each of the following three inputs, your method should return "6011000012345678"
For an extra 5 points, do some error checking on the input: make sure that it is a valid length, and that the separator (if used) is either a space or a hyphen, and is always the same (that is, 6011-0000 1234 5678 is not valid). In case of such an invalid input, your method should print a message to System.err, and return something reasonable.
There is more information associated with a credit card than just the account number:
Make a class to represent all the above information.
(Stop here and compile, before proceeding!)
Include the public methods (writing signature and test cases before the code, of course):
The first digit(s) of a credit card account number actually encode what type of card it is (Visa, Discover, etc.), as described here. Write a static method which takes a credit card number and returns the type of card.
If you did part (B): Modify your constructor so that it double-checks the stated company against what the account number says it should be. If there is a discrepancy then your constructor should throw new java.lang.IllegalArgumentException( msg ), where you replace msg with some particular String describing the mistake. (This causes the program to crash.2) When comparing Strings, remember η from the homework grading guide.
Credit card account numbers actually contain a check digit: The first 15 digits are “real” information, but the last digit is contrived so that adding up the 16 digits (with an extra twist described here4) always results in a multiple of 10. (You only have to worry about the types of cards which have exactly 16 digits.)
Write a static method which tells whether or not a (purported) credit card number has a correct check-digit. If you did part (B), modify your constructor so that it throws an exception if the the customer accidentally provided an invalid account number.
1Note that some information is kept track of by the bank, but is not actually part of the credit card info: the CID, the card's billing address, and the credit limit. ↩
2 In ITEC220 you'll learn how to write code which can catch exceptions and handle them — e.g. if the constructor is being called from your company's web page, then that web page can catch the error and have the user re-input their account number. ↩
4Challenge: Actually, there is one pair of (different) digits which can be transposed but still leaving the same check digit in this scheme, oops. 5pts extra credit if you can find it! (email me) ↩
3 If we just add up the digits, we can catch somebody mis-typing a digit. But if somebody transposes two adjacent digits (a common typing error), we wouldn't notice anything, because that wouldn't affect the sum4. That's why the described trick of doubling every other digit is used: it guards against a type of error which humans are prone to making. ↩
home—info—exams—lectures—labs—hws
Recipe—Laws—lies—syntax—java.lang docs—java.util docs
©2008, Ian Barland, Radford University Last modified 2008.Mar.20 (Thu) |
Please mail any suggestions (incl. typos, broken links) to ibarland ![]() |
![]() |