In the previous video we talked about constructors.
So let's now finish off our discussion about constructors and
come up with a constructor challenge.
So the other thing you may have notice looking at this coding is
that I've actually updated the fields directly.
I haven't used the setters.
So there is an alternative, what we could have done,
is we actually could have done something like set number.
And the advantage of that would be, of course, if we had some
validation in here that was testing for valid numbers and those types of things.
We could actually execute that code as well.
Now in Java there is conflicting opinions as to which is the best approach.
Cuz you'll find out in the next video when we start talking about sub classing,
in other words talking about inheritance, this won't always work.
So the general rule of thumb is it's always better to save the values of
the constructor directly to the field rather than calling the setter.
Cuz as you'll see in the next video,
there can be scenarios where this code that's in this setter isn't executed.
So by going back and actually doing the code directly, in other words,
going back and setting it to this dot and whatever the field name is,
you're guaranteed that the field values will be initialized.
So that's my general rule of thumb with constructors is don't call setters or
any other method other than another constructor within those constructors.
And the other reason for that is,
this is the point in the constructor where the object is being created.
So consequentially, some aspects of the initialization may not have been finished
while you're in the constructor.
And that's the other reason there's an opinion out there that suggests that you
shouldn't be calling other methods or even the setters within the constructor code.
But we can talk more about that later if needed.
For now I can go with what I've suggested which is just
saving the field values directly rather than calling setters or any other method.
Now the other thing I wanna show you is another shortcut that IntelliJack got.
Let's just assume that we wanted to create another constructor that we only
parsed the customer name, email addresses, instructor phone number and
we want the other two parameters to be created automatically by the computer.
So we could do that by creating another constructor.
But we could also come up here and do that with IntelliJack.
It's got the capability for
adding constructors as well in that generate option.
Click on generate and we're gonna select the first option which is constructor, and
it asks which field do you want to include in the constructor?
So which ones ae we gonna have passed to us, in other words,
which of the parameters to be used.
So I'm gonna select those three, customer name, email address and the phone number.
Had those.
So there's our third constructor, now.
And you can see it's only updating the three fields.
So that's one way of doing it.
But, of course, the disadvantage, there, is that our number and
our balance haven't been automatically updated.
So what we could do, is we could put this, and
we can assume it's gonna be the default account number,
we might say is 99999 and the default balance is 100.55.
Then we'll pass the customerName,
the customerEmailAddress, and the customerPhoneNumber.
So you see what we've done there is we've defaulted two parameters.
So we've come up with what the default is because they weren't specified.
And we've still gone back and called our major constructor.
This is the one that actually updates all the fields.
So you will find as you start creating and
writing more complex code It's not unusual to see multiple constructors like this.
And in that situation, often you do all your initialization in the one
constructor, like you can see here.
And the other constructor it should just call that major constructor.
That's a good way of doing things and
it often leads to really good coding cuz you're not having to duplicate code or
duplicating initialization in more than one place, so on and so forth.
So how would we call that?
We would call that very much the same as we've been doing before.
We could do, create an E class here.
Account, Tim's account equals new account.
Now you can see what's happened there is you've got three options now.
IntelliJ's saying,
okay there's three constructors that have been created for this class.
So we'll start typing, you can type, you can put Tim,
tim@email.com.
And one, two, three, four, five for the phone number.
I've got a 1950s phone number.
Okay, so you can see how that constructor was created, and of course,
we can confirm that's working.
timsAccount.getNumber, to make sure it's a number,
and we'll just timsAccount.get.
And just make sure the name is actually set.
Let's just run that to make sure that it's working.
99999 which was the default account number here.
So you can see and the name was Tim which of course was what we parsed here.
So, that's constructors.
You'll see those used extensively in Java.
And of course, we'll be using that a lot in this course as we move forward,
because they are a very important part of creating objects from classes.
Okay. So, it's time for a challenge.
I'm going to carve that code out.
Okay, so the challenge is to create a new class called VipCustomer.
So, it should have three fields, the name, the credit limit, and the email address.
So, what you want to do is create three constructors.
The first constructor should be empty.
And that should call the constructor with three parameters that we're gonna create.
Passing on any default values.
The second constructor should pass on the two values it receives as parameters and
then default the third value.
And the third constructor should just save all the fields that it receives,
cuz it's gonna have the three parameters one for each field.
Create getters, and so
so you don't need setters because we're saving the field values using the constructor.
And see if you can figure out how to create getters only using the code generation
feature of IntelliJ, and of course then test and
confirm it works by writing code in the main class that was generated by IntelliJ.
So, pause the video now, go away, and give that a go.
And when you're ready to see what my solution is, come back, and
we'll take it from there.
Okay, how did you get on?
Did you figure it out?
Now, let's get started.
Need to bring up my project window.
Now, it's command 1 a Mac.
If you wanna know what it is on your system, go to Tools>Window and
look at the shortcut.
So, we're gonna create a new class called VIP person.
Need to have three fields.
Okay like so, and the first contractor should be empty the second one
should pass through the two valleys.
The third contractor should save all the fuels.
So what we can do, is we can use the generational features of Intelli J,
create a constructor.
The first one we're gonna do is create the constructor with the three values.
And this is why you'll start getting used to IntelliJ code generation features,
because it's really a much quicker way of creating stuff than typing it.
Really handy for this little stuff.
So the second constructor only needed two parameters,
so let's default the email address.
and just save the first tooth.
Dot save.
It will come back to that in a second.
And the third constructor, of course,
is just going to be VIP person without any parameters.
And that's gonna call this, so we'll set that one up first.
It's going to be default to
default name 50,000 and
email address default @email.com.
This one here we don't want to save the fuels we want to call
the other constructor.
So what we want to do there is we've got the name of the credit limit, so
we're gonna put this name, creditLimit.
And we're gonna default and we'll call it unknown, just so
we know that it's different.
So that's our three constructors now that are created.
You can see the first two constructors call the third constructor.
The third constructor is the only one that does save valleys into our fuels.
And using our code generation I'm using the shortcut but
if you don't know what that is go into the code menu, generate and
look at the code shortcut for your platform.
Now this is the one I wanted to show you, "getter" only.
You don't have to do "getters and setters," just type getter,
type the three- or select the three that you want to create getters for,
press enter, and there's our three getters created.
Very nice.
Okay so what we now need to do is confirm that this works.
So we just go back to our main class.
We're going to do VipPerson.
And we'll just put person1 = new VipPerson and
we should be able to do person1.getname,
just to confirm that something's worked there.
We show the ones that k that all of them are.
So VRP person, person two equals VIP person.
And of course we've got one there with two parameters which are the names.
So we'll put Bob and 25,000.
And the third parameter should be created for us automatically.
And we'll do similar thing,
person2.getname which will be Bob because we parsed that.
And VipPerson person3
= new VipPerson Tim.
$100.00.
tim@email.com.
And print out person3.getName().
So that should be our code now to test that this is working.
Let's run that.
Default name was the first one with no parameters.
So we just go back and check our code.
We can see that the default code was Default name 50000.00 and default@email so
that's working.
And for the second one, I run that again.
Bob was the second one, so that's good for him.
That's worked, and the third one was Tim which we've confirmed is also working.
What we can do there, if we really wanted to be sure,
we could actually just do a printout of the also of the person3.getEmailAddress
just to make sure that it is set to tim@email.com.
Run that.
Tim@email.com which,
of course, hasn't been used anywhere else here as the other constructor.
So that's it, that's constructors.
Hopefully you've got a lot out of that.
We actually had a student who actually looked at
this particular lecture after it went live.
And notice the small problem.
So I just want to go over and show you, the discussion in the course first.
This is from Brian N.
and he actually said that when creating the withdrawal method's "if statement,"
the operator, which is less than equal should be a strict inequality (<).
And that's because if you deduct the withdrawal amount from the balance
a valid amount is still having a zero balance.
And the way I've coded it in the example
is that it's less than equal rather than less than.
So what I'm gonna just show you what I mean and make the change just so
you can see.
How the code was meant to look, so we'll go back to the code and have a look.
The problem is, if we go into account, and
specifically the withdraw method, at the moment, you can see the withdraw method.
The test is, if the current balance,
this.Balance take the withdrawal amount is less than or equal to zero
then we're coming up with an error, that the withdrawal won't be processed.
When in actual fact, it's quite valid, say if you've got a hundred dollars in your
bank account, to withdraw a hundred and to leave a balance of zero.
That's quite valid.
But this code as it's written,
because we're using less than or equal to zero, won't actually work.
It won't actually process that.
If you're entering an amount, a withdrawal amount, that's once deducted off
the balance, it leaves you with exactly zero money in your account.
So, to get this to work, what we need to do is add comments to some of this code
cuz the problem is in the account class.
So I'm going to actually briefly comment this out.
Cuz we're not using that for this part of the video, comment that out and
actually uncomment this component here.
If we go back and just have a look at our constructor again,
just to familiarize ourselves with again.
We've actually got a balance of 100.55 which is given to us by default if
we're actually using this particular constructor.
Now just to confirm that, if you go back and go into the main class again.
And we'll put [SOUND] current balance
is timsaccount.getbalance.
And if we run that, just to verify with- that's still the amount
of money that's in there current balance is $100.55.
Okay, so far so good.
So now if we try to withdraw that exact amount,
so we put timsAccount.withdraw and
the exact amount, which was 100.55.
So we try and do that and process it.
Current balance is 155, only 155 available, withdrawn, not processed.
Well that's clearly an error and we should be able
to withdraw $100.55 because that's what the amount is in the bank.
So we go back to our account clause and
the problem is in this withdrawal method where it's got less than equal,
less than is the appropriate operand which to add here.
We only actually want to cause it to come up with an error if deducting
an amount off of the balance leaves us with an amount that is less than zero,
instead of less than or equal to zero.
So now if we go back, and run this again.
We actually withdrawal of $100.55 processed.
Remaining balance is 0.0, which, of course, is quite valid.
So that's it.
I just wanted to add this little part in here just to be correct with the code.
Thanks again to Brian for pointing that on, pointing that out, I should say.
And just to let you know I've also updated the source code.
So if you do download the source code you'll actually see that
fix in the code as well.
So in the next video what we're going to do is we're gonna talk about inheritance,
because each time you create a class, what's happening is Java is actually
giving you those extra fields and methods that we talked about previously.
So we're gonna talk about inheritance and how that works in the next video.