6.9 OOP constructor-2
Hello, in the previous course, we deepened the concept of builder. In this course, we are going to do ... practice through an exercise. The exercise is to create a bank account. In which one can deposit or withdraw ... money And in this exercise we will make out to create of the three types of builder, that is to say ... the default builder. A constructor with parameter and a constructor without argument and at the end of this course ... we will set up a challenge so that you can yourself Learn to use constructors so ... start we will create our class account. which are the subject of a bank account Account and we will put. the ... account number forgives the account balance the customer’s name his email and phone number So Ò ... private We will make the balance. which is going to be a double its phone number and then its email address and ... to finish its name and the we will generate the getters and setters to make them automatic, I’ve already shown you ... So Generate or command n generate getter and setter and select it all we have getter ... and setter here. and we will create our first public void method which will not return anything we call deposit and ... in parameter we will put double Sommedepot the amount to deposit. so we’re going to increase the account balance, we’re going to make this balance Equals this balance plus the amount the customer is going to deposit or we could have made ... that. we’re going to put the most Here its comes back to the same and now we’re going to make another method to ... the deposit withdrawal thought to put comments in your public code parameter will put double sum and ... there, we’re going to put a condition. if the balance Less the amount withdrawn. is less than 0 We will ... show the amount available that can withdraw. So only this Available on Else, we will remove the amount. What has ... withdrawn from his account balance this amount withdrawal we will indicate. So will show him the amount, what a ... withdraw. and the balance that is in his withdrawal account Equal current balance equal we will also put it. the amount he has left after depositing so balance equals now that we have finished making the methods ... the more the getter we will return to our hand so will just check we have created the method to deposit ... and to withdraw. and we’ve done the same. We’ll create an account object and we’ll deposit. we will deposit money if our method functions. deposit we will put 100 euros. we will compile. the balance ... is 100 euros Now, we will withdraw withdrawal 50 so we will compile. 50 withdrawal and the current balance ... is at 50 the two methods function correctly Now we will assign the account to a customer. set the client’s name ... we’ll call him John counts his email john phone number his account number 12 345 of course, ... we can display everything, but we’ll display only the account name getNom we compile our program and there, on ... a john with his account is balance that has 100 He has withdrawn 50 so he has 50 Now ... imagine that we have more than 20 or 30 or 50 to fill so it will take a longer ... his will take a lot of time to fill all the setters and then call them getters and display them ... but there is a simpler way but before that, you have to know that. that we created an instance ... of our class account we used the default constructor. the default constructor was called. But right now... we can use the builder. with parameter and with the constructor that we can put parameters we can put the ... name of the user, his mail, are phone number. and the account number so we will go back to our account ... in our class account we and make the builder with parameter So, to create a builder, we must put the ... class name so public the name of the class account Opens the parenthesis and the braces. in the ... parenthesis, as we will initialize the number the balance the phone number, the mail and the name we will put ... in parameter. Number Comma the balance Then the phone number. the mail and to finish the name so this Number ... Equal to number. this balance Equal a balance Phone number. email this name Equal So, now we have our builder ... with parameter. But we will also put the manufacturer without parameters. So public Account So in the hand we have ... used this manufacturer. Now we change, we’ll use the constructor with parameter We’ll go back. You just have to ... put. The parameters you put here, that is John the name Comma the balance, we will put. 300 by ... example the phone number Then there is the mail. and to finish the name so the name, it’s John ... the number, it’s 123 Now we don’t need the setters that we put here so we’ll just take it off ... to be sure that everything is fine we’ll go back to our class counts. and we will put sout ... count we will put the constructor with parameter we return in the main method. Before compiling the program, we will ... put in comment. the line 21 Knowing that the balance of is 300 euros and that we have also deposited ... 100 euros, it means that the balance of the account will be 400 euros So it is indeed 400 and ... the manufacturer with parameter was called since in the manufacturer, we just got from inquart Constructeur with parameter ... So in the terminal you see that it was called. we made a withdrawal of 50 so the balance ... current is 350 euros. Now that you have seen how the builder works with parameter and without parameter in ... the next course we will continue on this exercise. and the end of the next course We will perform the challenge, ... a challenge. that will allow us to swallow everything. What we learned so see you soon.