So in the previous video we started our challenge for autoboxing and unboxing and
we've implemented the customer and branch classes.
So let's go ahead now and implement the bank class which is the third component.
And just as a recap,
what we're wanting to do with the bank is we want the ability to add an new branch.
We also want to add a customer to that branch with the initial
transaction amount.
We also want the ability to add transactions for an existing customer for
that branch.
And finally, we also wanna show a list of customers for a particular branch and
optionally display a list of the transactions for those customers.
So first things first, let's create our class.
So the class we're creating is bank.
We're just gonna call it bank.
And we need to assign a bank name, even though we're probably not gonna use it.
We'll actually just use that as a name of the actual bank.
And we need to create an array list, in this case of branches,
because a bank, of course, can have multiple branches.
So we're going to do that with an array list.
Right.
An array list of branch.
We're going to call that branches.
So the constructor is going to be a simple one.
When we're creating a new bank, it's literally just gonna be the name, but
also, as you've seen in other classes in the constructor,
we're going to initialize the array list.
So I'm going do branches equals new ArrayList.
So we're going to initialize that so it's ready to be used.
So the first method we need to create is going to be the add branch.
We're going to create that very similar to how you saw
me adding the code for adding customers and so forth.
It's something similar so it's gonna return a boolean.
It's called add branch and the string parameter's
going to be parsed out as a string and it's gonna be called branch name.
We're gonna do something similar like if find branch branchName.
If that is equal to null, and we're gonna go ahead and then add it.
So this.branches.add and it's gonna be a new branch.
And it's gonna be branchName.
So we're gonna return true and
if we don't get to this point we're gonna return false.
It's very similar to the code that you saw in the previous video.
We're testing to see whether the branch exists.
If it returns null, it means that that name hasn't been used before,
so in other words,
the branch doesn't exist and we can safely add that to our array list of branches.
And obviously we're calling the branch constructor, in that case,
just parsing along the branch name.
So that's add branch.
Nothing complicated about that at all.
Next thing we need to do is we'll need to add the method, rather, for add customer.
So that's going to actually need, because we can potentially have multiple branches,
every time we add a customer, we need to specify the branch name, the customer
name, and the initial transaction, when we are adding a customer.
And likewise for the customer transactions, we need to do the same.
So we are going to do public boolean again,
add customer, and the parameters will be, branch name,
customer name, and, of course, the initial amount.
This is when the customer walks in and wants to open an account at this branch.
So what we're going to do then is we're going to test to see whether it's on file.
So we'll do branch, branch equals find branch.
Branch name. And because we're adding,
we wanna put if branch name.
So we can't find the branch name.
There's a problem with the branch name.
So if that returns null, it means there's actually a problem, so
we actually wanna do a test to see if we're not equal to null.
In other words,
we're going to only be able to add a customer if the branch name exists.
So if we've got the branch name, it means that we're able to proceed and
actually add that.
So at this point, we can actually do a return.
And I'll explain this shortly.
But we're gonna return branch.newCustomer.
And the parameters for that were customer name and initial amount.
So what we've done there is we've just saved a line of code.
We could have actually stored here that boolean
to return what the answer was to that.
So we'll go back and have a look at this code.
Public we're calling the new customer code, if you recall, and
that's doing the text there to see if it's equal to null.
The customer wasn't on file.
We're gonna add it and then we're returning true.
So what we've really done here, by just doing a return branch.newCustomer.
Not only are we executing the code to add the customer, but
we're also gonna return the status of that, as well, to the calling process.
And, obviously, it gets down to here.
We got a return false,
which means that we weren't able to actually store the customer.
So that's add customer and again,
we're checking to see first whether the branch exists and then we're leaving
the check to see whether the customer can be added to the appropriate class.
So we're actually calling the branch code to do the checking in there.
So you can see how we're separating the data validation into the right areas, so
we're not putting huge amounts of data validation in one spot.
And this is one of the keys to object-orientated programming, to make
sure that you're trying to validate only a specific thing in a specific area.
And, therefore, your code is more compact and more along the lines of a black
box functionality and it's easy to maintain cuz it's fewer lines of code and
just really easy to understand as well.
So that's adding a customer for the first time and again, the point I want to
make here is that you can see we're actually calling the other code.
So we're calling the branch code here for the new customer.
So it's going into the branch class to perform that code.
We're still calling the actual code from the bank class.
But the branch code has got the code to do the processing of this
case of the new customer and
we're trying to keep the functionality relating to each class separate.
Okay, so that was add customer.
The next one was for adding transactions.
I'm gonna call that one add customer transactions cuz it's gonna
perform a similar role.
So it'll be public boolean addCustomerTransaction and
we still need the branch name, so it's branchName.
And it's gonna be customerName again and it's going to be an amount,
which is the transaction amount.
So we'll do the same thing, we're gonna do a test to see whether
the branch exists first, so branch = findBranch, branchName.
And likewise, in this case, if the branch isn't found, there's a problem with it and
we can't find the branch, we obviously can't continue.
So we can put if branch is not equal to null, it means that it
was able to be found and we can actually go ahead and actually call the code.
So to do that we can do return branch.addCustomerTransaction,
customerName and amount like so.
And if we get down to here, we can return false.
So really is operating very much similar to the add customer method.
The only difference was we actually had a separate method for a new customer and for
adding in other transactions.
So that's really the only difference that those two methods are really doing.
But again, we are using the branch code, we're actually calling the code within
that particular class, which is the way we wanna do it.
We wanna separate our code wherever possible.
Next, we need to implement the find branch and
to do that we can go back to our branch, find customer.
You can just really copy that and just change a few things because, essentially,
it's the same code.
So we're just going to paste that in.
And we're not gonna return a customer, we're gonna return a branch.
So it's gonna be branch, branch and this should be branch as well.
Branch, and it'll be branch.
When we go through and check it for branches in our find branch code,
branches and would be a branch, might actually be quicker to type it in.
Check branch.
Check branch, and then we're gonna go copy and paste to fix that up.
Oops.
All right, so there's our find code.
It's accepting and I'll change that, as well.
We'll call that branchName, and
I think it would have been quicker to type it in but anyway.
So we've got our find branch method accepting the branchName as parameter,
going through all the entries in our array list, checking to see whether there's
a match with a branch name that has been sent to this method.
If there is, we return the branch record, the object that's in the array list,
in other words.
Otherwise if we go right through the loop and we can't find anything,
then we're returning null, which means there wasn't a match.
So that's literally all that's doing.
You've seen that in operation before.
Now the next one that we want to implement,
going back to our main, we wanna actually implement this one now.
Show a list of customers for
a particular branch and optionally, a list of their transactions.
So that's probably a more meaty part of the code.
So we're gonna make up this space here.
Let's implement that.
So to do that, we're gonna create a method.
We're just gonna call it list customers.
So we're gonna call it public.
We're still going to call it, even though it's gonna just display a list,
we're going to return a boolean in case the branch wasn't found, so
the calling processing will know that.
So we're going to still parse the branch name because we still need that branch
name to search for and we're going to use a boolean called showTransactions And
showTransactions is gonna be set to true if we want to list the transactions for
customers and false if we only wanna see the customer names.
So again the same code at the start branch
equals findBranch, branchName.
Sorry, if branch is not equal to null, that means we found a match.
We can process it.
Otherwise we're gonna put a return false down here which
means there was a problem finding, in this case, the branch.
Okay, so now that we've actually got that.
So what we'll do at first is we're gonna print out a message.
And the message I'm gonna put is customers for branch plus branch.
Whoops.
branch.getName.
Now I could have just used branchName there as well
because obviously if there was a match, it means that branchName was correct.
But because we're using the branch object and putting branch in there as well,
just so it's really clear to the person reading the code, I should say,
that what we're trying to do.
We put customer details for branch.
So the next thing we actually we need is a list of our customers,
because we know which branch we're dealing with, so we want to actually
get a list of customers that are part of the branch that we've just retrieved.
So to do that, we're gonna create a variable.
And that's gonna be a list of customer records.
We're gonna call that branchCustomers.
And we're gonna use branch.
And at the moment we haven't implemented that.
So what we're really want to do here is just use the create getter for
the list of customers from the branch effectively.
We can go back here and add a getter to that.
Easiest thing to do would probably be to just to add the getter there for
a getCustomers and we can use that.
And that's gonna return a list of customers for that particular branch.
So if we do that now .getCustomers.
At this point, we've now got the list of customers for the branch that we're
looking for, so we need to go through a for loop and print some of these out.
So I've got for int i equals zero, i less than,
of course, it's branchCustomers.size, i++.
We're gonna print a message out here, and we'll put customer.
This is the customer we're working on.
So I'm put branchCustomers, which and what well do there is we'll actually put
customer branchCustomer equals, just to stop us having to repeat ourselves here.
BranchCustomers don't get i.
So that's the current entry that we're actually working on there now.
So we're gonna put branchCustomer don't get name.
And the other we'll just put in square brackets here, is just the number that
we're working on, just sort of the loop entry, which is just for our purposes.
So we're gonna show that the customer, and that's showing the name of the customer,
and if you recall, we said optionally show the transaction, so I'm gonna put if
showTransactions, so this can only get executed if showTransactions is true.
Then what we're gonna do there, of course,
we now need to get a list of transactions for those customers.
So first I'm gonna put a message to put transactions.
Transactions like so.
Now we need an array list of our doubles, of our transactions for that customer.
So to do that we can put an array list double.
We'll call that transactions.
And that's gonna be equal to our branchCustomer.getTransactions.
So we're gonna return that list of transactions on file for
that specific customer.
Now, now that we've done that, we need to actually do another loop now,
a loop three because, of course, they can be multiple transactions, and
print each one of those out.
So to do that we're gonna put for int j equals 0, and
you'll notice I'm doing j there because if I try and put i there,
because we're already within a loop, above us you can see, we're going
to get an error to say that i has already been define in the scope, as you can see.
So we need a j here, and I'll just go through that again shortly so it's clear.
It's gonna be j.
It's gonna be less than, in this case, it's transactions.size j++.
And then we're just gonna print out, and
what we'll do is we'll actually print out the transaction number as well.
So I'm just going to use square brackets plus and
we'll start counting from one, like so, instead from zero.
That's the transaction number and
then we'll put amount plus, of course,
this is gonna be transactions.get i, sorry j.
I nearly fell for that trap myself.
Now I could put .doubleValue there.
But we're using unboxing here, so I'm just going to leave that like so, and
I think I made a typo here.
That definitely needs another one there anyway.
Oops, looks like a typo there.
So that's what it should be.
Okay, so just gonna go through this again so
you're clear on what we're trying to do here.
We're starting off on here and
we're actually getting a list of all the customers for the particular branch.
So, therefore, the looping is starting using the variable i and
we're going through that.
But then, optionally, we had this showTransactions and
if that was set to true when we call it, the idea of that is
to show a list of all the transactions for a particular customer.
Consequently, we need to have a second loop, and
because we're looping within a loop, we need to use a different variable name.
So the whole purpose of this method, again,
is to list all the customers for a given branch.
And optionally, all the transactions for each customer.
And that's technically what we're doing here.
The other thing I might do here is, up here, I've put in this i,
I'm gonna put i plus one here.
And what that's doing, it's just gonna show just the number starting from one and
show the list of different customers so
we can see those when we're actually going through and outputting it.
And the other thing we need to do,
if we've successfully been through the loop here.
We actually want to return that point, going to put an else here.
Else.
Return false.
Because that would actually indicate there was an error and
what I'm gonna do is clear that up.
And here I'm gonna put return true.
So basically the code is here.
Say if branch is not equal to null, we're going to print out the customer names and
optionally the transactions, then return true.
That's what this first line here's doing.
Otherwise, if the branch in fact was null, we're
gonna return false which really signifies to us that we couldn't find the branch.
Now incidentally, these for loops, and because we're using .size, and
because we've got an array list that's been initialized.
If there no customers on file, then it's still gonna work.
It just won't actually print any customers out.
That's the beauty about using these functions and
making sure the array lists are initialized.
This will handle array lists of size zero.
You're not going to get an error or
anything like that because of the way we've actually implemented it.
All right, well this video has gone on and on again, so
we've now actually implemented the bank, the branch, and the customer.
So we're actually done with the three major components.
The only thing left for us to do, which we'll now do in the next video,
is just to write some test code to actually make sure this all works,
then add a bank, add some branches, add some customers, and
just check that the customer transactions that we had will also work.
So we'll do that in the next video.