Step 7 - The Account
Now that we are finished (at least for now) with the Atm class, we should move forward and create theAccount
class.
We will go over the same steps as we did when creating the Atm
class.
create a test file (
account_spec.rb
) andcreate a implementation file (
account.rb
)
In the spec file we'll be writing our spec for code that we are planning to implement. As usual there are decisions about what attributes to add to the class and what methods needs to be defined.
Fortunately, we've already have much of the necessary information about that class to make those decisions easier.
Remember the instance_double
we used while testing the Atm
's functionality? If we examine that object closely we can see that the attributes we used ware :pin_code
, :balance
,:account_status
and :exp_date
. At the very least these are the attributes we need to assign to theAccount
class definition.
There is one thing that the Account
should have, that was never needed in the interaction wit Atm
but that should be a part of it. That is an account owner.
We'll let you code on your own but provide an recording that showcase a possible workflow.
Tip 1 - pin code
Some tips for testing.
A Pin number is generally a 4 digit number. One way to check if a number has 4 digits is this.
Tips: Try running parts of this in IRB if you wonder why the Math.log10
method can be used for this and why we need to call .to_i
and + 1
on the result.
Also, I would suggest that we randomize the pin code when we initialize a new Account object. A 4 digit number can be randomly generated with:
Tip 2 - expiry date
The expiry date on atm cards (and other credit cards) is generally stored in the format of month/year - like "04/16" that translates to April 2016.
When we set the :exp_date we need to make the calculation of today's date and add a predefined amount of years that we want the card to be valid for (remember that for the purpose of this exercise, the account symbolizes BOTH a bank account AND an atm card)
Let's write a test to see if the :exp_date
is set in initialize
.
spec/account_spec.rb
So, how can we implement a method to set an expiry date when an account object is created?
First of all, we need to tell the class that the default validity of the card is 5 yrs. To do that, we can set a type of variable called constant with the default value of how many years a new card should be valid for. It can look something like this.
(I would like you to find out why I use capital letters in the variable name and find out what we mean when we say that you need to avoid Magic Numbers in your code.)
When we set the :exp_date
value, we can start with today's date and add the value of the constant and finish off by formatting the date the way we want it. It could look something like this.
(Do you really need Account::STANDARD_VALIDITY_YRS
? Perhaps STANDARD_VALIDITY_YRS
in enough? Try it out...)
Tip 3 - the account status
Another check that the Atm does on the Account is to check the :account_status
. That will be the next thing we implement on the class.
We start with a spec.
spec/account_spec.rb
Notice that we are using the datatype Symbol to set the :account_status
.
And again we need to set that attribute in our initialize
method.
lib/account.rb
Okay, so an account has status of :active
when it is instantiated (created) . But how about if we would like to deactivate an account. We could simply set the value of the :account_status
attribute to:deactivated
. But we can (and should) create a method for that. The question is if we should create a Class method or an Instance method. You need to research the difference between this type of methods but consider this two different approaches.
Examine the two following ways of using the methods above.
Use the one that you find best in your implementation but please be ready to make an argument about your choice.
Tip 4 - the account owner
Each account that we create should have an owner. The owner should have a class of his own. For the moment that class is not defined so we will go ahead and pretend again that there is an instance of Person by using an instance_double
.
spec/account_spec.rb
First let's see if the owner is being set.
And add a test for a mandatory owner for each instance of Account.
spec/account_spec.rb
Moving on to the implementation to make these two specs to pass (as usual, this is a suggestion, examine the code closely before using it).
lib/account.rb
Last updated