Okay, so it's time for a challenge.
In the previous video we went through auto boxing and unboxing and
what we're gonna do now is start our challenge which is going to be for
a simple banking application.
So I'm just gonna type out what is actually needed.
Okay, so here is the task, so
your job is to create a simple banking application as I mentioned.
So there should be a Bank class.
Now that Bank class should have the bank name, but
also an ArrayList of branches, which is a class.
And each branch should itself have an ArrayList of customers which is another
class, now the customer class itself should have the customer name and
also an array list of doubles.
Doubles being the object replica version of
the primitive top double which is the list of transactions for that customer.
So, in other words what we are doing is we are tracking transactions that a customer
would make.
The customer class would have the name and
the array list of doubles of that code as I mentioned.
So for the branch, the functionality you need is to be able to
add a new customer to that branch and for the initial transaction amount.
So that would be in the scenario where it's a brand new customer whose walked
into the bank.
And one’s to open an account.
So in other words, you need to add them to the branch, and add their name and
also the amount.
Also when I add a transaction for an existing customer.
So in other words, if they come back and then wanna add additional transactions.
So they should be able to process an unlimited number of transactions for
a given customer.
But still be attached to that branch.
So the customer that's should still be part of that branch.
And also we want to be able to show a list of customers for
a particular branch as well, that's very important.
And optionally a list of their transactions,
so you can decide to show their transactions as well or not show them.
And what you should be doing is using a demonstration of autoboxing and
unboxing in your code, and my hint for that is the transaction,
is probably a good place to look for that.
Also be sure to add data validation.
So in other words if you're adding a new customer to a branch,
you wanna check that, that name wasn't used previously for that branch and
likewise if you were adding an additional transaction for an already existing
customer for a branch you'd wanna check then in fact they did exist first.
We'll say think about where you're going to be adding this code.
Where you're adding code to perform certain actions.
Making sure that you think you're putting it in the right class.
For example, if your doing something to do with branches should probably
be in the branches class and that sort of thing.
But there may be scenarios you'll find that you're calling the functionality for
a particular class from another class, so the hint with that one will be the bank.
Nearly all your transactions that you're gonna be running in your main class,
your main method, that's going to be probably just using bank.
So that's going to have to a lot of methods as you can see on
the screen there, adding a new branch, adding a customer,
adding a transaction showing a list of customers.
But probably what you'd be finding is that bank class would be calling
code from other classes.
So that's where I'm getting it, so
think about where you're gonna be adding that code.
All right, so I'm gonna pause the video here, so go away and
see if you can create something like this.
And remember, it doesn't have to be terribly complicated,
just keep it pretty basic.
The customer's literally only the name and the transactions at this stage.
You don't need to store addresses or anything like that.
The main thing is the relationship between the various classes, and
also the autoboxing and unboxing.
So I'm gonna pause the video here, so come back and
start the video when you're ready to see what I've come up with.
Okay, how did you get on?
Did you figure it out?
So let's go through with my implementation, what I'm gonna do first is
just open a project window so we can see what we're doing here a little bit.
And the first class that I'm going to create is the customer class.
So in a design like this, when you're thinking about where do I start,
I would always start from the bottom rung.
So in this case we know that the customer is a major
part of this application because it's pretty well used everywhere.
So we need to have a customer before we can run any other code.
So I'm gonna create that new class, so I said customer, and
in terms of functionality, we're going to just have a name.
So private string name, which will be of course the customer name.
And we also want our ArrayList of Doubles.
And this is going to be the transactions, so we're gonna call that transactions.
Next we need a constructor.
So the constructor is going to accept two parameters.
So the first one is going to be the name, so we're just gonna store the name.
But the second parameter is the initial transaction.
So this is when a customer is opening a new account for a particular branch.
We want to have that initial amount, so we're gonna put a double there.
We're just gonna send a regular double and we're gonna call that initialAmount.
So it'll be the initial transaction, ultimately it's gonna be,
we'll call it initialAmount.
So have to think about how we're gonna store that initial amount.
We'll come back to that and add that shortly.
But the other thing we need here is we'll need a mechanism to add transactions.
So as customers come back to the bank or to the branch, whatever the case may be,
and actually add another transaction we need to be able to store those
in our array list.
So we want another method.
The public, void, add transaction.
We're gonna accept an amend at double.
Now it sets the primitive top and not the object wrapper.
And this will be this .transactions.add amount.
And obviously that's converting from a primitive to the object
wrapper which we've got in our array list so that's demonstrating boxing.
So getting back now to the constructor in this class to add that initial amount,
what we can do is we can actually just call that add transaction.
Literally that's all we need to do.
So to do that, before we actually do that, what we need to do is one other step.
And we need to actually initialize the transactions because we have to find
the array list of double transactions at the top here in the private field,
but we haven't initialized it.
So we need to initialize that first before we try and add anything to it.
And of course the constructors are great place for initializing.
So we're gonna do this .trail transactions equals new.
Of course that's gonna be the array list of doubles.
So then the next line is there, we're gonna initialize that, and
we saved the name.
We can just do add transaction.
Add transaction and pass the initial amount, and
that's gonna add that to our array list of transactions.
So we're now handling the initial amount and also subsequent amounts as well.
Next what we wanna do is we want to create some getters,
and the getters are going to be, we'll do both the name and by the transactions,
because we might need those in another class.
So that's our base customer class.
There's not really a lot of processing there because a lot of the processing
is in other places.
So the next class I'm gonna now is the branch.
So we create the new class branch.
Now, with this one, if you recall, going back to the main with the branch,
we need to be able to add a new customer and initial transaction amount.
We also need to add additional transactions for that customer.
Because we're actually adding a customer to a branch, we need to, obviously,
store a list of customers in the branch.
So to do that, coming back here, firstly we're going to assign the name, and
that going to be, in this case, the branch name.
Private string, well call that name again.
And we also need to add that list of customers.
So what this means is we can have a separate list of customers for
each branch.
So it's using the ArrayList.
Problem is some customers, we'll call that customers.
Substitute fields we need the constructor is gonna be very simple,
it's going to be initializing the string to whatever has been passed to it.
Then we also need to initialize the array list just to make sure it's ready to
be any records.
So I've got customers equals new Array list customers, as you can see there.
So initializing that for the first time.
We're also going to need the getter for the name.
We'll probably implement that now,
because we wanna be able to show the branch name on the screen at some point.
So we're just gonna used the getter for that.
The next one we want is the new customer.
So if you recall back in the main method when I talked it out.
For branch, we need to be able to add a new customer and
an initial transaction amount.
But we also need to add additional transactions for customer branch as well.
So going back to our branch,
first one we're going to do is we're going to create a method.
Public, and we'll use a boolean and I'll explain why shortly.
We'll call this one newCustomer.
And the parameters that are going to be passed to this are the name,
we'll type it out properly, customerName in this case.
And also the initial amount, so initialAmount.
The reason that we're gonna use boolean is because this could file.
If we pass a customer name that already exists,
we wanna actually return false to say that this transaction, or
this customer wasn't added because it was already on file.
That's why we're using a boolean.
So to do that of course we need to add a find customer class so
that we can actually test to see whether that class is actually being used or not.
So the logic of the code will be something like this.
Find customer and it'll be customer name.
If it equals null and that's telling us that it's not already on file so
because this is a new customer method,
if it's not on file that's a good thing because it means we can add it.
So if that's the case we're going to put our list.customers.add so, adding a new
record and you can see there on the screen it's looking for a customer object.
So we're gonna do a new customer, creating a new class and we're
just gonna pass those two parameters, customerName and initialAmount.
So obviously in that case with the new customer,
we're actually calling the constructor code in the customer class which of
course would be the right way of doing it.
This is all we need to do for the new customer so
we just need to do a return true at that point.
A true bit, and meaning in this case we successfully added it.
And so therefore if the code gets down to here,
it means that findCustomer actually returns a customer.
And that means that we can't add the new customer at that point,
because they already exist.
So if that was the case, we wanna return false.
That's what return false is there.
Okay, and next what we wanna do is we need to actually add the add transactions.
The add transactions is much the same as the new customer, but
the test here is we're actually checking to make sure it is on file.
And if it's not on file, we actually return false because it means that we
can't, obviously, add another transaction to a customer that doesn't exist.
So the code is that would be public.
Berlin at transaction is cut customer transaction.
Type it out so it makes sense.
Transaction and it'll be string customer name and it would be double amount.
This time it's not an initial amount because I've already that so,
our logic would be if find customer
customerName, if it's not equal to null,
so in other words, something has been returned, so it is on file.
So if that is the case, then what we wanna do there is we simply want to make sure we
grab the existing customer record and
update that by adding that amount to the transaction.
So to do that we've gotta do Customer existingCustomer
Equals findCustomer, and what I might do is just clean this up a little bit.
Cuz we've actually added a extra code.
And we don't code called findCustomer twice there.
So what I'm gonna do is actually put this code like.
Calling it once and we're gonna just do a test there.
And we're just gonna change this code just to existing customer.
And by doing it this way, we're actually only calling findCustomer once.
So instead of before, we've just used that one line which was cool because we didn't
actually need to use that object cuz we're actually testing for null.
In this case with addCustomerTransaction,
we actually are gonna use that to update to therefore it makes sense to save
the object here and then use it within here when we actually get a result back.
Okay, so if that's the case, so the customer is actually on file, so
all we need to do is put existingCustomer.addTransaction(amount).
And obviously, the addTransaction method is from the customer, so we're actually
executing the addTransaction method, which is actually this method here.
So obviously that's good because it's actually calling a method that's actually
in the class.
And what we could do there, the reason that's a good idea,
getting back to a customer class, we could actually add some validation in here.
We could print a message, to output, to log or do some other sort of checking
to make sure the transaction was an acceptable range as those types of things.
In other words what we're doing is making sure that we're calling the functionality
that's appropriate for this class.
If we're adding a transaction to a customer,
this would be the logical place to do it, because anytime I want to make a change to
the customer class we would actually do it in that class.
So that's why we're putting that there.
So that's good.
So we're just about done now with our branch.
What we haven't done yet though, is we haven't actually implemented the find.
So let's just add that find so we can fix up that error.
And actually what I haven't done yet is updated this here.
So let's return the statuses.
So I return true because that did succeed at that point.
And then it gets down to here where it's returning false it means that we were
unable to find a customer, so we should actually return false if that's the case.
Okay, so the next one is find customer, so we're gonna make it a private.
Because this is only a method that's used internally in this branch class, so
we shouldn't be exposing it publicly.
Anytime you're creating a class, you wanna take care and
consider each of your methods to see whether they need to be public or private.
And usually, in a lot of the case, they can be private and that makes it a lot
easier from the point of view of someone using your class because they don't have
to worry about anything that's private because they won't see that when they're
looking in the list of methods that they need to work with.
It just makes it a lot easier and it means that you can change a lot of things around
which we've talked about in previous videos.
So we're going to return a customer in this case, so
private customer, find customer.
String customerName.
And to do this we need to go through our list of customer records that are in
the array list.
So for int I equals zero.I less than customers.size so
you'll never get through all the records.
I++ then the test that we're going to actually check for
is we're gonna put ifthis.customers
.get i.getname.=customername.
Then we want to return this .customers.geti.
Otherwise all the way down here, we actually want to return null.
And what I'll do also with this, I'm gonna clean this up a little bit as well,
because you can see there's a problem.
This line here was actually retrieving the object,
and then actually we were retrieving it again.
So that's just really double processing, so
let's just do that by something like this with a customer.
Check to customer equals this.customs.dot get(i).
And then we're gonna change the slot late to put if customers, like so.
And we’re gonna return to customer.
So that just makes it a little bit more efficient looking at the code there now.
So basically what we’re doing is we’re going through the list of all
the customers that we’ve got stored for this branch, and we’re testing it.
Firstly we’re retrieving the the one that's in the array list
as we go through each and every element.
Then what we're doing is checking to see if the name for that customer is the same,
in other words is a match to the customer name that's been passed to this method,
and if you recall the new customer and
add customer transaction methods are actually using this.
They're actually passing the name of the customer so, if there's a match there then
we're going to return the customer entry that matched that from the array list.
Otherwise we're gonna return null, and
null actually means that there was no matches and that customers not on file.
So lets define customer, and I think we're actually done with that now.
So what we've got to do next is the bank class and then what we need to do is wrap
that up a little bit and add some code to actually test on this.
So time is ticking on so, I'm gonna end this video here and
we'll come back in the next video we'll implement the bank classes in and
then we'll go through and start running some test code in our main class.
So I'll see you in the next video.