Wednesday 2 March 2016

Node JS - Project 2 - Bank Account

Hello! I'm back again!

In this project we want to be able to do the following things:

  1. Create new bank accounts
  2. Search for bank accounts
  3. Deposit and withdraw amounts
  4. Check account balances

In lesson 18 of The Complete Node JS Developer Course (check here for an update on my progress so far), the task was set out, and we had to go away and work through it independently. Lesson 19 is a step-by-step guide to a complete solution.

Getting Started


In lesson 18 we did the ground work for the project. This is the code we created to set the foundations:



We have created an empty array that will store all of the accounts, and there are some methods (or functions) that we can call on to make deposits, withdrawals, and check balances.

The Task


In the instructions we are told to create a new function called 'createAccount'. It's going to take an account object and store it in an array of accounts.

Each account object needs a 'balance', which should be a number, and a 'username', which should be a string.

The second function we're going to create is called 'getAccount' and takes one argument, which is 'username'.

The Solution


createAccount

We start with the 'createAccount' function. It accepts one argument, which is the object 'account'.


Basically, what we've done there is create a function, 'createAccount', that takes an account object and pushes it into the accounts array.

Now we can work on our 'getAccount' function.

getAccount


Within the function, we use forEach to iterate over the accounts and find any matching account.

We create a new variable called 'matchedAccount' within the function, which will store the account if a match is found.

Within the forEach, we want to check if the username of the account matches any of our accounts, so we create an if statement to do that. We then save the account to the matchedAccount variable that we created earlier.

We then return the matched account.

Re-cap


So, here's what we have so far:


Underneath that, we are going to create a new account.

New Account


We create a new account and save it to a variable. We then set that variable equal to createAccount, which is a function that expects an account object. The new object is created right within the function! Awesome! We give it a username and a balance. Yay!


Let's start as we mean to go on, and make a deposit into our account.

Deposit


We'll be using that function we created earlier on lines 19 to 21. Here's a quick reminder:


Here's how we make a deposit into our new account:


Lovely!

We use console.log to keep track of things, and we do this with the getBalance function (also created earlier on lines 27 to 29):


Withdrawal


Another old'un, but a good'un (on lines 23 to 25):


And here's how we use it:


getBalance makes another appearance :)

getAccount

The star of the show!

Create a new variable called existingAccount, and set it equal to getAccount, with the username passed in. It accepts one argument, a string.


It will return the balance of the my account!

We can test it out by creating another account for Pinky.


To make sure that everything is working as it should do, we can log out both of the accounts that we have created so far, by simply doing the following:


That will log them both out to the terminal, with their usernames and balances. Beautiful!

Finally, let's create a new variable for the Pinky's account (like we did with my account earlier):


Here's a re-cap of that code from line 31 onwards:


And it's all done! Until the next time :)