Lab 9: Credit Cards
Due Date: April 04
- help students practice writing Ruby classes
- help students practice coding techniques discussed in class
- help students practice writing regular expressions
- reinforce previous lessons in unit testing
Due Date: April 04
Get a copy of the starter files Prof. H has posted online at github.com. This code has a
credit_card.rb file where you will write your solution, a
credit_cards_context.rb file with 30 valid and invalid cards of various types, and a
credit_cards_tests.rb file for testing the validity and type identification of the context cards. There is also a
sandbox.rb file for some simple, off-the-cuff experimentation if you wish.
Your assignment is to write a
CreditCard class that will adhere to a limited set of business rules listed at the end of the lab. The class should have a method called
valid? that will make sure that card number provided adheres to these rules and that the expiration date is today or in the future. If time allows, you should create a
type method which determines the card type (VISA, MC, etc.) from the card number given using the provided CreditCardType class.
This class will need an initializer that takes in three variables. Check out the format in
For our testing purposes in this lab, there is only one
valid? method which will be false if either the credit card number or the expiration date is invalid. You should very likely split these into separate methods within the class and have the
valid? method use those methods. You may even create additional classes if you'd like; structure the code using some of the ideas discussed in lecture last week and you will be fine.
Show a TA that you have the tests for
valid? passing correctly. If time allows, continue and implement the
type attribute based on the CreditCardType class.
Credit card numbers follow a certain pattern specific to the type of credit card, and follow an algorithm called the Luhn algorithm. For the purposes of this problem, we only want to check some credit-card specific information, and not bother with the more complicated Luhn formula.
Credit card type-specific rules generally state that a number must start with a string of digits, and have a specific length. An incomplete list of the rules that we are applying here can be seen in the table below:
|American Express||AMEX||34 or 37||15|
|Discover Card||DISC||6011 or 65||16|
|Visa||VISA||4||13 or 16|
Use regex to check the credit card numbers!